uoitlogo
Creating with Raspberry Pi
Raspberry Pie with cables

Scanner using Pi Camera!

    This tutorial will teach you how to create a scanner using your Raspberry Pi camera module.

    The scanner will be used to capture documents, receipts, notes, and similar sources for computer editing and display. Scanned documents will be saved as pdf files on your Pi's desktop!

    This tutorial assumes you have followed the lessons in the Getting Started section and have connected your camera to the Pi as shown in the Interacting With Pi Camera Board section.

    You will also need to install PIL (Python Image Library), OpenCV, and numpy packages!!

Component Quantity Description Where to Buy
Raspberry Pi 1 Canada, USA
SD card with Raspbian image installed 1 Canada, USA
5V USB Power Supply (a laptop or desktop will work for this) 1 - -
USB A micro Cable 1 Canada, USA
Ethernet Cable 1 Canada, USA
Raspberry Pi Camera Board 1 Sparkfun
Pi Cobbler (Not shown in circuit) 1 Adafruit
Breakout Cable (Not shown in circuit) 1 Adafruit
Insulated Wires 5 Kit or M/M Cables
Push Button 1 Canada
10 kΩ Resistor 2 Canada, USA
Red LED 1 Canada

Building The Circuit:

Final Circuit
First, connect the GND to the negative terminal of the breadboard (ground), and the 3V3 (3.3v power supply) to the positive terminal.

Connect the push button to the GPIO pin 17 from one side, and to the positive terminal of the breadboard from the other side. It is necessary to connect the push button across the middle of the breadboard. Connect a 10kΩ resistor in series with the red wire (shown to the left) that is connected to the GPIO pin 17. The other leg of the resistor should be connected to the ground.

An LED will be added to the circuit in order to show whether the capturing process is working or not. Place the LED’s short leg into the ground and the other leg to any row (preferred row #21, to follow up with me). Connect a 10kΩ to that row from one side and to GPIO pin 4 from the other side, using a wire.

Now plug in your Pi using the breakout cable and you are ready to go!!

Source Code:

Our program is composed of two python scripts:
  • Capture script; is to take an image with the camera module
  • Scanner script; is to scan the document that is in the image taken by Capture script

Download the following Resources:

Capture
Scanner
For Capture script, make sure you initialize both the button and LED to the GPIO pins we have connected earlier in this tutorial. Create a folder for this project on the Pi’s desktop. Call the folder Scanner. As shown in the source code, Capture, the captured image will be saved in the folder we have just created. Once the image is taken, you have to pass into the other script, Scanner, for image processing. The following functions were used:
  1. Canny edge detection
  2. Closing image structure
  3. Finding outer contour
  4. Perspective transformation
  5. Adaptive Gaussian thresholding

Some of the syntax is adapted from online open-source code. You can find more details about the description and syntax of each of these functions online here

Testing:

Let’s start testing our project! Are you ready?

Run your Pi:
First Step
Open LXTerminal, type “sudo nano capture.py”, press Enter. Copy the Capture script, Ctrl + X to exit, make sure it is saved before you exit:
Second Step
Do the same thing with Scanner, watch out it is “sudo nano scanner.py” this time.

Now connect your breadboard (the one built earlier) to your Pi using the breakout cable:
Third Step
Connect the camera module to the Pi:
Fourth Step
Run capture script by typing “sudo python capture.py”, press the button, and point the camera to the desired object. Make sure the image is precise and clear, with an object of four distinct corners or else the scanner will not recognize it!

The LED turned on!! Your camera has captured the image.
Scan the document you have just taken a picture of, by running the scanner script “python scanner.py”.

Now you can see the output! The original image and a scanned version of the document.
Here is an example:
Output
Also you should have realized that a pdf version of the scanned document is already saved as image_scan.pdf in your folder.
To view it; open the folder initially created on the desktop, and double-click on the pdf file.
You can now customize the pdf as desired!
Last Step
Thanks for following up all through the tutorial. I hope you have found it useful!