#coding:utf-8 import cv2 from ultralytics import YOLO # 所需加载的模型目录 path = 'models/best.pt' # Load the YOLOv8 model model = YOLO(path) ID = 0 while(ID<10): cap = cv2.VideoCapture(ID) # get a frame ret, frame = cap.read() if ret == False: ID += 1 else: print('摄像头ID:',ID) break # Loop through the video frames while cap.isOpened(): # Read a frame from the video success, frame = cap.read() if success: # Run YOLOv8 inference on the frame results = model(frame) # Visualize the results on the frame annotated_frame = results[0].plot() # Display the annotated frame cv2.imshow("YOLOv8 Inference", annotated_frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord("q"): break else: # Break the loop if the end of the video is reached break # Release the video capture object and close the display window cap.release() cv2.destroyAllWindows()