使用深度学习解码条形码和二维码

问题描述 投票:0回答:1

目标:

我有一架无人机,它必须在 3 米距离内检测和解码所有条形码和 QR 码(小或大)。尽管我已经能够在手持式相机上实现相同的效果,但在无人机上却无法实现同样的效果,因为很难保持无人机不动而不移动。

研究完成:

我上网冲浪,找到了 pyimagesearch 的程序,它在检测到的条形码上绘制边界框。该程序由两个文件组成,即,

1.simple_barcode_detection.py: 处理接收到的帧并返回条形码的边界框位置。

2.detect_barcode.py:将输入帧发送到simple_barcode_detection.py并显示返回的帧。注意:(该程序成功在条形码上绘制了边界框)。

我附上了我由此构建的代码的当前状态。

简单_条形码_检测.py:

# import the necessary packages
import numpy as np
import cv2
import imutils
from pyzbar import pyzbar
import imutils
global text, box
def detect(image):
    # convert the image to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # compute the Scharr gradient magnitude representation of the images
    # in both the x and y direction using OpenCV 2.4
    ddepth = cv2.cv.CV_32F if imutils.is_cv2() else cv2.CV_32F
    gradX = cv2.Sobel(gray, ddepth=ddepth, dx=1, dy=0, ksize=-1)
    gradY = cv2.Sobel(gray, ddepth=ddepth, dx=0, dy=1, ksize=-1)
    # subtract the y-gradient from the x-gradient
    gradient = cv2.subtract(gradX, gradY)
    gradient = cv2.convertScaleAbs(gradient)
    # blur and threshold the image
    blurred = cv2.blur(gradient, (9, 9))
    (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)
    # construct a closing kernel and apply it to the thresholded image
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    # perform a series of erosions and dilations
    closed = cv2.erode(closed, None, iterations=4)
    closed = cv2.dilate(closed, None, iterations=4)
    # find the contours in the thresholded image
    cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    # if no contours were found, return None
    if len(cnts) == 0:
        return None
    # otherwise, sort the contours by area and compute the rotated
    # bounding box of the largest contour
    c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
    rect = cv2.minAreaRect(c)
    box = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect)
    box = np.int0(box)
    return box
    # draw a bounding box arounded the detected barcode and display the frame
def process(image,box):
    min_y = int(np.min(box[:,-1]))
    max_y = int(np.max(box[:,-1]))
    min_x = int(np.min(box[:,0]))
    max_x = int(np.max(box[:,0]))
    image = image[min_y:max_y, min_x:max_x]
    barcodes = pyzbar.decode(image)
    # Processing the barcode # 
    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        text = "{} ({})".format(barcodeData, barcodeType)
    return text
 

检测条形码.py

# import the necessary packages
import simple_barcode_detection
from imutils.video import VideoStream
import argparse
import time
import cv2
from pyzbar import pyzbar
import imutils
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
                help="path to the (optional) video file")
args = vars(ap.parse_args())
# if the video path was not supplied, grab the reference to the
# camera
if not args.get("video", False):
    vs = VideoStream(src=0).start()
    time.sleep(2.0)
# otherwise, load the video
else:
    vs = cv2.VideoCapture(args["video"])
# keep looping over the frames
while True:
    frame = vs.read()
    frame = frame[1] if args.get("video", False) else frame
    if frame is None:
        break
    box = simple_barcode_detection.detect(frame)
    if box is not None:
        cv2.putText(frame, text, (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow("Frame", frame)
#    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF
    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
        break
# if we are not using a video file, stop the video file stream
if not args.get("video", False):
    vs.stop()
# otherwise, release the camera pointer
else:
    vs.release()
# close all windows
cv2.destroyAllWindows()

错误:

line 30, in <module> cv2.putText(frame, text, (x, y - 10), NameError: name 'text' is not defined                                                                                                                               

除此之外,我还发现了一篇研究论文,其中使用 yolo 和 pyzbar 来识别和解码条形码。

问题:

  1. 我们是否有可能接受返回的边界框位置并将它们发送到 pyzbar,以便它扫描该区域的条形码?

  2. pyzbar 是如何工作的&我们可以创建一个类似 pyzbar 的库吗?

参考图片:

  1. 无人机无法扫描条形码Drone not able to scan barcode

  2. 无人机无法扫描这种尺寸的条形码

enter image description here

友情链接:

  1. https://www.pyimagesearch.com/2014/12/15/real-time-barcode-detection-video-python-opencv/
  2. https://d1wqtxts1xzle7.cloudfront.net/63796937/rahman-2019-ijais-451835_IJAIS20200701-113597-137d8us-with-cover-page-v2.pdf?过期=1637915838&签名=geCHJHKsaMnDCJMzNAz-OHHjxXSdX~rLtTf-MO0gGuNutOnHl5x33q8-Xiab4haQGnhuE8ektKO4Ah2REj9nebwfwnO3GYxBGRqZgqMsK63375AU QV7YsTjJz9Qwp~OwUa9st2h4a6~ep3eSwvCjCX-DL-g612osElU6ATgdI4DGqqaaat-QuLAmjQqywXTZrTWs0~nLvVeZBLEJRnNbcphtlJPv1yM35Ix2AhiwKhax4X4qCqLR7 Wzt3XR5IaW33X3zSPNoo0QhDLEZrEFG0m-Hi156fsq488mC4n6Udyoi8KNhaUxqQ316b7Zru7XF1z1UaBbGU44O4nuI5AtflA__&密钥对 ID=APKAJLOHF5GGSLRBV4ZA
python opencv barcode yolo
1个回答
0
投票

我也面临同样的问题,请帮助我

© www.soinside.com 2019 - 2024. All rights reserved.