-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_time_detection.py
More file actions
43 lines (33 loc) · 1 KB
/
real_time_detection.py
File metadata and controls
43 lines (33 loc) · 1 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
import cv2
from ultralytics import YOLO
# Load YOLOv8n model
model = YOLO("yolov8n.pt")
# Start webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
while True:
ret, frame = cap.read()
if not ret:
break
# Run detection
results = model(frame, stream=True)
# Draw bounding boxes
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = box.xyxy[0]
conf = float(box.conf[0])
cls = int(box.cls[0])
label = model.names[cls]
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, f"{label} {conf:.2f}",
(int(x1), int(y1) - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 255, 0), 2)
# Display
cv2.imshow("Real-Time Object Detection (YOLOv8n)", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()