billiard/test123.py

38 lines
1012 B
Python
Raw Normal View History

2023-10-07 10:38:22 +00:00
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
2024-02-29 08:28:15 +00:00
model = YOLO('best640.pt')
2023-10-07 10:38:22 +00:00
# Open the video file
2024-02-29 08:28:15 +00:00
video_path = "./videos/黑八7.mp4"
2023-10-07 10:38:22 +00:00
cap = cv2.VideoCapture(video_path)
cv2.namedWindow("Video", cv2.WINDOW_NORMAL) # Create a named window
cv2.resizeWindow("Video", 640, 384) # Resize this window
2024-02-29 08:28:15 +00:00
# Loop through the video frameslusalkdjl
2023-10-07 10:38:22 +00:00
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("Video", 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()