File size: 2,147 Bytes
df02b79
9c0d6b6
df02b79
 
3391f09
df02b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3391f09
df02b79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e52d4f
df02b79
 
 
 
 
 
 
 
 
 
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
68
69
70
71
import gradio as gr
import pandas as np
from keras.models import model_from_json
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16, preprocess_input
import heapq

file = open("focusondriving.json", 'r')
model_json2 = file.read()
file.close()
loaded_model = model_from_json(model_json2)
loaded_model.load_weights("focusondriving.h5")

class_dict = {
    'c0': 'hands on the wheel',
    'c1': 'mobile in right hand',
    'c2': 'talking on the phone with right hand',
    'c3': "mobile in left hand",
    'c4': 'talking on the phone with left hand',
    'c5': 'touching at the dash',
    'c6': 'drinking',
    'c7': 'reaching behind',
    'c8': 'touching the head',
    'c9': 'looking to the side'
}

def predict_image(pic):
    img = image.load_img(pic, target_size=(224, 224))
    x = image.img_to_array(img) 
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = loaded_model.predict(x)
    preds = list(preds[0])

    list_desc_order = heapq.nlargest(2, range(len(preds)), key=preds.__getitem__)
    result1 = f'c{list_desc_order[0]}'
    result2 = '-'
    result2_ = 0
    if preds[list_desc_order[1]] > 0.3:
        result2 = f'c{list_desc_order[1]}'
        result2_  = round(preds[list_desc_order[1]], 2)
    txt = f"category {directory} result 1 {result1} {round(preds[list_desc_order[0]],2)} | result2 {result2} {result2_}"
    txt = f"categoria {directory}"
    
    score = round(preds[list_desc_order[0]], 2)*100
    score = int(score)
    txt2 = f"resultado: {class_dict.get(result1)}     probabilidad {score}%"
    return txt2
    
    
iface = gr.Interface(
    predict_image,
    [
        
        gr.inputs.Image(source="upload",type="filepath", label="Imagen")
    ],

    "text",
    
    
    
    interpretation="default",
    title = 'FER - Facial Expression Recognition',
    description = 'Probablemente nos daremos cuenta de que muchas veces se miente cuando se tratan las emociones, ¿pero nuestra cara también miente? https://saturdays.ai/2022/03/16/detectando-emociones-mediante-imagenes-con-inteligencia-artificial/ ',
    
    theme = 'grass'
 )


   
iface.launch()