Skip to content

Tutorials

Scanning Qr Code – Opencv with Python

Access community, courses and source codes
Logo

AI Vision Academy

Access the code of this tutorial, computer vision courses and an exclusive community on AI Vision Academy

  • Access to over 50+ source codes from Pysource.com/blog
  • Dedicated video courses about computer vision
  • Access to an exclusive community of professionals
  • Real-World AI Projects – Get hands-on experience building practical AI Computer Vision solutions with a structured path.
  • Monthly Coaching Calls – get support and any of your questions answered

Subscribe to our newsletter to learn more

We’re going to see today how to scan Qr code using OpenCV.
Below you can see the code read the Qr code from an image or a real-time video.

For this project, we will obviously need our OpenCV library then NumPy and pyzbar. If you need to install pyzbar, I recommend that you go directly to the project page to follow the instructions.

Scanning Qr Code read from an image

Let’s start by importing all the libraries into our python file. As you can see from the code below and in my first example I imported an image.

import cv2
import numpy as np
import pyzbar.pyzbar as pyzbar

image = cv2.imread("pysource_qrcode.png")

This is the qr code I used

Scanning Qr Code

Now I simply take the data with processing through the library and print the results.

decodedObjects = pyzbar.decode(image)
for obj in decodedObjects:
    print("Type:", obj.type)
    print("Data: ", obj.data, "\n")

We just did Scanning Qr Code – Opencv with Python for the single image.

Scanning Qr Code – Opencv with Python from webcam

For Scanning Qr Code – Opencv with Python with the webcam the procedure is similar to that used for the single image. Instead of the image path we use the function cv2.VideoCapture (0).

As we already know the video stream of the image, on OpenCV, is recognized as a sequence of images, so it will be necessary to insert the processing inside a While loop. In the code below everything will be clearer.

cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN

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

    decodedObjects = pyzbar.decode(frame)
    for obj in decodedObjects:
        #print("Data", obj.data)
        cv2.putText(frame, str(obj.data), (50, 50), font, 2,
                    (255, 0, 0), 3)

Conclusion

In this article I explained, in a very simple way, how to decode a qr code with opencv and python. You can download the complete and immediately usable code from the download link.

21 comments

    1. Simply you can add break on the loop of the objects detected.

      for obj in decodedObjects:
      print(“Type:”, obj.type)
      print(“Data: “, obj.data, “\n”)
      break

      1. ERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)
        ERROR: No matching distribution found for opencv-pythoh

        I use latest version (python-3.8.0)

  1. Hi. How can i stop scanning with cammera. I tried break but it fails. Also would like to open scaned qr internet link, but as i said i cant stop the loop of scaning. Help. Please.

  2. decodedObjects = pyzbar.decode(image)
    File “C:\Users\roman\AppData\Roaming\Python\Python35\site-packages\pyzbar\pyzbar.py”, line 181, in decode
    pixels, width, height = _pixel_data(image)
    File “C:\Users\roman\AppData\Roaming\Python\Python35\site-packages\pyzbar\pyzbar.py”, line 147, in _pixel_data
    pixels, width, height = image
    TypeError: ‘NoneType’ object is not iterable

    I have a mistake , when try to scan

  3. FileNotFoundError: Could not find module ‘C:\Users\badri\PycharmProjects\pb1\venv\lib\site-packages\pyzbar\libzbar-64.dll’ (or one of its dependencies). Try using the full path with constructor syntax.

    anyone know’s how to slove this

Join the discussion