-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhubconfCustom.py
More file actions
54 lines (43 loc) · 1.83 KB
/
hubconfCustom.py
File metadata and controls
54 lines (43 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from ultralytics import YOLO
import cv2
import time
import numpy as np
# Configuration
opt = {
"weights": r"C:\Users\mihar\OneDrive\Documents\Deep Leaning Project\ultralytics\yolo11s.pt",
"img-size": 640,
"conf-thres": 0.25,
"device": 'cpu'
}
# Load YOLO model only once
model = YOLO(opt['weights'])
def video_detection(frame, conf=0.25):
start_time = time.time()
# Run inference
results = model(frame, conf=conf, imgsz=opt["img-size"], device=opt["device"])
total_detections = 0
output_frame = frame.copy()
# Process detection results
for result in results:
if result.boxes is not None:
boxes = result.boxes.xyxy.cpu().numpy()
scores = result.boxes.conf.cpu().numpy()
class_ids = result.boxes.cls.cpu().numpy().astype(int)
for i, box in enumerate(boxes):
x1, y1, x2, y2 = map(int, box)
conf_score = scores[i]
cls_id = class_ids[i]
class_name = model.names[cls_id]
label = f"{class_name} {conf_score:.2f}"
color = (0, 255, 0)
cv2.rectangle(output_frame, (x1, y1), (x2, y2), color, 2)
cv2.putText(output_frame, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
total_detections += 1
# Calculate FPS
fps_x = int(1 / (time.time() - start_time + 1e-5)) # Avoid division by zero
cv2.putText(output_frame, f'Total Detected: {total_detections}', (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(output_frame, f'FPS: {fps_x}', (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
return [(output_frame, fps_x, output_frame.shape, total_detections)]