This is the official website of the yolo series, which contains detailed training, prediction, and deployment tutorials.
Official website:
https://docs.ultralytics.com/zh/modes/predict/#masks
The prediction of the picture is not shown, this is too simple, the following directly show the video and camera to predict each frame of code.
Here I used cxk video and yolov8 n model for testing, the effect is still relatively good.
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('yolov8n-pose.pt')
# Open the video file
video_path = "807427488-1-208.mp4"
cap = cv2.VideoCapture(video_path)
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
# Resize the frame
frame = cv2.resize(frame, (800, 500))
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()
This code is given at the end of the forecast on the website.
Loading the model to predict the video frame of the camera, the effect is still relatively good. I'm using the N-model which is the least accurate, but the fastest. Of course, there are other models with high accuracy, and you can use other models to make predictions.
import cv2
import time
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('yolov8n-pose.pt')
# Open the webcam
cap = cv2.VideoCapture(0) # 0 represents the default camera device index
# Variables for FPS calculation
prev_time = 0
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Flip the frame horizontally (left-right)
frame = cv2.flip(frame, 1)
# Run YOLOv8 inference on the frame
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Calculate FPS
current_time = time.time()
fps = 1 / (current_time - prev_time)
prev_time = current_time
# Display FPS on the frame
cv2.putText(annotated_frame, f"FPS: {int(fps)}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 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 there is an error reading the frame
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()