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.
Equipment
In the experiments, the following devices were used.
Instructions that you should know
To use OpenCV you need to run the opencv library. The following commands are used for this article:
- camera_object = cv2.VideoCapture( order_of_camera ) For connecting the camera The order of the connected cameras must be specified correctly.
- 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.
- result,image = camera_object.read() Read data from the camera, if the result is true, it means read successfully and store the image.
- cv2.imshow( “window_name”, image ) Display the image on the screen.
- cv2.destroyAllWindows() Close all windows
- result = cv2.waitKey( 1 ) For reading the value from the keyboard and store the code of the keyboard in the result
- object = cv2.QRCodeDetector() Create an object for QRCode validation.
- 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.
- cv2.imwrite( “file_name”, image ) Save the image to an image file. The name of the image must be specified.
- 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()
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