程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+pyzbar recognition QR code bar code

編輯:Python

Preface
How to use pyzbar Quickly identify QR code and bar code

One 、pyzbar What is it? ?
First of all, I need to understand ZBar:ZBar Is an open source software suite , Used from various sources ( Like video streaming 、 Image files and raw intensity sensors ) Read barcode . It supports many popular symbols ( Barcode type ), Include EAN-13/UPC-A、UPC-E、EAN-8、 Code 128、 Code 39、 staggered 2/5 And QR code .

and pyzbar It's through Python2 and 3 Interface , Use ZBar The library reads one-dimensional barcodes and QR code .
Two 、 install pyzbar

  1. Windows
    stay Windows Only need to use in the environment of pip Can be installed :

    pip install pyzbar

3、 ... and 、 Start identifying


import datetime
import time
from pathlib import Path
import numpy as np
import cv2
from pyzbar import pyzbar
def get_qrcode_result(image_input, binary_max=230, binary_step=2):
""" Get the result of QR code :param image_input: Enter picture data :param binary_max: Maximum value of binarization :param binary_step: Binarization step size for each increment :return: pyzbar Predicted results """
# Gray the input image 
if len(image_input.shape) >= 3:
image_input = cv2.cvtColor(image_input, cv2.COLOR_RGB2GRAY) # Convert grayscale 
# Get adaptive threshold 
binary, _ = cv2.threshold(image_input, 0, 255, cv2.THRESH_OTSU)
# Binary incremental detection 
res = []
while (binary < binary_max) and (len(res) == 0):
binary, mat = cv2.threshold(image, binary, 255, cv2.THRESH_BINARY)
res = pyzbar.decode(mat)
binary += binary_step
return res
if __name__ == '__main__':
image_dir = Path(r" Picture path ")
total_image = 0
success_count = 0
for image_file in Path.iterdir(image_dir):
if image_file.suffix not in [".jpg", ".png"]:
# Non picture , Skip the file 
continue
# Use cv2.imdecode You can read the pictures under the Chinese path 
image = cv2.imdecode(np.fromfile(Path(image_dir).joinpath(image_file),
dtype=np.uint8),
cv2.IMREAD_COLOR)
start_time = time.time()
result = get_qrcode_result(image, binary_max=230, binary_step=2)
print(f"Got {
image_file} result: {
result}, "
f"using time : {
datetime.timedelta(seconds=(time.time() - start_time))}")
if len(result) > 0:
success_count += 1
total_image += 1
print(f"total image = {
total_image}, success count = {
success_count}")

Possible problems :
installed ,run, The following error occurred !
FileNotFoundError: Could not find module ‘ Your path \Python\Python38\site-packages\pyzbar\libzbar-64.dll’ (or one of its dependencies). Try using the full path with constructor syntax.


resolvent :
Don't worry , I have stepped on this pit . The reason for this error is that it is not installed Visual C++ The dependent libraries

Download address :Visual C++ Redistributable Packages for Visual Studio 2013

After entering , Pull down to see the download button , Install after downloading !:

Reference resources :
[1] pyzbar install
[2] OSError: [WinError 126] Cannot find the specified module.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved