英文:
How do I work with the result of model.predict in yolov8
问题
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?
英文:
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?
答案1
得分: 1
这里的问题不在你的代码中,而在Ultralytics包内部使用的hydra包中。
它将传递给"source"的""0""视为null值,因此不会获取任何输入并对默认资产进行预测。如果您尝试使用本地图像或Web上的图像,代码将正常工作。
您可以尝试以下解决方法:
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()
所以在这里,我们尝试将图像写入文件,然后对该文件进行推断。
如果您想要保存检测结果,可以尝试以下方法:
inputs = [frame] # 或者如果您有多个图像 [frame1, frame2, 等等]
results = model(inputs) # Results对象列表 -> 使用模型进行推断
if results:
cv2.imwrite(img_path, frame)
for result in results:
boxes = result.boxes # 用于边界框输出的Boxes对象
# 对边界框进行一些处理
英文:
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
答案2
得分: 0
从YOLOv8模型获取结果并可视化
```python
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()
您还可以从下面的代码中获取框、掩码和概率
for result in results:
boxes = result.boxes # 用于边界框输出的框对象
masks = result.masks # 用于分割掩码输出的掩码对象
probs = result.probs # 用于分类输出的类别概率
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论