opencv 将YOLOv8的结果用于pyzbar

xlpyo6sf  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(275)

我希望将YOLOv8的结果传递给解码函数,以便从中读取条形码。
我的程序代码是:

model = YOLO("yolov8n.pt")

cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    results = model.predict(source=frame, show=True, conf=0.70, stream=True, device=0)
    decode(results.numpy())
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

当我这样做时,我得到以下错误消息:

AttributeError: 'generator' object has no attribute 'numpy'

另外,我想用kraken.binarization.nlbin()预处理帧,这是可能的吗?

jrcvhitl

jrcvhitl1#

如果您阅读了Ultralytics predict的文档,您将看到返回不包含任何图像。
你必须自定义预测器返回原始图像,这样你就可以使用results中的bbox来裁剪图像,然后你可以将裁剪结果传递给decode

import cv2
from ultralytics.yolo.engine.model import YOLO
from pyzbar.pyzbar import decode

    
def on_predict_batch_end(predictor):
    # results -> List[batch_size]
    path, im, im0s, vid_cap, s = predictor.batch
    predictor.results = zip(predictor.results, im0s)
         

model = YOLO("yolov8n.pt")
model.add_callback("on_predict_batch_end", on_predict_batch_end)
results = model.predict(source="0", show=True, stream=True)
for i, (result, im0) in enumerate(results):
    boxes = result.boxes
    for box in boxes:
        xyxy = box.xyxy[0]  # get box coordinates in (top, left, bottom, right) format
        t = int(xyxy[0].item())
        l = int(xyxy[1].item())
        b = int(xyxy[2].item())
        r = int(xyxy[3].item())
        crop_img = im0[l:r, t:b]
        d = decode(crop_img)
        print(d)
        cv2.imshow('YOLO V8 crop', crop_img)

这给了我下面的输出(我的手机屏幕上有一个QR码),我匿名的原因很明显:

0: 480x640 2 persons, 1 cell phone, 9.7ms
[Decoded(data=b'https://wa.me/qr/XXXXXXXXXXXXXX', type='QRCODE', rect=Rect(left=105, top=248, width=90, height=95), polygon=[Point(x=105, y=343), Point(x=193, y=341), Point(x=195, y=251), Point(x=111, y=248)], quality=1, orientation=None)]

相关问题