Spaces:
Runtime error
Runtime error
| # -*- coding: utf-8 -*- | |
| """Deploy Barcelo demo.ipynb | |
| Automatically generated by Colaboratory. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1FxaL8DcYgvjPrWfWruSA5hvk3J81zLY9 | |
|  | |
| # Modelo | |
| YOLO es una familia de modelos de detecci贸n de objetos a escala compuesta entrenados en COCO dataset, e incluye una funcionalidad simple para Test Time Augmentation (TTA), model ensembling, hyperparameter evolution, and export to ONNX, CoreML and TFLite. | |
| ## Gradio Inferencia | |
|  | |
| Este Notebook se acelera opcionalmente con un entorno de ejecuci贸n de GPU | |
| ---------------------------------------------------------------------- | |
| YOLOv5 Gradio demo | |
| *Author: Ultralytics LLC and Gradio* | |
| # C贸digo | |
| """ | |
| #!pip install -qr https://raw.githubusercontent.com/ultralytics/yolov5/master/requirements.txt gradio # install dependencies | |
| import os | |
| import re | |
| import json | |
| import pandas as pd | |
| import gradio as gr | |
| import torch | |
| from PIL import Image | |
| # Images | |
| torch.hub.download_url_to_file('https://i.pinimg.com/originals/7f/5e/96/7f5e9657c08aae4bcd8bc8b0dcff720e.jpg', 'ejemplo1.jpg') | |
| torch.hub.download_url_to_file('https://i.pinimg.com/originals/c2/ce/e0/c2cee05624d5477ffcf2d34ca77b47d1.jpg', 'ejemplo2.jpg') | |
| # Model | |
| #model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update | |
| #model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') # local model o google colab | |
| model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True, autoshape=True) # local model o google colab | |
| #model = torch.hub.load('path/to/yolov5', 'custom', path='/content/yolov56.pt', source='local') # local repo | |
| def getQuantity(string): | |
| contador_raw = ''.join(string.split(" ")[3:]) | |
| resultado_especie_1 = 'Aedes' | |
| resultado_especie_2 = 'Mosquito' | |
| resultado_especie_3 = 'Mosca' | |
| resultado_cantidad_1 = ''.join(re.findall(r'\d+',''.join(re.findall(r'\d+'+resultado_especie_1, contador_raw)))) | |
| resultado_cantidad_2 = ''.join(re.findall(r'\d+',''.join(re.findall(r'\d+'+resultado_especie_2, contador_raw)))) | |
| resultado_cantidad_3 = ''.join(re.findall(r'\d+',''.join(re.findall(r'\d+'+resultado_especie_3, contador_raw)))) | |
| resultado_cantidad_1 = resultado_cantidad_1 if len(resultado_cantidad_1) > 0 else "0" | |
| resultado_cantidad_2 = resultado_cantidad_2 if len(resultado_cantidad_2) > 0 else "0" | |
| resultado_cantidad_3 = resultado_cantidad_3 if len(resultado_cantidad_3) > 0 else "0" | |
| resultado_lista = [[resultado_cantidad_1,resultado_especie_1], | |
| [resultado_cantidad_2,resultado_especie_2], | |
| [resultado_cantidad_3,resultado_especie_3]] | |
| return resultado_lista | |
| def listJSON(resultado): | |
| resultado_lista = getQuantity(resultado) | |
| img_name = " ".join(resultado.split(" ")[0:2]) | |
| img_size = "".join(resultado.split(" ")[2]) | |
| strlista = "" | |
| for resultado_lista, description in resultado_lista: | |
| strlista += '{"quantity":"'+resultado_lista+'","description":"'+description+'"},' | |
| strlista = strlista[:-1] | |
| str_resultado_lista = '{"image":"'+str(img_name)+'","size":"'+str(img_size)+'","detail":['+strlista+']}' | |
| json_string = json.loads(str_resultado_lista) | |
| return json_string | |
| def arrayLista(resultado): | |
| resultado_lista = getQuantity(resultado) | |
| df = pd.DataFrame(resultado_lista,columns=['Cantidad','Especie']) | |
| return df | |
| def yolo(size, iou, conf, im): | |
| '''Wrapper fn for gradio''' | |
| g = (int(size) / max(im.size)) # gain | |
| im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize | |
| model.iou = iou | |
| model.conf = conf | |
| results2 = model(im) # inference | |
| results2.render() # updates results.imgs with boxes and labels | |
| results_detail = str(results2) | |
| lista = listJSON(results_detail) | |
| lista2 = arrayLista(results_detail) | |
| return Image.fromarray(results2.ims[0]), lista2, lista | |
| #------------ Interface------------- | |
| in1 = gr.inputs.Radio(['640', '1280'], label="Tama帽o de la imagen", default='640', type='value') | |
| in2 = gr.inputs.Slider(minimum=0, maximum=1, step=0.05, default=0.45, label='NMS IoU threshold') | |
| in3 = gr.inputs.Slider(minimum=0, maximum=1, step=0.05, default=0.50, label='Umbral o threshold') | |
| in4 = gr.inputs.Image(type='pil', label="Original Image") | |
| out2 = gr.outputs.Image(type="pil", label="YOLOv5") | |
| out3 = gr.outputs.Dataframe(label="Descripci贸n", headers=['Cantidad','Especie']) | |
| out4 = gr.outputs.JSON(label="JSON") | |
| #-------------- Text----- | |
| title = 'Trampas Barcel贸' | |
| description = """ | |
| <p> | |
| <center> | |
| Sistemas de Desarrollado por Subsecretar铆a de Innovaci贸n del Municipio de Vicente L贸pez. Advertencia solo usar fotos provenientes de las trampas Barcel贸, no de celular o foto de internet. | |
| <img src="https://www.vicentelopez.gov.ar/assets/images/logo-mvl.png" alt="logo" width="250"/> | |
| </center> | |
| </p> | |
| """ | |
| 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>" | |
| examples = [['640',0.45, 0.75,'ejemplo1.jpg'], ['640',0.45, 0.75,'ejemplo2.jpg']] | |
| iface = gr.Interface(yolo, | |
| inputs=[in1, in2, in3, in4], | |
| outputs=[out2,out3,out4], title=title, | |
| description=description, | |
| article=article, | |
| examples=examples, | |
| theme="huggingface", | |
| analytics_enabled=False | |
| ).launch(debug=True) | |
| iface.launch() | |
| """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). | |
| ## Citation | |
| [](https://zenodo.org/badge/latestdoi/264818686) | |
| """ |