Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from keras.models import model_from_json
|
3 |
+
from keras.preprocessing import image
|
4 |
+
import heapq
|
5 |
+
|
6 |
+
file = open("focusondriving.json", 'r')
|
7 |
+
model_json2 = file.read()
|
8 |
+
file.close()
|
9 |
+
loaded_model = model_from_json(model_json2)
|
10 |
+
loaded_model.load_weights("focusondriving.h5")
|
11 |
+
|
12 |
+
class_dict = {
|
13 |
+
'c0': 'hands on the wheel',
|
14 |
+
'c1': 'mobile in right hand',
|
15 |
+
'c2': 'talking on the phone with right hand',
|
16 |
+
'c3': "mobile in left hand",
|
17 |
+
'c4': 'talking on the phone with left hand',
|
18 |
+
'c5': 'touching at the dash',
|
19 |
+
'c6': 'drinking',
|
20 |
+
'c7': 'reaching behind',
|
21 |
+
'c8': 'touching the head',
|
22 |
+
'c9': 'looking to the side'
|
23 |
+
}
|
24 |
+
|
25 |
+
def predict_image(pic):
|
26 |
+
img = image.load_img(pic, target_size=(224, 224))
|
27 |
+
x = image.img_to_array(img)
|
28 |
+
x = np.expand_dims(x, axis=0)
|
29 |
+
x = preprocess_input(x)
|
30 |
+
preds = loaded_model.predict(x)
|
31 |
+
preds = list(preds[0])
|
32 |
+
|
33 |
+
list_desc_order = heapq.nlargest(2, range(len(preds)), key=preds.__getitem__)
|
34 |
+
result1 = f'c{list_desc_order[0]}'
|
35 |
+
result2 = '-'
|
36 |
+
result2_ = 0
|
37 |
+
if preds[list_desc_order[1]] > 0.3:
|
38 |
+
result2 = f'c{list_desc_order[1]}'
|
39 |
+
result2_ = round(preds[list_desc_order[1]], 2)
|
40 |
+
txt = f"category {directory} result 1 {result1} {round(preds[list_desc_order[0]],2)} | result2 {result2} {result2_}"
|
41 |
+
txt = f"categoria {directory}"
|
42 |
+
|
43 |
+
score = round(preds[list_desc_order[0]], 2)*100
|
44 |
+
score = int(score)
|
45 |
+
txt2 = f"resultado: {class_dict.get(result1)} probabilidad {score}%"
|
46 |
+
return txt2
|
47 |
+
|
48 |
+
|
49 |
+
iface = gr.Interface(
|
50 |
+
predict_image,
|
51 |
+
[
|
52 |
+
|
53 |
+
gr.inputs.Image(source="upload",type="filepath", label="Imagen")
|
54 |
+
],
|
55 |
+
|
56 |
+
"text",
|
57 |
+
|
58 |
+
|
59 |
+
interpretation="default",
|
60 |
+
title = 'FER - Facial Expression Recognition',
|
61 |
+
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/ ',
|
62 |
+
|
63 |
+
theme = 'grass'
|
64 |
+
)
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
iface.launch()
|