【发布时间】:2019-10-31 18:46:08
【问题描述】:
我确实知道之前有人问过这个问题,但我仍然无法让它在我的代码上工作......我正试图让我的树莓派成为一个学校项目的二维码讲师,当我运行我的代码时
from pyzbar import pyzbar
import argparse
import cv2
#code
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="chemin de l'image")
args = vars(ap.parse_args())
#load l'image
image=cv2.imread(args["image"])
#trouver lesQR/barcode dans l'image puisles decoder
barcodes=pyzbar.decode(image)
#loop barcode
for barcode in barcodes:
#extraire les box des coins et faire un carre rouge autour du
#barcode reconnu
(x,y,w,h)=barcode.rect
cv2.rectangle(image, (x,y),(x+w,y+h),(0,0,255), 2)
#barcode est un byte donc besoin convertir en string en premier
barcodeData=barcode.data.decode("utf=8")
barcodeType=barcode.type
#dessiner data barcode et ecrire sur image
text="{}({})".format(barcodeData,barcodeType)
cv2.putText(image,text,(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0,0,255),2)
print("[INFO] {} code, contenu: {}".format(barcodeType,barcodeData))
#montrer output
cv2.imshow("Image",image)
cv2.waitKey(0)
但是当我运行“pythonbarcode_scanner_image.py --image test.png”时,我得到的只是一个错误提示
File "barcode_scanner_image.py", line 16, in <module>
barcodes=pyzbar.decode(image)
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/pyzbar/pyzbar.py", line 181, in decode
pixels, width, height = _pixel_data(image)
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/pyzbar/pyzbar.py", line 147, in _pixel_data
pixels, width, height = image
TypeError: Cannot unpack non-iterable NoneType object
请帮忙
【问题讨论】:
-
NoneType意味着你从某个函数中得到None- 可能image是None- 所以你不能做pixels, width, height = None。使用print()检查变量中的值 -
如果您使用错误的文件路径,则
imread()不会显示错误但它会返回None,您必须检查if image:或if image is not None:。尝试使用/full/path/to/image.jpg -
@furas 也不行,我改了代码还是不行
-
你得到相同的错误还是不同的错误?如果不同,那么它可以工作,但您在代码中还有其他错误。
-
如果您遇到同样的错误,请检查您使用
print(image)得到的结果。如果它显示None,那么您仍然有错误的图像路径或文件不是图像。
标签: python python-3.x raspberry-pi zbar