File size: 617 Bytes
03efa53
 
 
eb82720
03efa53
eb82720
 
 
 
 
 
 
03efa53
eb82720
 
 
03efa53
eb82720
03efa53
 
 
eb82720
 
 
03efa53
 
eb82720
 
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
from ultralytics import YOLO
import cv2

model = YOLO("best50.pt")
cap = cv2.VideoCapture(0)  # 0 = default webcam

if not cap.isOpened():
    print("Error: Could not open webcam.")
    exit()

print("Press 'q' to quit.")
while True:
    ret, frame = cap.read()
    if not ret:
        print("Failed to grab frame.")
        break

    results = model(frame, conf=0.5, iou=0.4, imgsz=640, augment=False)
    annotated_frame = results[0].plot()

    cv2.imshow("Webcam Detection", annotated_frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
print("Webcam closed.")