Spaces:
Running
on
Zero
Running
on
Zero
Delete app.py
#3
by
himmlischerhelmut
- opened
app.py
DELETED
@@ -1,143 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import sahi.utils
|
3 |
-
from sahi import AutoDetectionModel
|
4 |
-
import sahi.predict
|
5 |
-
import sahi.slicing
|
6 |
-
from PIL import Image
|
7 |
-
import numpy
|
8 |
-
|
9 |
-
IMAGE_SIZE = 640
|
10 |
-
|
11 |
-
# Images
|
12 |
-
sahi.utils.file.download_from_url(
|
13 |
-
"https://user-images.githubusercontent.com/34196005/142730935-2ace3999-a47b-49bb-83e0-2bdd509f1c90.jpg",
|
14 |
-
"apple_tree.jpg",
|
15 |
-
)
|
16 |
-
sahi.utils.file.download_from_url(
|
17 |
-
"https://user-images.githubusercontent.com/34196005/142730936-1b397756-52e5-43be-a949-42ec0134d5d8.jpg",
|
18 |
-
"highway.jpg",
|
19 |
-
)
|
20 |
-
|
21 |
-
sahi.utils.file.download_from_url(
|
22 |
-
"https://user-images.githubusercontent.com/34196005/142742871-bf485f84-0355-43a3-be86-96b44e63c3a2.jpg",
|
23 |
-
"highway2.jpg",
|
24 |
-
)
|
25 |
-
|
26 |
-
sahi.utils.file.download_from_url(
|
27 |
-
"https://user-images.githubusercontent.com/34196005/142742872-1fefcc4d-d7e6-4c43-bbb7-6b5982f7e4ba.jpg",
|
28 |
-
"highway3.jpg",
|
29 |
-
)
|
30 |
-
|
31 |
-
|
32 |
-
# Model
|
33 |
-
model = AutoDetectionModel.from_pretrained(
|
34 |
-
model_type="yolov5", model_path="yolov5s6.pt", device="cpu", confidence_threshold=0.5, image_size=IMAGE_SIZE
|
35 |
-
)
|
36 |
-
|
37 |
-
|
38 |
-
def sahi_yolo_inference(
|
39 |
-
image,
|
40 |
-
slice_height=512,
|
41 |
-
slice_width=512,
|
42 |
-
overlap_height_ratio=0.2,
|
43 |
-
overlap_width_ratio=0.2,
|
44 |
-
postprocess_type="NMS",
|
45 |
-
postprocess_match_metric="IOU",
|
46 |
-
postprocess_match_threshold=0.5,
|
47 |
-
postprocess_class_agnostic=False,
|
48 |
-
):
|
49 |
-
|
50 |
-
image_width, image_height = image.size
|
51 |
-
sliced_bboxes = sahi.slicing.get_slice_bboxes(
|
52 |
-
image_height,
|
53 |
-
image_width,
|
54 |
-
slice_height,
|
55 |
-
slice_width,
|
56 |
-
False,
|
57 |
-
overlap_height_ratio,
|
58 |
-
overlap_width_ratio,
|
59 |
-
)
|
60 |
-
if len(sliced_bboxes) > 60:
|
61 |
-
raise ValueError(
|
62 |
-
f"{len(sliced_bboxes)} slices are too much for huggingface spaces, try smaller slice size."
|
63 |
-
)
|
64 |
-
|
65 |
-
# standard inference
|
66 |
-
prediction_result_1 = sahi.predict.get_prediction(
|
67 |
-
image=image, detection_model=model
|
68 |
-
)
|
69 |
-
print(image)
|
70 |
-
visual_result_1 = sahi.utils.cv.visualize_object_predictions(
|
71 |
-
image=numpy.array(image),
|
72 |
-
object_prediction_list=prediction_result_1.object_prediction_list,
|
73 |
-
)
|
74 |
-
output_1 = Image.fromarray(visual_result_1["image"])
|
75 |
-
|
76 |
-
# sliced inference
|
77 |
-
prediction_result_2 = sahi.predict.get_sliced_prediction(
|
78 |
-
image=image,
|
79 |
-
detection_model=model,
|
80 |
-
slice_height=int(slice_height),
|
81 |
-
slice_width=int(slice_width),
|
82 |
-
overlap_height_ratio=overlap_height_ratio,
|
83 |
-
overlap_width_ratio=overlap_width_ratio,
|
84 |
-
postprocess_type=postprocess_type,
|
85 |
-
postprocess_match_metric=postprocess_match_metric,
|
86 |
-
postprocess_match_threshold=postprocess_match_threshold,
|
87 |
-
postprocess_class_agnostic=postprocess_class_agnostic,
|
88 |
-
)
|
89 |
-
visual_result_2 = sahi.utils.cv.visualize_object_predictions(
|
90 |
-
image=numpy.array(image),
|
91 |
-
object_prediction_list=prediction_result_2.object_prediction_list,
|
92 |
-
)
|
93 |
-
|
94 |
-
output_2 = Image.fromarray(visual_result_2["image"])
|
95 |
-
|
96 |
-
return output_1, output_2
|
97 |
-
|
98 |
-
|
99 |
-
inputs = [
|
100 |
-
gr.Image(type="pil", label="Original Image"),
|
101 |
-
gr.Number(default=512, label="slice_height"),
|
102 |
-
gr.Number(default=512, label="slice_width"),
|
103 |
-
gr.Number(default=0.2, label="overlap_height_ratio"),
|
104 |
-
gr.Number(default=0.2, label="overlap_width_ratio"),
|
105 |
-
gr.Dropdown(
|
106 |
-
["NMS", "GREEDYNMM"],
|
107 |
-
type="value",
|
108 |
-
value="NMS",
|
109 |
-
label="postprocess_type",
|
110 |
-
),
|
111 |
-
gr.Dropdown(
|
112 |
-
["IOU", "IOS"], type="value", default="IOU", label="postprocess_type"
|
113 |
-
),
|
114 |
-
gr.Number(default=0.5, label="postprocess_match_threshold"),
|
115 |
-
gr.Checkbox(default=True, label="postprocess_class_agnostic"),
|
116 |
-
]
|
117 |
-
|
118 |
-
outputs = [
|
119 |
-
gr.Image(type="pil", label="YOLOv5s"),
|
120 |
-
gr.Image(type="pil", label="YOLOv5s + SAHI"),
|
121 |
-
]
|
122 |
-
|
123 |
-
title = "Small Object Detection with SAHI + YOLOv5"
|
124 |
-
description = "SAHI + YOLOv5 demo for small object detection. Upload an image or click an example image to use."
|
125 |
-
article = "<p style='text-align: center'>SAHI is a lightweight vision library for performing large scale object detection/ instance segmentation.. <a href='https://github.com/obss/sahi'>SAHI Github</a> | <a href='https://medium.com/codable/sahi-a-vision-library-for-performing-sliced-inference-on-large-images-small-objects-c8b086af3b80'>SAHI Blog</a> | <a href='https://github.com/fcakyon/yolov5-pip'>YOLOv5 Github</a> </p>"
|
126 |
-
examples = [
|
127 |
-
["apple_tree.jpg", 256, 256, 0.2, 0.2, "NMS", "IOU", 0.4, True],
|
128 |
-
["highway.jpg", 256, 256, 0.2, 0.2, "NMS", "IOU", 0.4, True],
|
129 |
-
["highway2.jpg", 512, 512, 0.2, 0.2, "NMS", "IOU", 0.4, True],
|
130 |
-
["highway3.jpg", 512, 512, 0.2, 0.2, "NMS", "IOU", 0.4, True],
|
131 |
-
]
|
132 |
-
|
133 |
-
gr.Interface(
|
134 |
-
sahi_yolo_inference,
|
135 |
-
inputs,
|
136 |
-
outputs,
|
137 |
-
title=title,
|
138 |
-
description=description,
|
139 |
-
article=article,
|
140 |
-
examples=examples,
|
141 |
-
theme="huggingface",
|
142 |
-
cache_examples=True,
|
143 |
-
).launch(debug=True, enable_queue=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|