I am having trouble detecting QR code using Pyzbar. Under perfection condition, I am able to detect the QR code using the original png image. However, when I do video capture from a camera, and then save that frame as in image, pyzbar fails to detect the QR code.
For example, this works
[Decoded(data=b'GOAL', type='QRCODE', rect=Rect(left=16, top=16, width=168, height=168))]
But the following does not even after I manually cropped the surroundings to only show the QR code.
[]
For both of the images, I am using
decode(image, scan_locations=True)
I am wondering what do I need to do in order for pyzbar to decode my QR code image?
Fortunately, Google Lens offers a native feature to scan QR codes from images in the gallery or camera roll on both Android and iOS. It comes preinstalled in different forms on almost all Android devices, such as a standalone app, widget, or baked into the Gallery or Camera app.
Open the Google Lens app, and go to the “image” option. Select the screenshot with the QR code. The app will scan the code, and generate its link in seconds. You can select the “only the QR code” option if there is text, or any other element on the image.
Used OpenCV
to threshold the image to black-in-white then pyzbar
is able to decode the QR code.
Firstly, threshold the image with the code below.
from pyzbar import pyzbar
import argparse
import numpy as np
import cv2
image =cv2.imread("QRCode.png")
# thresholds image to white in back then invert it to black in white
# try to just the BGR values of inRange to get the best result
mask = cv2.inRange(image,(0,0,0),(200,200,200))
thresholded = cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
inverted = 255-thresholded # black-in-white
The following is the processed images.
With,
barcodes = pyzbar.decode(inverted)
print (barcodes)
The print out showed the decoded type is QRCODE
and the data is GOAL
.
[Decoded(data='GOAL', type='QRCODE', rect=Rect(left=5, top=13, width=228, height=212),
polygon=[Point(x=5, y=222), Point(x=233, y=225), Point(x=220, y=19), Point(x=13, y=13)])]
Hope this help.
the problem you are facing is due to the fact that you flipped the image before processing
flipping the image after processing with pyzbar will make it align as you would want it to be
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With