File size: 2,114 Bytes
6c307ee
c0a3e93
 
 
6c307ee
c0a3e93
6c307ee
c0a3e93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c307ee
c0a3e93
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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()