使用YOLOv8的结果进行pyzbar操作

huangapple go评论86阅读模式
英文:

use the result of YOLOv8 for pyzbar

问题

以下是翻译的代码部分:

我想将来自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()对帧进行预处理这是可能的吗如果可以的话应该如何操作
英文:

I want to pass the result from the YOLOv8 to the decode function so that the barcodes are read from it.

My program code is:

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()

When I do this, I get the following error message:

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

Additionally I want to preprocess the frame with kraken.binarization.nlbin() is this possible, if so how?

答案1

得分: 0

如果您阅读Ultralytics的预测文档,您会发现返回结果中不包含任何图像。

您需要自定义您的预测器以返回原始图像,以便您可以使用results中存在的边界框来裁剪图像。然后,您可以将裁剪后的图像传递给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)]
英文:

If you read the documentation for Ultralytics' predict you will see that return does not contain any image.

You have to customize your predictor to return the original image so that you can use the bboxes present in results in order to crop the image. Then you can pass the crops to 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)  
            

This gives me the following output (I had a QR code on my phone screen) which I anonymized for obvious reasons:

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)]

huangapple
  • 本文由 发表于 2023年2月8日 22:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定