Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,66 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from numpy import asarray
|
5 |
|
6 |
+
model = tf.keras.models.load_model("simple-CNN-model.2022-8-9.hdf5")
|
|
|
7 |
|
8 |
+
def image_predict(img):
|
9 |
+
"""
|
10 |
+
Displays dominant colors beyond a given threshold.
|
11 |
+
img : image input, for ex 'blue-car.jpg'
|
12 |
+
isize: input image size, for ex. 227
|
13 |
+
thr: chosen threshold value
|
14 |
+
"""
|
15 |
+
thr=0
|
16 |
+
global model
|
17 |
+
if model is None:
|
18 |
+
model = tf.keras.models.load_model("models/simple-CNN-model.2022-8-9.hdf5")
|
19 |
+
print('model is loaded')
|
20 |
+
|
21 |
+
data = np.asarray(img)
|
22 |
+
print("data is: ", data, type(data))
|
23 |
+
|
24 |
+
ndata = np.expand_dims(data, axis=0)
|
25 |
+
y_prob = model.predict(ndata/255)
|
26 |
+
print('Yprob:', y_prob)
|
27 |
+
y_prob.argmax(axis=-1)
|
28 |
+
print('yprob', y_prob)
|
29 |
+
|
30 |
+
colorlabels = ['beige', 'black', 'blue', 'brown', 'gold', 'green', 'grey', 'orange', 'pink', 'purple', 'red', 'silver', 'tan', 'white', 'yellow']
|
31 |
+
print('color', [sorted(colorlabels)[i] for i in np.where(np.ravel(y_prob)>thr)[0]])
|
32 |
+
print('values', [np.ravel(y_prob)[i] for i in list(np.where(np.ravel(y_prob)>thr)[0])])
|
33 |
+
coltags = [sorted(colorlabels)[i] for i in np.where(np.ravel(y_prob)>thr)[0]]
|
34 |
+
colprob = [np.ravel(y_prob)[i] for i in list(np.where(np.ravel(y_prob)>thr)[0])]
|
35 |
+
|
36 |
+
if len(coltags) > 0:
|
37 |
+
response = []
|
38 |
+
for i,j in zip(coltags, colprob):
|
39 |
+
print(i,j)
|
40 |
+
resp = {}
|
41 |
+
resp[i] = f"{j}"
|
42 |
+
response.append(resp)
|
43 |
+
d = dict(map(dict.popitem, response))
|
44 |
+
print('the dictionary:', d)
|
45 |
+
|
46 |
+
return dict(d)
|
47 |
+
|
48 |
+
else:
|
49 |
+
return str('No label was found')
|
50 |
+
|
51 |
+
iface = gr.Interface(
|
52 |
+
title = "Product color tagging",
|
53 |
+
description = "App classifying images on different colors",
|
54 |
+
article = "<p style='text-align: center'><a href='https://www.rrighart.com' target='_blank'>Webpage</a></p>",
|
55 |
+
fn=image_predict,
|
56 |
+
inputs=gr.Image(shape=(227,227)),
|
57 |
+
outputs=gr.Label(),
|
58 |
+
examples=['shoes1.jpg', 'shoes2.jpg'],
|
59 |
+
enable_queue=True,
|
60 |
+
interpretation="default",
|
61 |
+
debug=True
|
62 |
+
)
|
63 |
iface.launch()
|
64 |
+
|
65 |
+
|
66 |
+
|