Webcam QR code scanner using OpenCV
In this article, we’re going to see about how to perform QR code scanning using a webcam.
Before starting, You need to know about how this process is going to work. Firstly you need to open your webcam, and you’ve to run your python program to make it ready to scan the QR code. You can take the Qr code picture on your mobile and show the picture in front of your webcam. It correctly identifies the QR code that presents on your screen. And this program redirects you to a link hidden in the QR code.
pip install OpenCV pip install webbrowser ( built in )
Step 1: For creating the qr code scanner you need to install the OpenCV library on your command prompt. First, you need to import the cv2 and browser library.Cv2 is used for scanning the QR code through a webcam and a web browser is used to take the URL into the browser.
Step 2: Next, we need to start the camera for capturing the QR code. For that declare a variable called a cap and in this variable pass the instance cv2.VideoCapture(0). The next process is we need to create a variable called detector and in this variable call the object cv2.QRCodeDetector(). This object is a very helpful one for capturing QR code in real-time.
Step 3: This step is very important you need to create a while loop and in this loop create a variable called an img and this loop will read your webcam screen continuously until this loop breaks
Next, create a variable called data, and this variable to be used to decode the qr code, and if any data present in the qr code image it will break the loop and it opens the link in your browser. So this is the condition that I inserted here.
Finally, call the object cv2.imshow this will produce the output and you’ve to assign the key to break the loop. Here I assigned the key that is called q, when we press the q it will stop the video streaming.
And then you’ve to create the variable, in this variable you need to call the object webbrowser.open( pass the variable a in this object )
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course