python实现二维码识别并制作成exe文件

本文档记录了使用Python的OpenCV和pyzbar库进行二维码检测的过程,以及将程序打包成exe文件时遇到的依赖缺失问题。在打包过程中,由于缺少libiconv.dll和libzbar-64.dll文件导致运行错误,解决方法是将这两个dll文件复制到exe文件同目录下。最终成功运行exe文件,实现了二维码检测功能。
摘要由CSDN通过智能技术生成

参考链接:

python3 + opencv +pyzbar 摄像头检测二维码并获取二维码内容
上面沿伸的:
代码:

import cv2
import pyzbar.pyzbar as pyzbar


def decodeDisplay(image):
    barcodes = pyzbar.decode(image)
    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)
        cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    .5, (0, 0, 125), 2)

        # 向终端打印条形码数据和条形码类型
        print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
    return image


def detect():
    camera = cv2.VideoCapture(0)

    while True:
        # 读取当前帧
        ret, frame = camera.read()
        # 转为灰度图像
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        im = decodeDisplay(gray)

        cv2.waitKey(5)
        cv2.imshow("camera", im)

    camera.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    detect()

识别二维码输出结果

感人这次没有bug,直接是可以输出了。随便拿了一个微雪显示屏的盒子,这个程序可以识别多个二维码,但是要求距离比较近,角度比较正:
在这里插入图片描述
运行栏:
在这里插入图片描述

制作成exe文件

参考链接:用Python工具Pyinstaller制作exe应用程序
报错:

pyinstaller : 无法将“pyinstaller”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。所在位置 行:1

参考链接:添加环境变量重启后运行:

PS D:\cubemos_SDK\SkeletonTracking\samples\python> pyinstaller test.py
The 'typing' package is an obsolete backport of a standard library package and is incompatible with PyInstaller. Please `conda remove typing` then try again.

又报错,根据提示改:

PS D:\cubemos_SDK\SkeletonTracking\samples\python> conda remove typing
Solving environment: \

等太久了,一直卡在Solving environment: \自己pip uninstall typing后:
在这里插入图片描述
出现错误:TypeError: _get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists’
参考链接:找到文件,将参数设置成默认为True,再下载一下PyQt5
然后:
在这里插入图片描述
生成了.spec文件和一个dist文件夹,文件夹里面又一个脚本同名文件夹
(另:参考链接:编辑spec文件发现直接运行命令pyinstaller -F 文件名.py就能生成exe文件)
点开同名文件夹里的exe文件,但是点开闪退。
在生成的.exe文件所在的文件夹中cmd,输入命令 .\文件名.exe看看出啥错:

Traceback (most recent call last):
  File "PyInstaller\loader\pyimod04_ctypes.py", line 53, in __init__
  File "ctypes\__init__.py", line 348, in __init__
OSError: [WinError 126] 找不到指定的模块。

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "pyzbar\zbar_library.py", line 58, in load
  File "pyzbar\zbar_library.py", line 52, in load_objects
  File "pyzbar\zbar_library.py", line 52, in <listcomp>
  File "ctypes\__init__.py", line 426, in LoadLibrary
  File "PyInstaller\loader\pyimod04_ctypes.py", line 55, in __init__
pyimod04_ctypes.PyInstallerImportError: Failed to load dynlib/dll 'libiconv.dll'. Most likely this dynlib/dll was not found when the application was frozen.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "PyInstaller\loader\pyimod04_ctypes.py", line 53, in __init__
  File "ctypes\__init__.py", line 348, in __init__
OSError: [WinError 126] 找不到指定的模块。

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "pyzbar\pyzbar.py", line 7, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
  File "pyzbar\wrapper.py", line 155, in <module>
  File "pyzbar\wrapper.py", line 148, in zbar_function
  File "pyzbar\wrapper.py", line 127, in load_libzbar
  File "pyzbar\zbar_library.py", line 60, in load
  File "pyzbar\zbar_library.py", line 52, in load_objects
  File "pyzbar\zbar_library.py", line 52, in <listcomp>
  File "ctypes\__init__.py", line 426, in LoadLibrary
  File "PyInstaller\loader\pyimod04_ctypes.py", line 55, in __init__
