本项目基于开源项目:https://github.com/THU-MIG/yolov10?tab=readme-ov-file
实现一个简单的实时检测效果。
Start capturing from the webcam
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import cv2
cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480)
while True: ret, img= cap.read() cv2.imshow('Webcam', img)
if cv2.waitKey(1) == ord('q'): break
cap.release() cv2.destroyAllWindows()
|
Complete code — object detection with YOLO and webcam
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
| import cv2 from ultralytics import YOLOv10
image_size = 640 conf_threshold = 0.25 model_path = '/yolov10-main/yolov10b.pt'
model = YOLOv10(model_path)
cap = cv2.VideoCapture(0) cap.set(3, 640) cap.set(4, 480)
while True: ret, img = cap.read() if not ret: break
results = model.predict(source=img, imgsz=image_size, conf=conf_threshold, save=False) annotated_img = results[0].plot() cv2.imshow('YOLOv10 Real Time Dectection', annotated_img) if cv2.waitKey(1) == ord('q'): break
cap.release() cv2.destroyAllWindows()
|
这是一个简单版本,更精美的方法可以用gradio实现。
效果
参考文献
https://dipankarmedh1.medium.com/real-time-object-detection-with-yolo-and-webcam-enhancing-your-computer-vision-skills-861b97c78993