color-tags / app.py
rrighart's picture
Upload app.py
c0a3e93
raw history blame
No virus
2.11 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from numpy import asarray
model = tf.keras.models.load_model("simple-CNN-model.2022-8-9.hdf5")
def image_predict(img):
"""
Displays dominant colors beyond a given threshold.
img : image input, for ex 'blue-car.jpg'
isize: input image size, for ex. 227
thr: chosen threshold value
"""
thr=0
global model
if model is None:
model = tf.keras.models.load_model("models/simple-CNN-model.2022-8-9.hdf5")
print('model is loaded')
data = np.asarray(img)
print("data is: ", data, type(data))
ndata = np.expand_dims(data, axis=0)
y_prob = model.predict(ndata/255)
print('Yprob:', y_prob)
y_prob.argmax(axis=-1)
print('yprob', y_prob)
colorlabels = ['beige', 'black', 'blue', 'brown', 'gold', 'green', 'grey', 'orange', 'pink', 'purple', 'red', 'silver', 'tan', 'white', 'yellow']
print('color', [sorted(colorlabels)[i] for i in np.where(np.ravel(y_prob)>thr)[0]])
print('values', [np.ravel(y_prob)[i] for i in list(np.where(np.ravel(y_prob)>thr)[0])])
coltags = [sorted(colorlabels)[i] for i in np.where(np.ravel(y_prob)>thr)[0]]
colprob = [np.ravel(y_prob)[i] for i in list(np.where(np.ravel(y_prob)>thr)[0])]
if len(coltags) > 0:
response = []
for i,j in zip(coltags, colprob):
print(i,j)
resp = {}
resp[i] = f"{j}"
response.append(resp)
d = dict(map(dict.popitem, response))
print('the dictionary:', d)
return dict(d)
else:
return str('No label was found')
iface = gr.Interface(
title = "Product color tagging",
description = "App classifying images on different colors",
article = "<p style='text-align: center'><a href='https://www.rrighart.com' target='_blank'>Webpage</a></p>",
fn=image_predict,
inputs=gr.Image(shape=(227,227)),
outputs=gr.Label(),
examples=['shoes1.jpg', 'shoes2.jpg'],
enable_queue=True,
interpretation="default",
debug=True
)
iface.launch()