【问题标题】:OpenCV QRCodeDetector Out Of Memory even with a small fileOpenCV QRCodeDetector 内存不足,即使文件很小
【发布时间】:2021-12-16 03:29:55
【问题描述】:

所以我正在尝试使用来自this S.O. answer 的代码解码二维码图像。这是修改后的代码:

import cv2

# Name of the QR Code Image file
filename = r"C:\temp\2021-12-14_162414.png"
# read the QRCODE image
image = cv2.imread(filename)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()
# detect and decode
data, vertices_array, binary_qrcode = detector.detectAndDecode(image)
# if there is a QR code
# print the data
if vertices_array is not None:
    print("QRCode data:")
    print(data)
else:
    print("There was some error")

(这是整个程序;我还在试验。)

PNG 文件本身非常小,只有 43 KB 大小,分辨率为 290x290 (24 bpp),包含只是二维码。

但是,我不断收到错误消息:

Traceback (most recent call last):
  File "C:/Repos/tesqr/decod-cv2.py", line 10, in <module>
    data, vertices_array, binary_qrcode = detector.detectAndDecode(image)
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\core\src\alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 54056250000 bytes in function 'cv::OutOfMemoryError'

为什么 alloc.cpp 要求 54 GB 的 RAM ???

我是 OpenCV 新手,所以请帮我解决问题所在。

我正在使用的库是:

$ pip3 freeze | grep opencv
opencv-contrib-python-headless==4.5.4.60

输入图像:

【问题讨论】:

标签: python opencv


【解决方案1】:

简答:

试试WeChatQRCode

长答案:

使用 QRCodeDetector 进行解码存在几个开放内存问题。我希望在未来的版本中它会得到修复。同时你也可以从 cv2 尝试WeChatQRCode

微信二维码包括两个基于 CNN 的模型:物体检测模型和超分辨率模型。对象检测模型用于检测带有边界框的二维码。二维码小时采用超分辨率模型放大。

您的代码已修改:

import cv2

# Name of the QR Code Image file
filename = "2021-12-14_162414.png"
# read the QRCODE image
image = cv2.imread(filename)
# initialize the cv2 QRCode detector
detector =cv2.wechat_qrcode_WeChatQRCode(detector_prototxt_path = "detect.prototxt", detector_caffe_model_path = "detect.caffemodel", super_resolution_prototxt_path = "sr.prototxt", super_resolution_caffe_model_path = "sr.caffemodel")
# detect and decode
data, vertices_array = detector.detectAndDecode(image)
# if there is a QR code
# print the data
if vertices_array is not None:
    print("QRCode data:")
    print(data)
else:
    print("There was some error")

输出:

QRCode data:
('PK\x03\x04\n',)

如您所见,它需要 prototxt 和 caffemodel 文件。你可以找到他们here

【讨论】:

    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    相关资源
    最近更新 更多