import cv2 from ultralytics import YOLO import re # Load the YOLOv8 model model = YOLO('runs/detect/train6/weights/best.pt') # Open the video file video_path = "your_path" cap = cv2.VideoCapture(video_path) # # Alternativly: use an rtsp-stream as input # cap = cv2.VideoCapture("rtsp://user:password@ip_adress:8554/Streaming/Channels/101") # # Alternativly: use your webcam as input # cap = cv2.VideoCapture(0); # 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) # Count the number of detections in the frame num_detections = len(results[0]) # Print the number of detections in the frame print(f"Number of detections in frame: {num_detections}") # 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()