Mauricio Guerta commited on
Commit ·
80a7a94
1
Parent(s): 49623aa
adiciona first
Browse files- app.py +79 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import json
|
| 4 |
+
import yolov5
|
| 5 |
+
|
| 6 |
+
# Images
|
| 7 |
+
torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')
|
| 8 |
+
torch.hub.download_url_to_file('https://raw.githubusercontent.com/WongKinYiu/yolov7/main/inference/images/image3.jpg', 'image3.jpg')
|
| 9 |
+
torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt','yolov5s.pt')
|
| 10 |
+
|
| 11 |
+
model_path = "yolov5s.pt" #"yolov5m.pt", "yolov5l.pt", "yolov5x.pt",
|
| 12 |
+
image_size = 640,
|
| 13 |
+
conf_threshold = 0.25,
|
| 14 |
+
iou_threshold = 0.45,
|
| 15 |
+
model = yolov5.load(model_path, device="cpu")
|
| 16 |
+
|
| 17 |
+
def yolov5_inference(
|
| 18 |
+
image: gr.inputs.Image = None,
|
| 19 |
+
|
| 20 |
+
):
|
| 21 |
+
"""
|
| 22 |
+
YOLOv5 inference function
|
| 23 |
+
Args:
|
| 24 |
+
image: Input image
|
| 25 |
+
model_path: Path to the model
|
| 26 |
+
image_size: Image size
|
| 27 |
+
conf_threshold: Confidence threshold
|
| 28 |
+
iou_threshold: IOU threshold
|
| 29 |
+
Returns:
|
| 30 |
+
Rendered image
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
results = model([image], size=image_size)
|
| 34 |
+
tensor = {
|
| 35 |
+
"tensorflow": [
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
if results.pred is not None:
|
| 40 |
+
for i, element in enumerate(results.pred[0]):
|
| 41 |
+
object = {}
|
| 42 |
+
#print (element[0])
|
| 43 |
+
itemclass = round(element[5].item())
|
| 44 |
+
object["classe"] = itemclass
|
| 45 |
+
object["nome"] = results.names[itemclass]
|
| 46 |
+
object["score"] = element[4].item()
|
| 47 |
+
object["x"] = element[0].item()
|
| 48 |
+
object["y"] = element[1].item()
|
| 49 |
+
object["w"] = element[2].item()
|
| 50 |
+
object["h"] = element[3].item()
|
| 51 |
+
tensor["tensorflow"].append(object)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
text = json.dumps(tensor)
|
| 56 |
+
#print (text)
|
| 57 |
+
return text #results.render()[0]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
inputs = [
|
| 61 |
+
gr.inputs.Image(type="pil", label="Input Image"),
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
| 65 |
+
title = "YOLOv5"
|
| 66 |
+
description = "YOLOv5 is a family of object detection models pretrained on COCO dataset. This model is a pip implementation of the original YOLOv5 model."
|
| 67 |
+
|
| 68 |
+
examples = [['zidane.jpg'], ['image3.jpg']]
|
| 69 |
+
demo_app = gr.Interface(
|
| 70 |
+
fn=yolov5_inference,
|
| 71 |
+
inputs=inputs,
|
| 72 |
+
outputs=["text"],
|
| 73 |
+
title=title,
|
| 74 |
+
examples=examples,
|
| 75 |
+
#cache_examples=True,
|
| 76 |
+
#live=True,
|
| 77 |
+
#theme='huggingface',
|
| 78 |
+
)
|
| 79 |
+
demo_app.launch(debug=True, enable_queue=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
yolov5
|