Those are good suggestions. I wanted to add that you can run AI models on the CPU too, just more slowly. The video will run fine at 30fps, though the detections will update at only about 2.5fps - but that may be fine for many applications. For example:Note: You need to install ultralyics for this to run. Unfortunately apt and pip conspire together to make this surprisingly painful:
* Ensure you have the apt OpenCV installed ("sudo apt install python3-opencv").
* Now make a venv, being sure to include --system-site-packages so that you get libcamera bindings.
* The system pip is too old for ultralytics, so after activating your venv, update it with "pip install -U pip".
* Now you can install ultralytics - "pip install ultralytics".
* You will probably find that pip has broken PyQt5 by installing an incompatible version of OpenCV. Installing PyQt5 through pip is in my experience not for the faint-hearted, so the easiest thing is to uninstall the pip version of OpenCV with "pip uninstall opencv-python", and things will fall back to the system one we started with.
* Couldn't be easier, right?
Code:
from ultralytics import YOLOfrom picamera2 import Picamera2, MappedArraymodel = YOLO("yolo11n.pt")results = Nonedef draw_detections(request): last_results = results if last_results: with MappedArray(request, 'main') as m: m.array[...] = last_results.plot(img=m.array)picam2 = Picamera2()config = picam2.create_video_configuration({'size': (1280, 720)}, lores={'format': 'BGR888'})picam2.start(config, show_preview=True)picam2.pre_callback = draw_detectionswhile True: results = model(picam2.capture_array('lores'))[0]
* Ensure you have the apt OpenCV installed ("sudo apt install python3-opencv").
* Now make a venv, being sure to include --system-site-packages so that you get libcamera bindings.
* The system pip is too old for ultralytics, so after activating your venv, update it with "pip install -U pip".
* Now you can install ultralytics - "pip install ultralytics".
* You will probably find that pip has broken PyQt5 by installing an incompatible version of OpenCV. Installing PyQt5 through pip is in my experience not for the faint-hearted, so the easiest thing is to uninstall the pip version of OpenCV with "pip uninstall opencv-python", and things will fall back to the system one we started with.
* Couldn't be easier, right?
Statistics: Posted by therealdavidp — Fri Dec 13, 2024 12:02 pm