In the below code how would I have a print message or perform any kind of action really if the class with index 0 for example, is detected in the livestream?
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('yolov8n.pt')
# Open the video file (use 0 for webcam or "test.mp4" for a local file)
video_path = 0
cap = cv2.VideoCapture(video_path)
# Set the desired frame size
frame_width = 1280
frame_height = 720
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Resize the frame to the desired size
frame = cv2.resize(frame, (frame_width, frame_height))
# Run YOLOv8 inference on the frame
results = model(frame, conf=0.80)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
cv2.imshow("Live Feed with AI Object Detection", 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()