Hi guys,
In this blogpost I’ll show you how to make the raspberry pi scan bar codes. So without any delay lets begin .
Parts you will need
- Raspberry pi (running raspbian OS)
- camera module
- Internet connection to the pi
Hoping you have all the parts. Let us first start by connecting the camera module
Connecting The Camera module
It is pretty straight forward to connecting the camera module to the pi. Here are the steps.
- Lift up the camera connectors lock
- Insert the camera module ribbon cable inside
- Push down the lock
Enabling the camera on the pi
- Boot up the pi
- Open up terminal software
- In terminal type the following command sudo raspi-config
A settings dialog box will pop up with a set of options to modify various settings for the pi
- Go to the Camera setting option and enable the camera
- Reboot by typing sudo reboot to make the camera work
Testing the Camera module
If you are using the camera for the first time and want to test it check out the following links
- https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspistill.md for testing in the terminal
- https://www.raspberrypi.org/documentation/usage/camera/python/README.md for testing in python
SimpleCV
SimpleCV is an image processing library. It is an open source software that runs on all the major platforms and it is quite a powerful piece of image processing software. for more info regarding SimpleCV visit http://simplecv.org/ . I shall cover more of the features of SimpleCV in the upcoming projects. But first we need to install SimpleCV on the Pi
Installing SimpleCV on Raspberry pi
- Open up the terminal software
- Type the following command
sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip
these were the dependencies or prerequisites for the SimpleCV library
- Now to download SimpleCV type
sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master
- It will take some time for the software to download
- After it is done downloading open up python and type import SimpleCV
- If you get no errors then you have successfully installed SimpleCV
Getting the raspberry pi camera recognised by SimpleCV software
The raspberry pi camera is not recognised by the SimpleCv module. This was the most difficult problem that i encountered. Luckily I found help online in the form of this post by Xfirm that will install a uv4l driver for the camera. Many thanks!!. just go through that link and get the camera working in SimpleCV.
Zbar
Zbar is the module that will process the barcodes. There is another library you can use called the ZXing library. It can also process barcodes but currently I’m using the Zbar one.
Installing the Zbar library
- Open up the lx terminal software in raspbian OS
- First type sudo apt-get update (if not updated recently)
- After type the following command sudo apt-get install python-zbar
- Open up python and type import zbar, you should get no errors
The final part
- You will need this following program
from SimpleCV import Color,Camera,Display cam = Camera() #starts the camera display = Display() while(display.isNotDone()): img = cam.getImage() #gets image from the camera barcode = img.findBarcode() #finds barcode data from image if(barcode is not None): #if there is some data processed barcode = barcode[0] result = str(barcode.data) print result #prints result of barcode in python shell barcode = [] #reset barcode data to empty set img.save(display) #shows the image on the screen
- Run the program and wait for a few seconds for the program to run i.e. led on the camera to light up
- Now direct the camera towards the Barcode/QR code you want to scan wait for few seconds and youll get the barcode data printed in the python shell
The uv4l driver has a resolution of 640X480. therefore the barcodes need to be of a higher resolution to get them scanned properly. If any of you guys have figured out how to incease resolution of the uv4l driver please let me know in the comments down below.
Also like and share this post so that it reaches out to more people. See you guys next time.
Does this only read QR codes, or will it also read a 2D DataMatrix?
LikeLike
Hey man I really appreciate your tutorial. I will be building upon it for my project. But my only concern is if it will run on a rasberry pi zero w with it’s camera
LikeLike
Hi, has anybody a solution for the zxing library. I want to decode aztec code. Any help would be very appreciated.
LikeLike
Have you resolved this issue?
I installed zxing from here: https://github.com/oostendo/python-zxing
but I cannot import it. I get the following error message:
ImportError: No module named zxing
Any suggestions?
LikeLike
Hi, has anybody a solution for the zxing library. I want to decode aztec code. Any help would be very appreciated
LikeLike
hi,
Thanks for the post. The program looks elegant. I am able to use my camera in SimpleCV via different program but when I use the program listed here, I get the error on
while(display.isNotDone()):
isNOTDone is not a valid attribute of Display is the syntax error that I get.
can you pls let me know how to correct this error?
Thanks,
Mike
LikeLike
Hi. The link of uv4 is off. Can you provide another?
Because my SimpleCV isn’t recognizing my camera. It’s kinda urgent.
LikeLike
check out this link
http://www.linux-projects.org/uv4l/installation/
LikeLike
How to close the simple CV window after barcode is scanned
LikeLike
I cant find neither
LikeLike
Is there anyway to determine the format of the barcode that just got read? I.e To find out if it was a QR code?
LikeLike
I can see with the camera, but it isn’t scanning the barcode or QR code. I opened python shell , import SimpleCV , import zbar, no errors. I then open bash and run the program, barcode.py.
LikeLike
All working well, except when I try to scan a Book Barcode it only returns the ISBN10 instead of the ISBN13 version.
I’ve tried the windows installer of ZBAR and it correctly decodes to ISBN13.
Odd.
It’s a shame there isn’t more to go on in the python wrapper. 😦
LikeLike
I have the problem while installing simpleCV.The download is not completed and always fails 99%.I don’t know why.
LikeLike
Try ‘apt-get update’ or ‘apt-get upgrade’ command and then try to install simple cv
LikeLike
*sudo apt-get update
LikeLike
i still have the error coming up even after the os update.I am using raspbian stretch.
LikeLike
I am trying to implement a solution using the new RPi camera to scan regular barcodes. I tried your code but while it can successfully scan QR codes, it cant seem to pick up these regular barcodes.
Can you help me at all here? Thanks
LikeLike
Barcodes do get scanned. Same method. But i found qr codes getting scanned much easily. I think the problem can be solved by using a better resolution camera
LikeLike
(y)
LikeLike
When I run the coding its give an error saying that “No module named SimpleCV”. I’ve downloaded the SimpleCV using the links given, can you explain why I get such error? Tq 🙂
LikeLike
Try installing simplecv again if that helps. Also check if you have the prerequisites installed. Try ‘import numpy’ etc.. In python. I would like to know Which os are you using?
LikeLike
I’ve try to install the SimpleCV and the sgvwrite and its gives no error when I’m trying the import SimpleCV command. And then when I’m trying to run the code, it gives an error said
ERROR:
Traceback (most recent call last):
File “QR.py”, line 8, in
img = cam.getImage() #gets image from the camera
File “/usr/local/lib/python2.7/dist-packages/SimpleCV/Camera.py”, line 618, in getImage
cv.GrabFrame(self.capture)
TypeError: Expected CvCapture for argument ‘capture’
what does it mean? And I’m using the Noobs wheezy.
LikeLike
So the simplecv library is installed. But I have no idea why you’re getting that error. Did you try some simple programs like getting camera live feed etc using simplecv. I’m also using the noobs wheezy ver on the pi.
LikeLike
I also have the same problem. When i try to run the code and when i try to import SimpleCV in python shell. Even when i try to import zbar it shows “No module named zbar.” I know for sure the libraries are installed because i can see the physical folder in the explorer and because i tied “import numpy” and “import opencv” and it doesn’t return any errors. Did you find a solution for that?
LikeLike
the resolution in uv4l can be changed via command line or web interface – read the manuals!
LikeLiked by 1 person
Thanks @strunz. will see if that can make it work!
LikeLike
Hey, when I run the given program after doing all the necessary steps, I get the error message that says so :
lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system /run/user/109/gvfs
Output information may be incomplete.
Fatal Python error: (pygame parachute) Segmentation Fault
Aborted
Any idea how it can be fixed? Thank you in advance!
LikeLike