Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QR Code Detection from Pyzbar with Camera Image

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

enter image description here

[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.

enter image description here

[]

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?

like image 971
Sugihara Avatar asked Apr 28 '18 20:04

Sugihara


People also ask

Can I scan QR code from 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.

Can QR codes be scanned from screenshots?

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.


2 Answers

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.

enter image description here

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.

like image 144
thewaywewere Avatar answered Nov 04 '22 01:11

thewaywewere


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

like image 27
Jaysa Avatar answered Nov 04 '22 01:11

Jaysa