Replies: 1 comment
-
@Jason1-2 You can limit the from ultralytics import YOLO
import RPi.GPIO as GPIO
from PIL import Image
# Set up the GPIO pin for the output signal
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
# Load YOLOv8 model
model = YOLO('best.pt')
# Initialize the predict_generator with source=0
predict_generator = model.predict_generator(source=0, show=True)
# Get the camera frame and detect objects in the frame
for img, pred in zip(predict_generator, range(1)): # Process only 1 frame
results = model(img)
if any(obj in results.names for obj in ["Bottle", "Can", "Cup"]):
# Turn on output signal
GPIO.output(18, GPIO.HIGH)
else:
# Turn off output signal
GPIO.output(18, GPIO.LOW)
break # Exit the loop after processing 1 frame This code will process only one frame from the live camera, detect the objects in the frame, and set the GPIO output accordingly. If you want to process multiple frames (e.g., one frame per second), you can change the Note that I added a |
Beta Was this translation helpful? Give feedback.
-
I need help troubleshooting my python coding for my project. I’m using Thonny for python on a raspberry pi, I am also using ultralytics YOLOv8 to create a model that can detect Bottles, Cans, and Cups. This model is called ‘best.pt’. The code below is the python code I created, and I have successfully detected my model on a Live Camera. You can see a Bottle, if a bottle is in front of the camera, and the same thing happens to Can and Cup. The Frames are displayed on the bottom of the screen, and it shows what the camera is detecting, an example looks like this 0: 480x640 1 Bottle, 2330.8ms. This gets displayed infinitely, but I only need this to be displayed once. I have been working on this code and I realized that the code doesn’t get past the “img = model.predict(source=0,show=True)”, and this is because it displays infinite frames, when it’s done then it can continue to the next line of code, but it can’t since it must display infinity frames. How can I fix this? What is an argument I can apply into the code that would only display one frame at a time?
from ultralytics import YOLO
Import RPi.GPIO as GPIO
from PIL import Image
Set up the GPIO pin for the output signal
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
Load YOLOv8 model
model=YOLO( ‘best.pt’)
Get camera frame
img = model.predict(source=0,show=True)
Detect objects in frame
results = model(img)
if any (obj in results.names for obj in [“Bottle”, “Can”, “Cup”]):
else:
Beta Was this translation helpful? Give feedback.
All reactions