Update app.py
Browse files
app.py
CHANGED
@@ -34,70 +34,73 @@ torch.hub.download_url_to_file('https://i.pinimg.com/originals/c2/ce/e0/c2cee056
|
|
34 |
model = torch.hub.load('/content/proyecto/yolov9', 'custom', path='/content/proyecto/best.pt', source='local', force_reload=True, autoshape=True)
|
35 |
#model = torch.hub.load('yolov9', 'custom', path='best.pt', source='local', force_reload=True, autoshape=True) # load on CPU
|
36 |
|
37 |
-
def getQuantity(string):
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
[resultado_cantidad_3,resultado_especie_3]]
|
53 |
-
|
54 |
-
return resultado_lista
|
55 |
|
56 |
def listJSON(resultado):
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
def arrayLista(resultado):
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
def yolo(size, iou, conf, im):
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
#------------ Interface-------------
|
102 |
|
103 |
in1 = gr.inputs.Radio(['640', '1280'], label="Tamaño de la imagen", default='640', type='value')
|
@@ -106,7 +109,7 @@ in3 = gr.inputs.Slider(minimum=0, maximum=1, step=0.05, default=0.50, label='Umb
|
|
106 |
in4 = gr.inputs.Image(type='filepath', label="Original Image")
|
107 |
|
108 |
out2 = gr.outputs.Image(type="pil", label="YOLOv9")
|
109 |
-
out3 = gr.outputs.Dataframe(label="Cantidad_especie", headers=['Cantidad','Especie'], type="pandas")
|
110 |
out4 = gr.outputs.JSON(label="JSON")
|
111 |
#-------------- Text-----
|
112 |
title = 'Trampas Barceló'
|
@@ -119,21 +122,21 @@ Sistemas de Desarrollado por Subsecretaría de Modernización del Municipio de V
|
|
119 |
</p>
|
120 |
"""
|
121 |
article ="<p style='text-align: center'><a href='https://docs.google.com/presentation/d/1T5CdcLSzgRe8cQpoi_sPB4U170551NGOrZNykcJD0xU/edit?usp=sharing' target='_blank'>Para mas info, clik para ir al white paper</a></p><p style='text-align: center'><a href='https://drive.google.com/drive/folders/1owACN3HGIMo4zm2GQ_jf-OhGNeBVRS7l?usp=sharing ' target='_blank'>Google Colab Demo</a></p><p style='text-align: center'><a href='https://github.com/Municipalidad-de-Vicente-Lopez/Trampa_Barcelo' target='_blank'>Repo Github</a></p></center></p>"
|
122 |
-
|
123 |
examples = [['640',0.25, 0.5,'ejemplo1.jpg'], ['640',0.25, 0.5,'ejemplo2.jpg']]
|
124 |
|
125 |
-
iface = gr.Interface(yolo,
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
iface.launch(server_name="0.0.0.0", server_port=7860, enable_queue=True, debug=True)
|
138 |
"""For YOLOv5 PyTorch Hub inference with **PIL**, **OpenCV**, **Numpy** or **PyTorch** inputs please see the full [YOLOv5 PyTorch Hub Tutorial](https://github.com/ultralytics/yolov5/issues/36).
|
139 |
## Citation
|
|
|
34 |
model = torch.hub.load('/content/proyecto/yolov9', 'custom', path='/content/proyecto/best.pt', source='local', force_reload=True, autoshape=True)
|
35 |
#model = torch.hub.load('yolov9', 'custom', path='best.pt', source='local', force_reload=True, autoshape=True) # load on CPU
|
36 |
|
37 |
+
def getQuantity(string):
|
38 |
+
contador_raw = ''.join(string.split(" ")[3:])
|
39 |
+
resultado_especie_1 = 'Aedes'
|
40 |
+
resultado_especie_2 = 'Mosquito'
|
41 |
+
resultado_especie_3 = 'Mosca'
|
42 |
+
resultado_cantidad_1 = ''.join(re.findall(r'\d+',''.join(re.findall(r'\d+'+resultado_especie_1, contador_raw))))
|
43 |
+
resultado_cantidad_2 = ''.join(re.findall(r'\d+',''.join(re.findall(r'\d+'+resultado_especie_2, contador_raw))))
|
44 |
+
resultado_cantidad_3 = ''.join(re.findall(r'\d+',''.join(re.findall(r'\d+'+resultado_especie_3, contador_raw))))
|
45 |
+
resultado_cantidad_1 = resultado_cantidad_1 if len(resultado_cantidad_1) > 0 else "0"
|
46 |
+
resultado_cantidad_2 = resultado_cantidad_2 if len(resultado_cantidad_2) > 0 else "0"
|
47 |
+
resultado_cantidad_3 = resultado_cantidad_3 if len(resultado_cantidad_3) > 0 else "0"
|
48 |
+
resultado_lista = [[resultado_cantidad_1,resultado_especie_1],
|
49 |
+
[resultado_cantidad_2,resultado_especie_2],
|
50 |
+
[resultado_cantidad_3,resultado_especie_3]]
|
51 |
+
return resultado_lista
|
|
|
|
|
|
|
52 |
|
53 |
def listJSON(resultado):
|
54 |
+
resultado_lista = getQuantity(resultado)
|
55 |
+
img_name = " ".join(resultado.split(" ")[0:2])
|
56 |
+
img_size = "".join(resultado.split(" ")[2])
|
57 |
+
strlista = ""
|
58 |
+
for resultado_lista, description in resultado_lista:
|
59 |
+
strlista += '{"quantity":"'+resultado_lista+'","description":"'+description+'"},'
|
60 |
+
strlista = strlista[:-1]
|
61 |
+
str_resultado_lista = '{"image":"'+str(img_name)+'","size":"'+str(img_size)+'","detail":['+strlista+']}'
|
62 |
+
json_string = json.loads(str_resultado_lista)
|
63 |
+
return json_string
|
64 |
+
|
65 |
def arrayLista(resultado):
|
66 |
+
resultado_lista = getQuantity(resultado)
|
67 |
+
df = pd.DataFrame(resultado_lista,columns=['Cantidad','Especie'])
|
68 |
+
return df
|
69 |
|
70 |
def yolo(size, iou, conf, im):
|
71 |
+
print(f"1 {im}")
|
72 |
+
im = Image.open(im)
|
73 |
+
|
74 |
+
#print(f"1 {im} {im.size}")
|
75 |
+
'''Wrapper fn for gradio'''
|
76 |
+
g = (int(size) / max(im.size)) # gain
|
77 |
+
#print(f"2 {g}")
|
78 |
+
#im = im.resize((int(x * g) for x in im.size), Image.LANCZOS) # resize with antialiasing
|
79 |
+
new_width, new_height = im.size # Unpack width and height from im.size
|
80 |
+
|
81 |
+
# Calculate new dimensions using gain
|
82 |
+
new_width = int(new_width * g)
|
83 |
+
new_height = int(new_height * g)
|
84 |
+
|
85 |
+
# Resize the image using the calculated dimensions
|
86 |
+
im = im.resize((new_width, new_height), Image.LANCZOS)
|
87 |
+
im = np.asarray(im, dtype=np.float32)
|
88 |
+
|
89 |
+
print(f"3 {type(im)}")
|
90 |
+
model.iou = iou
|
91 |
+
print("4")
|
92 |
+
model.conf = conf
|
93 |
+
print("5")
|
94 |
+
results2 = model(im) # inference
|
95 |
+
print("6")
|
96 |
+
results2.render() # updates results.imgs with boxes and labels
|
97 |
+
print(f"Resultado luego de render: {results2}")
|
98 |
+
|
99 |
+
results_detail = str(results2)
|
100 |
+
lista = listJSON(results_detail)
|
101 |
+
lista2 = arrayLista(results_detail)
|
102 |
+
return Image.fromarray(results2.ims[0]), lista2, lista
|
103 |
+
|
104 |
#------------ Interface-------------
|
105 |
|
106 |
in1 = gr.inputs.Radio(['640', '1280'], label="Tamaño de la imagen", default='640', type='value')
|
|
|
109 |
in4 = gr.inputs.Image(type='filepath', label="Original Image")
|
110 |
|
111 |
out2 = gr.outputs.Image(type="pil", label="YOLOv9")
|
112 |
+
out3 = gr.outputs.Dataframe(label="Cantidad_especie", headers=['Cantidad','Especie'], type="pandas")
|
113 |
out4 = gr.outputs.JSON(label="JSON")
|
114 |
#-------------- Text-----
|
115 |
title = 'Trampas Barceló'
|
|
|
122 |
</p>
|
123 |
"""
|
124 |
article ="<p style='text-align: center'><a href='https://docs.google.com/presentation/d/1T5CdcLSzgRe8cQpoi_sPB4U170551NGOrZNykcJD0xU/edit?usp=sharing' target='_blank'>Para mas info, clik para ir al white paper</a></p><p style='text-align: center'><a href='https://drive.google.com/drive/folders/1owACN3HGIMo4zm2GQ_jf-OhGNeBVRS7l?usp=sharing ' target='_blank'>Google Colab Demo</a></p><p style='text-align: center'><a href='https://github.com/Municipalidad-de-Vicente-Lopez/Trampa_Barcelo' target='_blank'>Repo Github</a></p></center></p>"
|
125 |
+
|
126 |
examples = [['640',0.25, 0.5,'ejemplo1.jpg'], ['640',0.25, 0.5,'ejemplo2.jpg']]
|
127 |
|
128 |
+
iface = gr.Interface(yolo,
|
129 |
+
inputs=[in1, in2, in3, in4],
|
130 |
+
outputs=[out2,out3,out4], title=title,
|
131 |
+
description=description,
|
132 |
+
article=article,
|
133 |
+
examples=examples,
|
134 |
+
analytics_enabled=False,
|
135 |
+
allow_flagging="manual",
|
136 |
+
flagging_options=["Correcto", "Incorrecto", "Casi correcto", "Error", "Otro"],
|
137 |
+
#flagging_callback=hf_writer
|
138 |
+
)
|
139 |
+
|
140 |
iface.launch(server_name="0.0.0.0", server_port=7860, enable_queue=True, debug=True)
|
141 |
"""For YOLOv5 PyTorch Hub inference with **PIL**, **OpenCV**, **Numpy** or **PyTorch** inputs please see the full [YOLOv5 PyTorch Hub Tutorial](https://github.com/ultralytics/yolov5/issues/36).
|
142 |
## Citation
|