File size: 1,984 Bytes
6c307ee
c0a3e93
 
 
99b1d03
88927e5
6c307ee
c0a3e93
6c307ee
c0a3e93
 
 
 
 
 
 
 
 
 
 
88927e5
 
 
 
c0a3e93
99b1d03
c0a3e93
 
99b1d03
c0a3e93
99b1d03
 
 
 
c0a3e93
 
 
 
 
 
 
99b1d03
c0a3e93
99b1d03
c0a3e93
 
99b1d03
c0a3e93
 
 
 
 
 
 
99b1d03
 
c0a3e93
 
88927e5
c0a3e93
 
 
6c307ee
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
import gradio as gr
import tensorflow as tf
import numpy as np
from numpy import asarray
from datetime import datetime
import cv2

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")
    
    #img = img.reshape((None, 227, 227, 3)) 
    
    img = cv2.resize(img, (227, 227))   
    data = np.asarray(img)
        
    ndata = np.expand_dims(data, axis=0)
    y_prob = model.predict(ndata/255)
    #y_prob.argmax(axis=-1)
    
    now = datetime.now()
    print("--------")
    print("data and time: ", now)
            
    colorlabels = ['beige', 'black', 'blue', 'brown', 'gold', 'green', 'grey', 'orange', 'pink', 'purple', 'red', 'silver', 'tan', 'white', 'yellow']
    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] = float(j)
            response.append(resp)
        d = dict(map(dict.popitem, response))
        print('colors: ', d)
               
        return dict(d)

    else:
        return str('No label was found')

iface = gr.Interface(
    title = "Object color tagging",
    description = "App classifying objects 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(width=227, height=227), # shape=(227,227) 
    outputs=gr.Label(),
    examples=['shoes1.jpg', 'shoes2.jpg'],
    )
iface.launch()