forked from Stalin-143/Ai-Basic-programes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYOLOv4.py
More file actions
48 lines (38 loc) · 1.4 KB
/
YOLOv4.py
File metadata and controls
48 lines (38 loc) · 1.4 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
import cv2
import numpy as np
from ultralytics import YOLO
# Load YOLOv8 model
model = YOLO('yolov8n.pt') # You can use other versions like 'yolov8s.pt' or 'yolov8m.pt'
# Initialize webcam (change to video file path if needed)
video_source = 0 # Use 0 for the default webcam
cap = cv2.VideoCapture(video_source)
# Function to display results
def draw_detections(frame, results):
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
conf = box.conf[0] # Confidence score
cls = int(box.cls[0]) # Class ID
label = f"{model.names[cls]} {conf:.2f}"
# Draw bounding box and label
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return frame
print("Starting object detection... Press 'q' to quit.")
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Failed to grab frame. Exiting...")
break
# Run YOLO detection
results = model(frame)
# Draw detections on the frame
frame = draw_detections(frame, results)
# Display the frame
cv2.imshow("Object Detection", frame)
# Exit on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()