If you want to see how the thing you make this time works, please see here (youtube video).
First, install what you need this time at once.
pip install pyqrcode
pip install pypng
pip install python_barcode
pip install pyzbar
Here, first, generate a QR code that includes character information.
qrcode_barcode.py
import pyqrcode
a = pyqrcode.create(content="Hello From Jetsonnano",error='H')
a.png(file='test.png',scale=6)
When you execute this, the following QR code will be created. Next, generate a QR code containing the url information. This is the QR code that is commonly attached to books.
qrcode_barcode.py
import pyqrcode
b = pyqrcode.QRCode('https://www.youtube.com/channel/UCDYbu9aViDvkubFcwgbbKDA',error='M')
b.png('test2.png',scale=6)
When you do this, the following QR code will be generated.
You can change the color and size of these QR codes, so if you are interested, please see Youtube.
Then generate the barcode. There are many other types of barcodes that can be used in Python besides the one I'm using this time. If you are interested, please check it out.
qrcode_barcode.py
import barcode
from barcode.writer import ImageWriter
d = '938469387183'
a = barcode.get_barcode_class('ean13')
b = a(d,writer=ImageWriter())
c = b.save('barcode')
Doing this will generate a barcode like the one below. Barcodes can also contain characters.
Here, the QR code and barcode are read. Both are read in the same way.
qrcode_barcode.py
from pyzbar.pyzbar import decode
from PIL import Image
d = decode(Image.open('qrcode.png'))
print(d[0].data.decode("utf-8"))
#e = decode(Image.open('barcode.png'))
#print(e[0].data.decode('utf-8'))
Here, when a QR code or barcode is detected using OpenCV, that information is displayed on the screen.
qrcode_barcode.py
from pyzbar.pyzbar import decode
import cv2
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while cap.isOpened():
ret,frame = cap.read()
if ret == True:
d = decode(frame)
if d:
frame = cv2.putText(frame,d[0].data.decode('utf-8'),(10,50),font,1,(0,255,255),2,cv2.LINE_AA)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
When you do this, information such as barcodes read by the camera will be displayed at the top of the screen. However, this method is not very cool, so I will introduce a more cool method next.
When it detects a barcode etc., it encloses it in a red square and displays the information on it.
qrcode_barcode.py
from pyzbar.pyzbar import decode
import cv2
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while cap.isOpened():
ret,frame = cap.read()
if ret == True:
d = decode(frame)
if d:
for barcode in d:
x,y,w,h = barcode.rect
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
barcodeData = barcode.data.decode('utf-8')
frame = cv2.putText(frame,barcodeData,(x,y-10),font,.5,(0,0,255),2,cv2.LINE_AA)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
I could read the barcodes and QR codes I made, but I couldn't read the ones attached to books.
Youtube also explains how to make this simple surveillance camera, so please have a look if you like it. If you have any questions, please use the comment section of the video or the comment section of this article. Also, if you like it, please subscribe to the channel.
Recommended Posts