python 识别barcode并提高准确率

其实Python的条码扫描库,一直都有一个很是出名,那就是zbar,但此库虽然牛,却已经停止维护了,如果是python3,则不能使用zbar库了,python2.7还是可以用的。忙活了一下午才发现我的python3.5不能用zbar库,哈哈蓝瘦
1、zbar依赖库安装,python2.x,可以使用zbar,已经停止维护了,python3不能使用。

sudo apt-get install libzbar-dev
pip install zbar
#如果没有安装成功,或者使用过程中报错:No module named 'zbar',尝试下面安装:
pip install zbar-py

2、python3可以使用pyzbar库

pip3 install pyzbar

opencv读图解析结果,并显示

import pyzbar.pyzbar as pyzbar
import cv2
img = cv2.imread("out/3.jpg")
decodedObjects = pyzbar.decode(img)
for barcode in decodedObjects:
    print(barcode)
    (x, y, w, h) = barcode.rect
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 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.6, (0, 0, 125), 2)
cv2.imshow("output.jpg",img)
cv2.waitKey(0)

PIL Image读图解析结果,并显示

from PIL import Image,ImageEnhance,ImageDraw,ImageFont
img=Image.open("out/5.jpg")
decodedObjects = pyzbar.decode(img)
for barcode in decodedObjects:
    print(barcode)
    (x, y, w, h) = barcode.rect
    draw = ImageDraw.ImageDraw(img)  # 用a来表示右侧这段
    print(x, y, w, h)
    draw.rectangle((x, y, x+w, y+h),  outline='red', width=1)  # 在100
    barcodeData = barcode.data.decode("utf-8")
    barcodeType = barcode.type
    text = "{} ({})".format(barcodeData, barcodeType)
    draw.text((x, y-10), text, font=ImageFont.truetype('./QRCode-OpenCV/font/kalimati.ttf', 25), fill= (255,0,0))
img.show()
img.save('out.jpg')

3、cv2.barcode_BarcodeDetector(),目前只支持EAN-13, EAN-8 and UPC-A,OpenCV在V4.5.3版本以上。

import cv2
import numpy as np
import os
bardet = cv2.barcode_BarcodeDetector()
img = cv2.imread("./out/6.jpg")
path='/home/mby/文档/out'
dirs=os.listdir(path)

# 输出所有文件和文件夹
for file in dirs:
    print (file)
    img = cv2.imread(os.path.join(path, file))
    ok, decoded_info, decoded_type, corners = bardet.detectAndDecode(img)
    c=corners.astype(np.int32)
    cv2.polylines(img, c, 1, 255)
    cv2.imshow('',img)
    cv2.waitKey()

通常情况下清晰的图片都是可以的,但工业应用的都是相对模糊一些的图片,效果并不好。有待解决。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值