[EN] QRCode Detected!

This article describes how to use OpenCV to find QRCode from an image from a Web Camera connected to the Raspberry Pi via a USB port. The example contains an example of reading the results from a web camera. And exit the program by pressing the ESC key, the QRCode search example and decoding the text within the image and storing the result in the image file.

(Figure. 1)

Equipment

In the experiments, the following devices were used.

  1. Raspberry Pi 3 or 4
  2. USB Web Camera

Instructions that you should know

To use OpenCV you need to run the opencv library. The following commands are used for this article:

  1. camera_object = cv2.VideoCapture( order_of_camera ) For connecting the camera The order of the connected cameras must be specified correctly.
  2. result = camera_object.isOpened( ) Returns the availability status of the camera, if the result is true, the camera is ready otherwise the camera is not ready.
  3. result,image = camera_object.read() Read data from the camera, if the result is true, it means read successfully and store the image.
  4. cv2.imshow( “window_name”, image ) Display the image on the screen.
  5. cv2.destroyAllWindows() Close all windows
  6. result = cv2.waitKey( 1 ) For reading the value from the keyboard and store the code of the keyboard in the result
  7. object = cv2.QRCodeDetector() Create an object for QRCode validation.
  8. text, points = qrCodeDetector.detectAndDecode( image ) Decode the QRCode from the image. If points are none, the QRCode is not found in the image and text, stores the QRCode decoding result as text.
  9. cv2.imwrite( “file_name”, image ) Save the image to an image file. The name of the image must be specified.
  10. camera_object.release() For release the camera

Example Code 1

The example program code24-1 is an example of using camera 1 (#0) to read image data and display the image. The program will end when the user presses the ESC key. Each cycle counts the sequence of display frames as well.

# code24-1
import sys
import cv2
import time
camera = cv2.VideoCapture(0)
if (camera.isOpened() == False):
    print("Can not open camera #0.")
    sys.exit(0)
print("Camera ready")
frameCounter = 0
doAgain = True
while doAgain:
    ret, image = camera.read()
    if ret:
        frameCounter += 1
        print("frame no.{}".format(frameCounter))
        cv2.imshow("Image", image)
        key = cv2.waitKey(1) & 0xFF
        if key == 27: # ESC
            cv2.destroyAllWindows()
            doAgain = False
camera.release()

Example Code 2

The example code24-2 is using code24-1 with improving the QRCode verification in the image and when the QRCode is found in the image, it will be decoded and save to the result.jpg file as shown in Figure 2 and 3.

#code24-2
import sys
import cv2
import time
camera = cv2.VideoCapture(0)
if (camera.isOpened() == False):
    print("Can not open camera #0.")
    sys.exit(0)
print("Camera ready")
doAgain = True
while doAgain:
    ret, image = camera.read()
    if ret:
        qrCodeDetector = cv2.QRCodeDetector()
        text, points = qrCodeDetector.detectAndDecode(image)
        if points is not None:
            print(text)
            cv2.imwrite("./result.jpg",image)
        else:
            print("QR code not detected")
        cv2.imshow("Image", image)
        key = cv2.waitKey(1) & 0xFF
        if key == 27: # ESC
            cv2.destroyAllWindows()
            doAgain = False
camera.release()
(Figure. 2 result when QRCode was detected)
(Figure. 3 result of what code24-2 save when QRCode was detected in the image)

Conclusion

From this article, you will find that using OpenCV to read camera data is very convenient and uncomplicated and there are libraries provided to use directly result in reducing the coding. From the examples in this article, you will be able to program to display images from a web camera connected to the Raspberry Pi3/4 board via USB port or to a PC on a Windows, Linux, or macOS operating system. In example code 2, when creating a QRCode object, it is possible to check the number of QRCodes found in the image and decode the QRCode found to text.

Finally, we hope that the article will be useful for further application and have fun with programming.

(C) 2020, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-09-13