-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObject_detection.py
More file actions
36 lines (27 loc) · 1 KB
/
Copy pathObject_detection.py
File metadata and controls
36 lines (27 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
from ultralytics import YOLO
import cv2
# Load YOLOv8 model
model = YOLO("yolov8n.pt") # You can use "yolov8s.pt" for a larger model
# Open webcam
cap = cv2.VideoCapture(0) # Change to "video.mp4" for video input
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Perform object detection
results = model(frame)
# Display results
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Get box coordinates
confidence = box.conf[0] # Confidence score
label = result.names[int(box.cls[0])] # Get class name
# Draw box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, f"{label} {confidence:.2f}", (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.imshow("YOLOv8 Object Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()