Spaces:
Build error
Build error
Update app.py
Browse filesAdded comments
app.py
CHANGED
@@ -8,27 +8,23 @@ import numpy as np
|
|
8 |
from ultralytics import YOLO
|
9 |
import yolov5
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
'https://www.shutterstock.com/shutterstock/photos/318604739/display_1500/stock-photo-highway-and-container-truck-at-china-318604739.jpg'
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
file = requests.get(url)
|
22 |
-
open(save_name, 'wb').write(file.content)
|
23 |
-
|
24 |
-
for i, url in enumerate(file_urls):
|
25 |
-
download_file(
|
26 |
-
file_urls[i],
|
27 |
-
f"image_{i}.jpg"
|
28 |
-
)
|
29 |
|
30 |
-
#
|
|
|
|
|
|
|
|
|
31 |
|
|
|
32 |
def yolov5_inference(
|
33 |
image: gr.inputs.Image = None,
|
34 |
model_path: gr.inputs.Dropdown = None,
|
@@ -36,18 +32,24 @@ def yolov5_inference(
|
|
36 |
conf_threshold: gr.inputs.Slider = 0.25,
|
37 |
iou_threshold: gr.inputs.Slider = 0.45 ):
|
38 |
|
39 |
-
|
40 |
model = yolov5.load(model_path, device="cpu")
|
|
|
|
|
41 |
model.conf = conf_threshold
|
42 |
model.iou = iou_threshold
|
|
|
|
|
43 |
results = model([image], size=image_size)
|
|
|
|
|
44 |
crops = results.crop(save=False)
|
45 |
img_crops = []
|
46 |
for i in range(len(crops)):
|
47 |
img_crops.append(crops[i]["im"][..., ::-1])
|
48 |
return results.render()[0], img_crops
|
49 |
|
50 |
-
|
51 |
inputs = [
|
52 |
gr.inputs.Image(type="pil", label="Input Image"),
|
53 |
gr.inputs.Dropdown(["Crime_Y5.pt","yolov5s.pt", "yolov5m.pt", "yolov5l.pt", "yolov5x.pt"], label="Model", default = 'Crime_Y5.pt'),
|
@@ -56,15 +58,19 @@ inputs = [
|
|
56 |
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
57 |
]
|
58 |
|
|
|
59 |
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
60 |
outputs_crops = gr.Gallery(label="Object crop")
|
61 |
-
|
|
|
62 |
description = "YOLOv5 is a family of object detection models pretrained on COCO dataset. This model is a pip implementation of the original YOLOv5 model."
|
63 |
|
|
|
64 |
examples = [['1.jpg', 'Crime_Y5.pt', 640, 0.35, 0.45]
|
65 |
,['2.jpg', 'Crime_Y5.pt', 640, 0.35, 0.45]
|
66 |
,['4.jpg', 'Crime_Y5.pt', 640, 0.35, 0.45]]
|
67 |
|
|
|
68 |
demo_app = gr.Interface(
|
69 |
fn=yolov5_inference,
|
70 |
inputs=inputs,
|
|
|
8 |
from ultralytics import YOLO
|
9 |
import yolov5
|
10 |
|
11 |
+
# Image download
|
12 |
+
# file_urls = [
|
13 |
+
# ]
|
|
|
14 |
|
15 |
+
# def download_file(url, save_name):
|
16 |
+
# url = url
|
17 |
+
# if not os.path.exists(save_name):
|
18 |
+
# file = requests.get(url)
|
19 |
+
# open(save_name, 'wb').write(file.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# for i, url in enumerate(file_urls):
|
22 |
+
# download_file(
|
23 |
+
# file_urls[i],
|
24 |
+
# f"image_{i}.jpg"
|
25 |
+
# )
|
26 |
|
27 |
+
# Function for inference
|
28 |
def yolov5_inference(
|
29 |
image: gr.inputs.Image = None,
|
30 |
model_path: gr.inputs.Dropdown = None,
|
|
|
32 |
conf_threshold: gr.inputs.Slider = 0.25,
|
33 |
iou_threshold: gr.inputs.Slider = 0.45 ):
|
34 |
|
35 |
+
# Loading Yolo V5 model
|
36 |
model = yolov5.load(model_path, device="cpu")
|
37 |
+
|
38 |
+
# Setting model configuration
|
39 |
model.conf = conf_threshold
|
40 |
model.iou = iou_threshold
|
41 |
+
|
42 |
+
# Inference
|
43 |
results = model([image], size=image_size)
|
44 |
+
|
45 |
+
# Cropping the predictions
|
46 |
crops = results.crop(save=False)
|
47 |
img_crops = []
|
48 |
for i in range(len(crops)):
|
49 |
img_crops.append(crops[i]["im"][..., ::-1])
|
50 |
return results.render()[0], img_crops
|
51 |
|
52 |
+
# gradio Input
|
53 |
inputs = [
|
54 |
gr.inputs.Image(type="pil", label="Input Image"),
|
55 |
gr.inputs.Dropdown(["Crime_Y5.pt","yolov5s.pt", "yolov5m.pt", "yolov5l.pt", "yolov5x.pt"], label="Model", default = 'Crime_Y5.pt'),
|
|
|
58 |
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
59 |
]
|
60 |
|
61 |
+
# gradio Output
|
62 |
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
63 |
outputs_crops = gr.Gallery(label="Object crop")
|
64 |
+
|
65 |
+
title = "Crime detection using custom trained Yolo model"
|
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 |
+
# gradio examples: "Image", "Model", "Image Size", "Confidence Threshold", "IOU Threshold"
|
69 |
examples = [['1.jpg', 'Crime_Y5.pt', 640, 0.35, 0.45]
|
70 |
,['2.jpg', 'Crime_Y5.pt', 640, 0.35, 0.45]
|
71 |
,['4.jpg', 'Crime_Y5.pt', 640, 0.35, 0.45]]
|
72 |
|
73 |
+
# gradio app launch
|
74 |
demo_app = gr.Interface(
|
75 |
fn=yolov5_inference,
|
76 |
inputs=inputs,
|