在OpenCV里实现条码识别

现在条码这么普及,对于条码的识别,很多应用场合都需要使用。在这里就介绍一个比较小的条码识别库,它就是pyzbar,对于一般的条码,它是能识别出来的,如果比较新的格式可能识别不了。可以使用下面的命令来安装它:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyzbar

成功安装之后,就可以用下面的代码进行测试:

#python 3.7.4,opencv4.1
#蔡军生 https://blog.csdn.net/caimouse/article/details/51749579
#[email protected]
#
import numpy as np
import cv2
from matplotlib import pyplot as plt
from pyzbar import pyzbar

#读取图片
img = cv2.imread('barcode4.png')
#条码识别
barcodes = pyzbar.decode(img)

#获取图片所有的条码
for barcode in barcodes:
    (x, y, w, h) = barcode.rect
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    barcodeData = barcode.data.decode("utf-8")
    barcodeType = barcode.type

    text = "{} ({})".format(barcodeData, barcodeType)
    cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
		0.5, (0, 0, 255), 2)

    print("[INFO] Found {} barcode: {

猜你喜欢

转载自blog.csdn.net/caimouse/article/details/103328996
今日推荐