Skip to content

Commit 1acc0af

Browse files
authored
Merge pull request #334 from uraid/master
Fix face and object recognition of rotated images
2 parents 10e0526 + 70fec24 commit 1acc0af

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

photonix/classifiers/face/model.py

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from photonix.classifiers.face.deepface.commons.distance import findEuclideanDistance
1818
from photonix.classifiers.face.deepface.DeepFace import build_model
1919
from photonix.photos.utils.redis import redis_connection
20+
from photonix.photos.utils.metadata import PhotoMetadata
2021

2122

2223
GRAPH_FILE = os.path.join('face', 'mtcnn_weights.npy')
@@ -72,8 +73,17 @@ def load_graph(self, graph_file):
7273
def predict(self, image_file, min_score=0.99):
7374
# Detects face bounding boxes
7475
image = Image.open(image_file)
76+
7577
if image.mode != 'RGB':
7678
image = image.convert('RGB')
79+
80+
# Perform rotations if decalared in metadata
81+
metadata = PhotoMetadata(image_file)
82+
if metadata.get('Orientation') in ['Rotate 90 CW', 'Rotate 270 CCW']:
83+
image = image.rotate(-90, expand=True)
84+
elif metadata.get('Orientation') in ['Rotate 90 CCW', 'Rotate 270 CW']:
85+
image = image.rotate(90, expand=True)
86+
7787
image = np.asarray(image)
7888
results = self.graph['mtcnn'].detect_faces(image)
7989
return list(filter(lambda f: f['confidence'] > min_score, results))

photonix/classifiers/object/model.py

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from photonix.classifiers.object.utils import label_map_util
1212
from photonix.classifiers.base_model import BaseModel
1313
from photonix.photos.utils.redis import redis_connection
14+
from photonix.photos.utils.metadata import PhotoMetadata
1415

1516

1617
GRAPH_FILE = os.path.join('object', 'ssd_mobilenet_v2_oid_v4_2018_12_12_frozen_inference_graph.pb')
@@ -115,8 +116,17 @@ def format_output(self, output_dict, min_score):
115116

116117
def predict(self, image_file, min_score=0.1):
117118
image = Image.open(image_file)
119+
118120
if image.mode != 'RGB':
119121
image = image.convert('RGB')
122+
123+
# Perform rotations if decalared in metadata
124+
metadata = PhotoMetadata(image_file)
125+
if metadata.get('Orientation') in ['Rotate 90 CW', 'Rotate 270 CCW']:
126+
image = image.rotate(-90, expand=True)
127+
elif metadata.get('Orientation') in ['Rotate 90 CCW', 'Rotate 270 CW']:
128+
image = image.rotate(90, expand=True)
129+
120130
# the array based representation of the image will be used later in order to prepare the
121131
# result image with boxes and labels on it.
122132
image_np = self.load_image_into_numpy_array(image)

0 commit comments

Comments
 (0)