我正在将自定义 yolov8 对象检测模型与我的网络摄像头一起使用。我能够使用网络摄像头进行检测,但无法从视频源中获取保存的裁剪图像,以检测置信度小于 0.5 的情况。如果我能从社区或个人那里获得任何帮助,我将不胜感激。我的代码如下。
import cv2
import sys
import os
from ultralytics import YOLO
import time
model = YOLO("C:/Users/Graham/Documents/YoloRobo/runs/detect/train5/weights/best.pt") # path to model file
results = model.predict(source="0", show=True,show_conf=True, line_width=2)
time.sleep(2)
print(results)
for *xyxy, conf, cls in results.xyxy[0]:
if conf < 0.5:
image = results[0].path
xyxy = results[0].boxes.xyxy.tolist()
conf = results[0].boxes.conf.tolist()
# Crop detected object
crop_img = img.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
# Generate a file name based on the detected class and confidence
object_name = model.names[int(cls)]
cropped_file_name = f"{object_name}_{conf:.3f}.png"
# Save the cropped image
crop_img.save(cropped_file_name)
print(f"Saved: {cropped_file_name}")
# Inform the user if no objects were found with confidence < 0.5
if len(results.xyxy[0]) == 0:
print("No objects found with confidence less than 0.5.")
k=0
# Close the window when key q is pressed
while k!=113:
# Display the image
cv2.imshow("Window", image)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
代码运行,我没有收到上述错误,但是当我结束代码时,我也收到错误:
Traceback (most recent call last):
Cell In[2], line 16
for *xyxy, conf, cls in results.xyxy[0]:
AttributeError: 'list' object has no attribute 'xyxy'
我尝试更新
OpenCV
并包含特定边界框的代码,同时更改 xyxy
坐标以尝试调用它,但没有任何效果。我想知道延迟捕获裁剪图像是否也有用,但它不会以小于 0.5 的置信度获取裁剪后的边界框。
错误显示作为响应的
results
对象的数据类型为list。您的代码似乎是 GeneralizedYOLOv4 类型的输出,它不等于列表。在上述版本中,您可以直接调用 xyxy 作为对象的属性并执行进一步的操作。
您可以使用
isinstance(results, yolov4.GeneralizedYOLOv4)
检查相同内容
如果 GeneralizedYOLOv4 对象不是响应,则可能是由于输入或模型未正确加载的一些问题,因为这是证明上下文的可选步骤
请参阅最新的文档并检查相应的示例。
您可以使用指定的代码来取回盒子
results = model(img)
for idx, result in enumerate(results):
boxes = result.boxes