0

I have my webcam set up to be the input for my model.predict() function and want to trigger some code if the function detects a certain object. The model.predict() function does not seem to ever terminate when using a webcam however, making this not possible. Just wondering what a solution to this could be.

from ultralytics import YOLO
from ultralytics.yolo.v8.detect.predict import DetectionPredictor
import cv2
print('hi')

model = YOLO("C:/Users/User/Downloads/best.pt")
outs = model.predict(source="0", show=True)

print('hey')
# hi gets printed but not hey

If i include the paramater verbose=true in the predict function, the information I need is printed to the terminal, but I do not know how to access this in a variable to trigger more code. Perhaps multi-threading could help but surely there would be a simpler method?

Lookdatway
  • 17
  • 1
  • 5

2 Answers2

1

The problem is not in your code, the problem is in the hydra package used inside the Ultralytics package.

It is treating "0" passed to "source" as a null value, thus not getting any input and predicts on the default assets. if you tried it with any local image or an image on the web, the code will work normally.

You can try this work around:

model = YOLO("model.pt")
camera = cv2.VideoCapture(0)
img_counter = 0

while True:
    ret, frame = camera.read()

    if not ret:
        print("failed to grab frame")
        break
    cv2.imshow("test", frame)

    k = cv2.waitKey(1)
    if k%256 == 27:
        # ESC pressed
        print("Escape hit, closing...")
        break
    elif k%256 == 32:
        # SPACE pressed
        img_path = "path/opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_path, frame)
        outs = model.predict(img_path)
        img_counter += 1

camera.release()

So what we are doing here, is we are trying to write the image to a file and then infering on that file.

You can try the following if you wanna save on detection:

inputs = [frame]  # or if you have multiple images [frame1, frame2, etc.]
results = model(inputs)  # list of Results objects -> perform inference using the model

if results:
    cv2.imwrite(img_path, frame)
for result in results:
    boxes = result.boxes  # Boxes object for bbox outputs 
# Do something with the bounding boxes
  • This is only triggered when the space bar is pressed right? is there any way to do this when it detects the object? – Lookdatway Apr 05 '23 at 00:52
0

Getting Results from YOLOv8 model and visualizing it

from ultralytics import YOLO
import torch
import cv2 
import numpy as np
import pathlib
import matplotlib.pyplot as plt
img = cv2.imread("BUS.jpg")
model = YOLO("best.pt")
results = model(img)
res_plotted = results[0].plot()

Also you can get boxes, masks and prods from below code

for result in results:
    boxes = result.boxes  # Boxes object for bbox outputs
    masks = result.masks  # Masks object for segmentation masks outputs
    probs = result.probs  # Class probabilities for classification outputs
Jayanth D
  • 16
  • 1