Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,91 +12,67 @@ os.makedirs("models", exist_ok=True)
|
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
print(f"Using device: {device}")
|
14 |
|
15 |
-
#
|
16 |
model_path = Path("models/yolov5n.pt")
|
17 |
if not model_path.exists():
|
18 |
-
print("Downloading and caching YOLOv5n...")
|
19 |
torch.hub.download_url_to_file("https://github.com/ultralytics/yolov5/releases/download/v6.2/yolov5n.pt", "models/yolov5n.pt")
|
20 |
|
21 |
-
|
22 |
-
model = torch.hub.load("ultralytics/yolov5", "custom", path=str(model_path), autoshape=False).to(device)
|
23 |
|
24 |
# Model optimizations
|
25 |
-
model.conf = 0.5
|
26 |
-
model.iou = 0.45
|
27 |
-
model.classes = None # Detect all classes
|
28 |
-
|
29 |
-
# Precision optimizations
|
30 |
if device.type == "cuda":
|
31 |
-
model.half()
|
32 |
-
torch.backends.cudnn.benchmark = True # Better CUDA performance
|
33 |
else:
|
34 |
model.float()
|
35 |
-
torch.set_num_threads(2)
|
36 |
|
37 |
model.eval()
|
38 |
|
39 |
-
#
|
40 |
-
colors = np.random.rand(len(model.names), 3) * 255
|
41 |
-
|
42 |
-
total_time = 0
|
43 |
-
frame_count = 0
|
44 |
|
45 |
def detect_objects(image):
|
46 |
-
global total_time, frame_count
|
47 |
-
|
48 |
if image is None:
|
49 |
return None
|
50 |
|
51 |
start = time.perf_counter()
|
52 |
|
53 |
-
#
|
54 |
-
input_size = 320 # Reduced from 640
|
55 |
im = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
56 |
-
im = cv2.resize(im, (
|
|
|
|
|
|
|
57 |
|
|
|
58 |
with torch.no_grad():
|
59 |
-
|
60 |
-
im = torch.from_numpy(im).to(device).half().permute(2, 0, 1).unsqueeze(0) / 255
|
61 |
-
else:
|
62 |
-
im = torch.from_numpy(im).to(device).float().permute(2, 0, 1).unsqueeze(0) / 255
|
63 |
-
|
64 |
-
pred = model(im, augment=False)[0]
|
65 |
|
66 |
-
#
|
67 |
pred = pred.float() if device.type == "cpu" else pred.half()
|
68 |
-
pred = non_max_suppression(pred, model.conf, model.iou
|
69 |
|
70 |
-
#
|
71 |
output = image.copy()
|
72 |
-
if pred is not None
|
73 |
-
pred[:, :4] = scale_coords(
|
74 |
for *xyxy, conf, cls in pred:
|
75 |
x1, y1, x2, y2 = map(int, xyxy)
|
76 |
cv2.rectangle(output, (x1, y1), (x2, y2), colors[int(cls)].tolist(), 2)
|
77 |
|
78 |
-
# FPS
|
79 |
-
|
80 |
-
total_time += dt
|
81 |
-
frame_count += 1
|
82 |
-
fps = 1 / dt
|
83 |
-
avg_fps = frame_count / total_time
|
84 |
-
|
85 |
-
# Simplified FPS display
|
86 |
cv2.putText(output, f"FPS: {fps:.1f}", (10, 30),
|
87 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
88 |
|
89 |
return output
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
with gr.Blocks(title="Optimized YOLOv5") as demo:
|
95 |
-
gr.Markdown("# Real-Time YOLOv5 Object Detection")
|
96 |
with gr.Row():
|
97 |
-
input_img = gr.Image(label="Input",
|
98 |
output_img = gr.Image(label="Output")
|
99 |
-
|
100 |
-
input_img.change(fn=detect_objects, inputs=input_img, outputs=output_img)
|
101 |
|
102 |
demo.launch()
|
|
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
print(f"Using device: {device}")
|
14 |
|
15 |
+
# Load YOLOv5n model
|
16 |
model_path = Path("models/yolov5n.pt")
|
17 |
if not model_path.exists():
|
|
|
18 |
torch.hub.download_url_to_file("https://github.com/ultralytics/yolov5/releases/download/v6.2/yolov5n.pt", "models/yolov5n.pt")
|
19 |
|
20 |
+
model = torch.hub.load("ultralytics/yolov5", "custom", path=str(model_path)).to(device)
|
|
|
21 |
|
22 |
# Model optimizations
|
23 |
+
model.conf = 0.5
|
24 |
+
model.iou = 0.45
|
|
|
|
|
|
|
25 |
if device.type == "cuda":
|
26 |
+
model.half()
|
|
|
27 |
else:
|
28 |
model.float()
|
29 |
+
torch.set_num_threads(2)
|
30 |
|
31 |
model.eval()
|
32 |
|
33 |
+
colors = np.random.rand(80, 3) * 255 # COCO classes
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def detect_objects(image):
|
|
|
|
|
36 |
if image is None:
|
37 |
return None
|
38 |
|
39 |
start = time.perf_counter()
|
40 |
|
41 |
+
# Preprocess
|
|
|
42 |
im = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
43 |
+
im = cv2.resize(im, (320, 320))
|
44 |
+
tensor = torch.from_numpy(im).to(device)
|
45 |
+
tensor = tensor.half() if device.type == "cuda" else tensor.float()
|
46 |
+
tensor = tensor.permute(2, 0, 1).unsqueeze(0) / 255
|
47 |
|
48 |
+
# Inference
|
49 |
with torch.no_grad():
|
50 |
+
pred = model(tensor)[0]
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# Post-process
|
53 |
pred = pred.float() if device.type == "cpu" else pred.half()
|
54 |
+
pred = non_max_suppression(pred, model.conf, model.iou)[0]
|
55 |
|
56 |
+
# Visualization
|
57 |
output = image.copy()
|
58 |
+
if pred is not None:
|
59 |
+
pred[:, :4] = scale_coords(tensor.shape[2:], pred[:, :4], image.shape).round()
|
60 |
for *xyxy, conf, cls in pred:
|
61 |
x1, y1, x2, y2 = map(int, xyxy)
|
62 |
cv2.rectangle(output, (x1, y1), (x2, y2), colors[int(cls)].tolist(), 2)
|
63 |
|
64 |
+
# FPS counter
|
65 |
+
fps = 1 / (time.perf_counter() - start)
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
cv2.putText(output, f"FPS: {fps:.1f}", (10, 30),
|
67 |
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
|
68 |
|
69 |
return output
|
70 |
|
71 |
+
with gr.Blocks() as demo:
|
72 |
+
gr.Markdown("# Real-Time Object Detection")
|
|
|
|
|
|
|
73 |
with gr.Row():
|
74 |
+
input_img = gr.Image(label="Input", streaming=True) # Modified webcam handling
|
75 |
output_img = gr.Image(label="Output")
|
76 |
+
input_img.change(detect_objects, input_img, output_img)
|
|
|
77 |
|
78 |
demo.launch()
|