How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV

0. First

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

1. QR code generation

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. test.png 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. test2.png

You can change the color and size of these QR codes, so if you are interested, please see Youtube.

2. Barcode generation

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. barcode.png

3. Read QR code and barcode

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'))

4. Reading using OpenCV

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.

5. Reading using OpenCV (cool)

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.

Finally

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

How to generate a QR code and barcode in Python and read it normally or in real time with OpenCV
[Note] How to write QR code and description in the same image with python
How to install OpenCV on Cloud9 and run it in Python
How to make a surveillance camera (Security Camera) with Opencv and Python
How to get the date and time difference in seconds with python
How to stop a program in python until a specific date and time
How to read a CSV file with Python 2/3
How to generate permutations in Python and C ++
How to input a character string in Python and output it as it is or in the opposite direction.
How to read a serial number file in a loop, process it, and graph it
How to convert / restore a string with [] in python
How to measure processing time in Python or Java
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
You can do it in 3 minutes! How to make a moving QR code (GIF)!
Generate QR code in Python
How to generate exponential pulse time series data in python
How to log in to AtCoder with Python and submit automatically
How to interactively draw a machine learning pipeline with scikit-learn and save it in HTML
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
An easy way to view the time taken in Python and a smarter way to improve it
[Python] Generate QR code in memory
How to make a string into an array or an array into a string in Python
How to create a heatmap with an arbitrary domain in Python
Read big endian binary in Python and convert it to ndarray
How to write offline real time Solve E04 problems in Python
Draw a watercolor illusion with edge detection in Python3 and openCV3
How to write offline real time Solve F01 problems with Python
How to build Python and Jupyter execution environment with VS Code
How to calculate "xx time" in one shot with Python timedelta
Note installing modules such as pytorch and opencv with pip in Blender python (2.82a or later)
How to generate a new loggroup in CloudWatch using python within Lambda
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
How to write offline real time I tried to solve E11 with python
[AWS lambda] Deploy including various libraries with lambda (generate a zip with a password and upload it to s3) @ Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
How to put a half-width space before letters and numbers in Python.
How to check opencv version in python
How to make a container name a subdomain and make it accessible in Docker
Read CSV file with Python and convert it to DataFrame as it is
How to format a list of dictionaries (or instances) well in Python
How to read standard input or variable files at the same time like paste command in Python
How to use is and == in Python
[Python] How to save the installed package and install it in a new environment at once Mac environment
How to write offline real time I tried to solve E12 with python
A command to specify a file with a specific name in a directory with find and mv, cp, or gzip it (linux)
Generate an SSID and password, make it a QR code and throw it into Slack.
[Python] How to name table data and output it in csv (to_csv method)
How to drop Google Docs in one folder in a .txt file with python
Try implementing Yubaba with Brainf * ck 512 lines (Generate and execute code in Python)
It takes a long time to shut down in CentOS 7 with LVM configuration.
Difference in writing method to read external source code between Ruby and Python
How to get a list of files in the same directory with python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python