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.")