-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOBJECT.py
More file actions
47 lines (37 loc) · 1.32 KB
/
OBJECT.py
File metadata and controls
47 lines (37 loc) · 1.32 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
import cv2
from ultralytics import YOLO
# Load the pretrained YOLOv8 model (YOLOv8n is fastest)
model = YOLO("yolov8n.pt") # You can also use yolov8s.pt or yolov8m.pt
# Initialize webcam (0 = default camera)
cap = cv2.VideoCapture("55546-501516509_tiny.mp4")
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
while True:
ret, frame = cap.read()
if not ret:
break
# Run YOLOv8 detection on the frame
results = model(frame, stream=True)
# Process the results
for r in results:
boxes = r.boxes
for box in boxes:
# Get bounding box coordinates
x1, y1, x2, y2 = map(int, box.xyxy[0])
confidence = float(box.conf[0])
class_id = int(box.cls[0])
class_name = model.names[class_id]
# Draw the bounding box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
label = f"{class_name} {confidence:.2f}"
cv2.putText(frame, label, (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
# Show the frame
cv2.imshow("YOLOv8 Object Detection", frame)
# Exit on 'q' key
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Release resources
cap.release()
cv2.destroyAllWindows()