Spaces:
Build error
Build error
added count feature
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
|
4 |
import argparse
|
5 |
import time
|
6 |
from pathlib import Path
|
7 |
|
8 |
-
import cv2
|
9 |
import torch
|
10 |
import torch.backends.cudnn as cudnn
|
11 |
from numpy import random
|
@@ -14,7 +12,6 @@ from models.experimental import attempt_load
|
|
14 |
from utils.datasets import LoadStreams, LoadImages
|
15 |
from utils.general import (
|
16 |
check_img_size,
|
17 |
-
check_imshow,
|
18 |
non_max_suppression,
|
19 |
apply_classifier,
|
20 |
scale_coords,
|
@@ -26,7 +23,6 @@ from utils.plots import plot_one_box
|
|
26 |
from utils.torch_utils import (
|
27 |
select_device,
|
28 |
load_classifier,
|
29 |
-
time_synchronized,
|
30 |
TracedModel,
|
31 |
)
|
32 |
from PIL import Image
|
@@ -50,9 +46,7 @@ def detect(img):
|
|
50 |
parser.add_argument(
|
51 |
"--weights", nargs="+", type=str, default=loaded_model, help="model.pt path(s)"
|
52 |
)
|
53 |
-
parser.add_argument(
|
54 |
-
"--source", type=str, default="Inference/", help="source"
|
55 |
-
) # file/folder, 0 for webcam
|
56 |
parser.add_argument(
|
57 |
"--img-size", type=int, default=640, help="inference size (pixels)"
|
58 |
)
|
@@ -105,11 +99,6 @@ def detect(img):
|
|
105 |
opt.trace,
|
106 |
)
|
107 |
save_img = True # save inference images
|
108 |
-
webcam = (
|
109 |
-
source.isnumeric()
|
110 |
-
or source.endswith(".txt")
|
111 |
-
or source.lower().startswith(("rtsp://", "rtmp://", "http://", "https://"))
|
112 |
-
)
|
113 |
|
114 |
# Directories
|
115 |
save_dir = Path(
|
@@ -144,13 +133,7 @@ def detect(img):
|
|
144 |
).to(device).eval()
|
145 |
|
146 |
# Set Dataloader
|
147 |
-
|
148 |
-
if webcam:
|
149 |
-
view_img = check_imshow()
|
150 |
-
cudnn.benchmark = True # set True to speed up constant image size inference
|
151 |
-
dataset = LoadStreams(source, img_size=imgsz, stride=stride)
|
152 |
-
else:
|
153 |
-
dataset = LoadImages(source, img_size=imgsz, stride=stride)
|
154 |
|
155 |
# Get names and colors
|
156 |
names = model.module.names if hasattr(model, "module") else model.names
|
@@ -170,7 +153,6 @@ def detect(img):
|
|
170 |
img = img.unsqueeze(0)
|
171 |
|
172 |
# Inference
|
173 |
-
t1 = time_synchronized()
|
174 |
pred = model(img, augment=opt.augment)[0]
|
175 |
|
176 |
# Apply NMS
|
@@ -181,7 +163,6 @@ def detect(img):
|
|
181 |
classes=opt.classes,
|
182 |
agnostic=opt.agnostic_nms,
|
183 |
)
|
184 |
-
t2 = time_synchronized()
|
185 |
|
186 |
# Apply Classifier
|
187 |
if classify:
|
@@ -189,17 +170,12 @@ def detect(img):
|
|
189 |
|
190 |
# Process detections
|
191 |
for i, det in enumerate(pred): # detections per image
|
192 |
-
|
193 |
-
p, s, im0, frame = path[i], "%g: " % i, im0s[i].copy(), dataset.count
|
194 |
-
else:
|
195 |
-
p, s, im0, frame = path, "", im0s, getattr(dataset, "frame", 0)
|
196 |
|
197 |
p = Path(p) # to Path
|
198 |
-
save_path = str(save_dir / p.name) # img.jpg
|
199 |
txt_path = str(save_dir / "labels" / p.stem) + (
|
200 |
"" if dataset.mode == "image" else f"_{frame}"
|
201 |
) # img.txt
|
202 |
-
s += "%gx%g " % img.shape[2:] # print string
|
203 |
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
|
204 |
if len(det):
|
205 |
# Rescale boxes from img_size to im0 size
|
@@ -208,7 +184,7 @@ def detect(img):
|
|
208 |
# Print results
|
209 |
for c in det[:, -1].unique():
|
210 |
n = (det[:, -1] == c).sum() # detections per class
|
211 |
-
s += f"{n} {names[int(c)]}{'s' * (n > 1)}
|
212 |
|
213 |
# Write results
|
214 |
for *xyxy, conf, cls in reversed(det):
|
@@ -234,51 +210,15 @@ def detect(img):
|
|
234 |
line_thickness=3,
|
235 |
)
|
236 |
|
237 |
-
# Print time (inference + NMS)
|
238 |
-
# print(f'{s}Done. ({t2 - t1:.3f}s)')
|
239 |
-
|
240 |
-
# Stream results
|
241 |
-
if view_img:
|
242 |
-
cv2.imshow(str(p), im0)
|
243 |
-
cv2.waitKey(1) # 1 millisecond
|
244 |
-
|
245 |
-
# Save results (image with detections)
|
246 |
-
if save_img:
|
247 |
-
if dataset.mode == "image":
|
248 |
-
cv2.imwrite(save_path, im0)
|
249 |
-
else: # 'video' or 'stream'
|
250 |
-
if vid_path != save_path: # new video
|
251 |
-
vid_path = save_path
|
252 |
-
if isinstance(vid_writer, cv2.VideoWriter):
|
253 |
-
vid_writer.release() # release previous video writer
|
254 |
-
if vid_cap: # video
|
255 |
-
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
256 |
-
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
257 |
-
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
258 |
-
else: # stream
|
259 |
-
fps, w, h = 30, im0.shape[1], im0.shape[0]
|
260 |
-
save_path += ".mp4"
|
261 |
-
vid_writer = cv2.VideoWriter(
|
262 |
-
save_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h)
|
263 |
-
)
|
264 |
-
vid_writer.write(im0)
|
265 |
-
|
266 |
-
if save_txt or save_img:
|
267 |
-
s = (
|
268 |
-
f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}"
|
269 |
-
if save_txt
|
270 |
-
else ""
|
271 |
-
)
|
272 |
-
# print(f"Results saved to {save_dir}{s}")
|
273 |
-
|
274 |
print(f"Done. ({time.time() - t0:.3f}s)")
|
275 |
|
276 |
-
return Image.fromarray(im0[:, :, ::-1])
|
277 |
|
278 |
|
279 |
gr.Interface(
|
280 |
-
detect,
|
281 |
-
[gr.Image(type="pil")],
|
282 |
-
gr.Image(type="pil"),
|
283 |
title="Anything Counter",
|
|
|
|
|
|
|
284 |
).launch(debug=True)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
import argparse
|
4 |
import time
|
5 |
from pathlib import Path
|
6 |
|
|
|
7 |
import torch
|
8 |
import torch.backends.cudnn as cudnn
|
9 |
from numpy import random
|
|
|
12 |
from utils.datasets import LoadStreams, LoadImages
|
13 |
from utils.general import (
|
14 |
check_img_size,
|
|
|
15 |
non_max_suppression,
|
16 |
apply_classifier,
|
17 |
scale_coords,
|
|
|
23 |
from utils.torch_utils import (
|
24 |
select_device,
|
25 |
load_classifier,
|
|
|
26 |
TracedModel,
|
27 |
)
|
28 |
from PIL import Image
|
|
|
46 |
parser.add_argument(
|
47 |
"--weights", nargs="+", type=str, default=loaded_model, help="model.pt path(s)"
|
48 |
)
|
49 |
+
parser.add_argument("--source", type=str, default="Inference/", help="source")
|
|
|
|
|
50 |
parser.add_argument(
|
51 |
"--img-size", type=int, default=640, help="inference size (pixels)"
|
52 |
)
|
|
|
99 |
opt.trace,
|
100 |
)
|
101 |
save_img = True # save inference images
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
# Directories
|
104 |
save_dir = Path(
|
|
|
133 |
).to(device).eval()
|
134 |
|
135 |
# Set Dataloader
|
136 |
+
dataset = LoadImages(source, img_size=imgsz, stride=stride)
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
# Get names and colors
|
139 |
names = model.module.names if hasattr(model, "module") else model.names
|
|
|
153 |
img = img.unsqueeze(0)
|
154 |
|
155 |
# Inference
|
|
|
156 |
pred = model(img, augment=opt.augment)[0]
|
157 |
|
158 |
# Apply NMS
|
|
|
163 |
classes=opt.classes,
|
164 |
agnostic=opt.agnostic_nms,
|
165 |
)
|
|
|
166 |
|
167 |
# Apply Classifier
|
168 |
if classify:
|
|
|
170 |
|
171 |
# Process detections
|
172 |
for i, det in enumerate(pred): # detections per image
|
173 |
+
p, s, im0, frame = path, "", im0s, getattr(dataset, "frame", 0)
|
|
|
|
|
|
|
174 |
|
175 |
p = Path(p) # to Path
|
|
|
176 |
txt_path = str(save_dir / "labels" / p.stem) + (
|
177 |
"" if dataset.mode == "image" else f"_{frame}"
|
178 |
) # img.txt
|
|
|
179 |
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
|
180 |
if len(det):
|
181 |
# Rescale boxes from img_size to im0 size
|
|
|
184 |
# Print results
|
185 |
for c in det[:, -1].unique():
|
186 |
n = (det[:, -1] == c).sum() # detections per class
|
187 |
+
s += f"{n} {names[int(c)]}{'s' * (n > 1)} " # add to string
|
188 |
|
189 |
# Write results
|
190 |
for *xyxy, conf, cls in reversed(det):
|
|
|
210 |
line_thickness=3,
|
211 |
)
|
212 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
print(f"Done. ({time.time() - t0:.3f}s)")
|
214 |
|
215 |
+
return [Image.fromarray(im0[:, :, ::-1]), s]
|
216 |
|
217 |
|
218 |
gr.Interface(
|
219 |
+
fn=detect,
|
|
|
|
|
220 |
title="Anything Counter",
|
221 |
+
inputs=gr.Image(type="pil"),
|
222 |
+
outputs=[gr.Image(label="detection", type="pil"), gr.Textbox(label="count")],
|
223 |
+
allow_flagging="manual",
|
224 |
).launch(debug=True)
|