Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
damianmoore authored Sep 1, 2021
2 parents b9b6438 + 10e0526 commit 70fec24
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion photonix/classifiers/color/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self):
self.colors = {
# Name: ((red, green, blue), ordering)

'Red': ((120, 4, 20), 1),
'Red': ((120, 4, 20), 1),
'Orange': ((245, 133, 0), 2),
'Amber': ((234, 166, 30), 3),
'Yellow': ((240, 240, 39), 4),
Expand Down
7 changes: 5 additions & 2 deletions photonix/classifiers/face/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ def load_graph(self, graph_file):
def predict(self, image_file, min_score=0.99):
# Detects face bounding boxes
image = Image.open(image_file)


if image.mode != 'RGB':
image = image.convert('RGB')

# Perform rotations if decalared in metadata
metadata = PhotoMetadata(image_file)
if metadata.get('Orientation') in ['Rotate 90 CW', 'Rotate 270 CCW']:
image = image.rotate(-90, expand=True)
elif metadata.get('Orientation') in ['Rotate 90 CCW', 'Rotate 270 CW']:
image = image.rotate(90, expand=True)

image = np.asarray(image)
results = self.graph['mtcnn'].detect_faces(image)
return list(filter(lambda f: f['confidence'] > min_score, results))
Expand Down
7 changes: 5 additions & 2 deletions photonix/classifiers/object/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ def format_output(self, output_dict, min_score):

def predict(self, image_file, min_score=0.1):
image = Image.open(image_file)


if image.mode != 'RGB':
image = image.convert('RGB')

# Perform rotations if decalared in metadata
metadata = PhotoMetadata(image_file)
if metadata.get('Orientation') in ['Rotate 90 CW', 'Rotate 270 CCW']:
image = image.rotate(-90, expand=True)
elif metadata.get('Orientation') in ['Rotate 90 CCW', 'Rotate 270 CW']:
image = image.rotate(90, expand=True)

# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = self.load_image_into_numpy_array(image)
Expand Down
49 changes: 30 additions & 19 deletions photonix/classifiers/style/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from photonix.classifiers.base_model import BaseModel
from photonix.photos.utils.redis import redis_connection
from photonix.web.utils import logger


GRAPH_FILE = os.path.join('style', 'graph.pb')
Expand Down Expand Up @@ -71,6 +72,10 @@ def predict(self, image_file, min_score=0.66):
input_mean=input_mean,
input_std=input_std)

if t is None:
logger.info(f'Skipping {image_file}, file format not supported by Tensorflow')
return None

input_name = "import/" + input_layer
output_name = "import/" + output_layer
input_operation = self.graph.get_operation_by_name(input_name)
Expand All @@ -90,22 +95,25 @@ def predict(self, image_file, min_score=0.66):

def read_tensor_from_image_file(self, file_name, input_height=299, input_width=299, input_mean=0, input_std=255):
input_name = "file_reader"

file_reader = tf.io.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels=3, name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader, name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels=3, name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0)
resized = tf.image.resize(dims_expander, [input_height, input_width], method=tf.image.ResizeMethod.BILINEAR, antialias=True)
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.compat.v1.Session()
return sess.run(normalized)
try:

file_reader = tf.io.read_file(file_name, input_name)
if file_name.endswith(".png"):
image_reader = tf.image.decode_png(file_reader, channels=3, name='png_reader')
elif file_name.endswith(".gif"):
image_reader = tf.squeeze(tf.image.decode_gif(file_reader, name='gif_reader'))
elif file_name.endswith(".bmp"):
image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
image_reader = tf.image.decode_jpeg(file_reader, channels=3, name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0)
resized = tf.image.resize(dims_expander, [input_height, input_width], method=tf.image.ResizeMethod.BILINEAR, antialias=True)
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.compat.v1.Session()
return sess.run(normalized)
except:
return None


def run_on_photo(photo_id):
Expand All @@ -114,7 +122,7 @@ def run_on_photo(photo_id):
from photonix.classifiers.runners import results_for_model_on_photo, get_or_create_tag
photo, results = results_for_model_on_photo(model, photo_id)

if photo:
if photo and results is not None:
from photonix.photos.models import PhotoTag
photo.clear_tags(source='C', type='S')
for name, score in results:
Expand All @@ -132,5 +140,8 @@ def run_on_photo(photo_id):

results = model.predict(sys.argv[1], min_score=0.01)

for label, score in results:
print('{} (score: {:0.5f})'.format(label, score))
if results is None:
print(f'{sys.argv[1]} could not be processed by style classifier')
else:
for label, score in results:
print('{} (score: {:0.5f})'.format(label, score))
3 changes: 2 additions & 1 deletion photonix/photos/utils/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
redis_connection = redis.Redis(
host=os.environ.get('REDIS_HOST', '127.0.0.1'),
port=int(os.environ.get('REDIS_PORT', '6379')),
db=int(os.environ.get('REDIS_DB', '0'))
db=int(os.environ.get('REDIS_DB', '0')),
password=os.environ.get('REDIS_PASSWORD')
)
Binary file added tests/photos/cmyk.tif
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/test_classifier_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def test_style_predict():
assert result[0][0] == 'serene'
assert '{0:.3f}'.format(result[0][1]) == '0.962'

# Check that there is no error when running with non-RGB image
cmyk = str(Path(__file__).parent / 'photos' / 'cmyk.tif')
result = model.predict(cmyk)
assert result == None


def test_face_predict():
from photonix.classifiers.face.model import FaceModel
Expand Down

0 comments on commit 70fec24

Please sign in to comment.