A barcode reader is an optical scanner that can read printed barcodes, decode the data contained in the barcode, and send the data to a computer.
In this tutorial, I will guide you on building your own barcode reader using a camera or webcam in Python. This tutorial is very brief and short, even if you’re a beginner you can finish it.
In order to follow through with this tutorial, you need to have the below library installed on your machine
- Opencv (For Image processing )
- pyzbar (For decoding barcodes on images)
- Pillow (Python Imaging Library)
Opencv & Pillow installation
OpenCV and pillow installation are straight forward If you’re in a window just use pip and while those in the Linux Operating system use pip3.
pip install opencv-python pip install pillow
The zbar DLLs are included with the Windows Python wheels. On other operating systems, you will need to install the zbar shared library.
Mac OS X:
brew install zbar
sudo apt-get install libzbar0 pip install pyzbar
pip install pyzbar
Also you need to have sample image with barcode on your project directory,
the sample barcode image you can just download it from cloud.
. ├── app.py └── barcode.jpg 0 directories, 2 files
what is Pyzbar?
As I have described above Pyzbar is a Python library that can be used to read one-dimensional barcodes and QR codes from Python 2 and 3 using the zbar library.
It Works with PIL / Pillow images, OpenCV / numpy ndarrays, and raw bytes and it also decodes locations of barcodes in the image.
Example Usage :
Fo instance Let’s pyzbar library to decode information in the barcode below
>>> from pyzbar import pyzbar >>> from PIL import Image >>> info = pyzbar.decode(Image.open('barcode.jpg')) >>> print(info) [Decoded(data=b'http://bn.m.wikipedia.org', type='QRCODE', rect=Rect(left=101, top=101, width=599, height=599), polygon=[Point(x=101, y=697), Point(x=700, y=700), Point(x=697, y=101), Point(x=102, y=102)])]
As we have seen above in just one line of code we were able to decode bar code information of the image together with its position in the image.
building realtime bar code reader
Now our remaining task is to build a script capable of performing a real-time reading of bar-code using images fetched from camera streams.
Thus where OpenCV comes into play, we will use Opencv to open up a camera or webcam and automatically detect and read bar codes on it.
Specifying camera Index
Below is Opencv Script to Stream from the Camera, If you’re using a camera that came with a Laptop or Computer use Index of 0 on VideoCapture.
If you’re using a webcam try using Index of 1 on the VideoCapture otherwise experiment with other index numbers like 2, 3 and etc.
import cv2 camera = cv2.VideoCapture(0) ret, frame = camera.read() while ret: ret, frame = camera.read() cv2.imshow('Barcode reader', frame) if cv2.waitKey(1) & 0xFF == 27: break camera.release() cv2.destroyAllWindows()
Code Explanation :
camera = cv2.VideoCapture(0) ret, frame = camera.read()
The above code it’s for opening the camera and begin reading frames capture, ret it’s Boolean , It will be false false once the opencv failed to open the camera and true when it does.
cv2.imshow('Barcode reader', frame)
The above line of code is to render the frames fetched from camera
if cv2.waitKey(1) & 0xFF == 27: break
The above two lines of code , break the while loop when you press Esc button on your Keyboard.
The above lines of code are for stopping opencv from fetching camera streams and terminating Opecv Display window.
Building a Function to detect and Read bar codes
Now after we have built a script to perform Real time streaming from camera, Let’s now build a function which will be responsible for detecting bar codes and reading them.
I made the below Function using knowledge we just learned above
def read_barcodes(frame): barcodes = pyzbar.decode(frame) for barcode in barcodes: x, y , w, h = barcode.rect barcode_text = barcode.data.decode('utf-8') print(barcode_text) cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2) return frame
We now have to recombine the read_bar function and the Streaming script in order to read bar codes of frame fetched directly from camera.
Using the above knowledge I then compiled the above code into final code as shown below.
import cv2 from pyzbar import pyzbar def read_barcodes(frame): barcodes = pyzbar.decode(frame) for barcode in barcodes: x, y , w, h = barcode.rect barcode_text = barcode.data.decode('utf-8') print(barcode_text) cv2.rectangle(frame, (x, y),(x+w, y+h), (0, 255, 0), 2) return frame def main(): camera = cv2.VideoCapture(0) ret, frame = camera.read() while ret: ret, frame = camera.read() frame = read_barcodes(frame) cv2.imshow('Barcode reader', frame) if cv2.waitKey(1) & 0xFF == 27: break camera.release() cv2.destroyAllWindows() if __name__ == '__main__': main()
Once you run the below above it will begin rendering Real time camera streams , therefore you can place a paper , book or component with bar code information close to your camera.
and then program will draw rectangle around any detected bar code together with printing to console it’s bar code information.
To get the full code you can check it out on My Github
In case of any comment , suggestions , feedback .. Drop it in the comment box below and I will reply to you to the fast that I can.
I also recommend you to read this ;
- How to convert picture to sound in Python
- Realtime vehicle detection in Python in 5 minutes
- Getting started with image processing using a pillow
- How to detect Edges in a picture using OpenCV Canny algorithm
Don’t forget to subscribe to this blog to stay updated on upcoming tutorials