pyimod04_ctypes.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\69445\\AppData\\Local\\Temp\\_MEI94642\\pyzbar\\libiconv.dll'. Most likely this dynlib/dll was not found when the application was frozen.
[1728] Failed to execute script 'test' due to unhandled exception!

os126问题我都习惯了,参考老问题,这次用第一个方法
我是缺少libiconv.dll、libzbar-64.dll文件,把这两个dll复制到py同名文件夹里以后就可以了。
运行exe文件:
在这里插入图片描述
在这里插入图片描述
和在pycharm里是一样的,就是没检测到二维码时会warning。
经验教训:在pycharm里跑好的程序打包可能会缺少dll依赖。

以下代码是6.12更新,输出在彩色图像上:

import cv2
import pyzbar.pyzbar as pyzbar
import time


def decodeDisplay(image_gray,image_original):
    barcodes = pyzbar.decode(image_gray)
    for barcode in barcodes:
        # 提取条形码的边界框的位置
        # 画出图像中条形码的边界框
        (x, y, w, h) = barcode.rect
        cv2.rectangle(image_original, (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(image_original, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    .5, (0, 0, 125), 2)

        # 向终端打印条形码数据和条形码类型
        print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
    return image_original


def detect():
    #camera = cv2.VideoCapture(0)

    while True:
        # 读取当前帧
        #ret, frame = camera.read()
        # 转为灰度图像
        frame=cv2.imread("people+QR.jpg")
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        im = decodeDisplay(gray,frame)

        cv2.waitKey(5)
        cv2.imshow("Detect", im)
        cv2.waitKey(100)

    #camera.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    '''
    start = time.clock()
    frame = cv2.imread("QR.jpg")
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    im = decodeDisplay(gray)

    cv2.waitKey(5)
    cv2.imshow("Detect", im)
    cv2.waitKey(1000)
    print("SUCCESS QR DETECT!")
    cv2.destroyAllWindows()
    end = time.clock()
    print('Running task2 time: %s Seconds' % (end - start))
    '''
    detect()


  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现二维码识别与坐标定位,可以使用 Python 中的 OpenCV 库和 pyzbar 库。这里提供一个简单的示例代码: 首先需要安装 pyzbar 库: ```python pip install pyzbar ``` 然后导入必要的库: ```python import cv2 from pyzbar import pyzbar ``` 接下来,读取图片并识别二维码: ```python # 读取图片 image = cv2.imread('qrcode.jpg') # 转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 找到二维码识别 qrcodes = pyzbar.decode(gray) ``` 此时,`qrcodes` 就是识别出的二维码列表,每个二维码包含了一些信息,比如二维码的类型、内容和位置等。可以通过遍历 `qrcodes` 列表来获取二维码的坐标: ```python for qrcode in qrcodes: # 获取二维码位置 (x, y, w, h) = qrcode.rect # 在原图上绘制矩形 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) ``` 这里使用 `cv2.rectangle` 函数在原图上绘制矩形框,参数分别是原图、左上角坐标、右下角坐标、颜色和线宽。最后可以将绘制好的图片显示出来: ```python # 显示图片 cv2.imshow('QRCodes', image) cv2.waitKey(0) ``` 完整代码如下: ```python import cv2 from pyzbar import pyzbar # 读取图片 image = cv2.imread('qrcode.jpg') # 转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 找到二维码识别 qrcodes = pyzbar.decode(gray) # 遍历二维码并绘制矩形框 for qrcode in qrcodes: # 获取二维码位置 (x, y, w, h) = qrcode.rect # 在原图上绘制矩形 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示图片 cv2.imshow('QRCodes', image) cv2.waitKey(0) ``` 运行代码后,就可以看到识别出的二维码并且已经绘制好了矩形框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值