A QR code that has become commonplace in everyday life, centered on electronic payments. I want to make it myself and make it recognized I will post the result of trying with Raspberry Pi.
・ Arbitrary QR code generation ・ Code reading + content display using a USB camera It looks like this when it is executed. (https://www.youtube.com/watch?v=xLL4vZN3S7g) Display the contents of the code (which can be generated by yourself). In order to make it easier to understand how you are recognizing, the numerical value increases for each recognition.
environment ・ RasPi4 (It should work with RasPi3) ・ USB camera (Logitech) → Raspi cam is also available.
-Not required if RasPi or OpenCV setup is complete. Try ArUco on Raspberry Pi
-Installation of required packages Do the following in the terminal
pip install --upgrade pip
pip install pillow
pip install pyzbar
pip install qrcode
QRcreate.py
import qrcode
from PIL import Image
#Determine the size
qr = qrcode.QRCode(box_size=5)
#List of types and sizes.
#1 57x57
#2 114x114
#5 285x285
#10 570x570
#Specify the content to be embedded
qr.add_data('Try the QR code close to you')
qr.make()
img_qr = qr.make_image()
img_qr.save('/home/pi/qr_lena_1.png') #The location and name you want to save.
QRdetect.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pyzbar.pyzbar import decode
from PIL import Image
import cv2
import time #For test, not required
#Start video capture
cap = cv2.VideoCapture(1)
i=0 #For test
while True:
#Get images from video capture
ret, frame = cap.read()
#Get size
Height, Width = frame.shape[:2]
img = cv2.resize(frame,(int(Width),int(Height)))
PIL_Image=Image.fromarray(img)
#Image display
cv2.imshow('Qr Test', img)
data = decode(PIL_Image)
#Display if you can read something
if(data != []):
print('Read' ,i)
i=i+1
#print(data[0][0])
print(data[0][0].decode('utf-8', 'ignore'))
#Reception of keyboard input
cv2.waitKey(1)
#Free memory for video capture
cap.release()
#Close all windows
cv2.destroyAllWindows()
It's a little hard to see, but you can see that the number increases with each recognition. If you don't print, it will run faster. (https://www.youtube.com/watch?v=xLL4vZN3S7g)
This is a very helpful page. Detailed explanation (https://note.com/yurufuwa_dev/n/n3442772e67ad) Various generation methods (https://qiita.com/MuAuan/items/7265da5281aa69623a03)
that's all.
Recommended Posts