Skip to content
Related Articles

Related Articles

Improve Article

Webcam QR code scanner using OpenCV

  • Last Updated : 30 May, 2021

In this article, we’re going to see about how to perform QR code scanning using a webcam. 

Webcam QR CODE Scanner

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.

Requirements:

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.

Python3




import cv2
import webbrowser

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. 



Python3




cap = cv2.VideoCapture(0)
# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()

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

Python3




while True:
    _, img = cap.read()

Step 4:

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.

Python3




# detect and decode
   data, bbox, _ = detector.detectAndDecode(img)
   # check if there is a QRCode in the image
   if data:
       a=data
       break

Step 5:

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 )

Python3




cv2.imshow("QRCODEscanner", img)    
    if cv2.waitKey(1) == ord("q"):
        break
  
b=webbrowser.open(str(a))
cap.release()
cv2.destroyAllWindows()

Output :


 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :