参考链接:
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()