0% found this document useful (0 votes)
5 views1 page

Yolo 2

The document outlines a Python script that sets up a camera using Picamera2 and runs the YOLOv8 object detection model on captured frames. It displays the annotated frames with detection results and frames per second (FPS) information. The program continues to run until the user presses 'q' to exit.

Uploaded by

alaminuiu07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Yolo 2

The document outlines a Python script that sets up a camera using Picamera2 and runs the YOLOv8 object detection model on captured frames. It displays the annotated frames with detection results and frames per second (FPS) information. The program continues to run until the user presses 'q' to exit.

Uploaded by

alaminuiu07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import cv2

from picamera2 import Picamera2


from ultralytics import YOLO

# Set up the camera with Picam


picam2 = Picamera2()
picam2.preview_configuration.main.size = (1280, 720)
picam2.preview_configuration.main.format = "RGB888"
picam2.preview_configuration.align()
picam2.configure("preview")
picam2.start()

# Load YOLOv8
model = YOLO("yolov8n.pt")

while True:
# Capture a frame from the camera
frame = picam2.capture_array()

# Run YOLO model on the captured frame and store the results
results = model(frame)

# Output the visual detection data, we will draw this on our camera preview
window
annotated_frame = results[0].plot()

# Get inference time


inference_time = results[0].speed['inference']
fps = 1000 / inference_time # Convert to milliseconds
text = f'FPS: {fps:.1f}'

# Define font and position


font = cv2.FONT_HERSHEY_SIMPLEX
text_size = cv2.getTextSize(text, font, 1, 2)[0]
text_x = annotated_frame.shape[1] - text_size[0] - 10 # 10 pixels from the
right
text_y = text_size[1] + 10 # 10 pixels from the top

# Draw the text on the annotated frame


cv2.putText(annotated_frame, text, (text_x, text_y), font, 1, (255, 255, 255),
2, cv2.LINE_AA)

# Display the resulting frame


cv2.imshow("Camera", annotated_frame)

# Exit the program if q is pressed


if cv2.waitKey(1) == ord("q"):
break

# Close all windows


cv2.destroyAllWindows()

You might also like