Have you ever got worried when you've not been able to find a bill or post-it note that you really needed? Well with this Raspberry Pi Document Scanner now you won't have to! All your notes, receipts and documents will now be a click away safely stored in your Dropbox folder.
In this Instructable you'll learn how to make a Raspberry Pi Document Scanner that automatically uploads a copy of your document in a Dropbox folder. The files will be stored with the day and date on when the document was scanned. Have a look at the video walk-through/demo of the project linked above.
Now lets get started !
Step 1: Parts List and Software Requirement
The following parts will be needed for this project :
- Raspberry Pi Model B (or upwards)
- Raspberry Pi Camera Module.
- Memory Card with Raspbian Jessie.
- WiFi Adapter or Ethernet Connection.
- Document or Receipt to be Scanned.
- Some Acrylic sheet to make a stand for the camera module. (optional)
If you plan on working on the Raspberry Pi itself and not through SSH you would need the following materials:
- Monitor or TV screen(with HDMI or VGA input).
- USB keyboard and mouse.
Apart from this you would need the following software packages installed on your Raspberry Pi as a prerequisite:
- Raspbian Jessie (download link)
- OpenCV 3 installed on the Raspberry Pi (Guide to Download and Setup below.)
- PuTTY for SSH control of the Raspberry Pi (optional) (download link)
- WinSCP for remotely transferring files to the Raspberry Pi (optional) (download link)
Downloading and building OpenCV 3 on your Raspberry Pi is a relatively easy but lengthy process and will most likely take up a day (especially for older models). I used the tutorial from Pyimagesearch in order to build OpenCV 3 on the Raspberry Pi. The tutorial has an in depth Video guide as well as a detailed step by step guide which is very easy to follow. Here are the links for both of them :
The version of OpenCV 3 used at the time of writing this Instructable is OpenCV 3.1.0.
Step 2: Enable the Camera Module!
You will first need to enable the Raspberry Pi Camera Module in order to use the raspistill command.
First enter the following command in the terminal:
then go to the Enable Camera option and click on the Enable option in order to access the camera.
The Raspberry Pi Camera module slots into the CSI port on the Raspberry Pi. The metal contacts on the Camera Module ribbon should face away from the Ethernet port. (see the image attached)
Step 3: Installing Python Packages
Assuming you've had a clean and successful install of OpenCV 3 we can move on to installing the Python modules required on the Raspberry Pi.
The following python modules will be required:
We will install these using the pip install command. Before installinig any of these packages you must first get into your OpenCV virtual environment on the Rapsberry Pi.
In order to do this first load your profile by opening the command terminal and entering the following command:
After your profile has loaded start the OpenCV virtual environment by entering the following command:
You'll be in the OpenCV environment once you see (cv)pi@raspberry:~$ as the prompt in the command terminal.(Please refer to the screenshots above.)
In order to install numpy module use the following command:
pip install numpy
2. Installing Scipy
In order to install Scipy run the following command:
pip install scipy
If this throws up some errors you can alternatively use :
sudo apt-get install python-scipy
You will now have the Scipy module installed into your virtual environment.
3. Installing scikit-image
In order to install scikit-image module enter the following command in the terminal:
pip install scikit-image
This process may take a while in order to install on the Raspberry Pi.
You should now have the three modules installed on your OpenCV virtual environment on the Raspberry Pi. Please have a look at the screenshots attached above for a better understanding of the installation procedure.
On to the next step....
Step 4: Setting Up the Dropbox Uploader!
The Dropbox uploader script we will use is included in the folder containing the code. In order to set the dropbox account you will need to move the dropbox_uploader.sh file to the /home/pi directory on the Raspberry Pi.
I referred to this tutorial while writing the steps to setup the Dropbox Uploader.
You can also manually download the script using the following command:
curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh
and then giving the execution permission for the script using:
chmod +x dropbox_uploader.sh
The first time you run dropbox_uploader.sh , you'll be guided through a wizard in order to configure access to your Dropbox. This configuration will be stored in ~/.dropbox_uploader.
Now run the script using ./dropbox_uploader.sh
It will ask you for an APP key and and App secret. In order to get these use the following steps:
First visit https://www.dropbox.com/developers/apps and login to your dropbox account. Then click the "Create App" button.
Then choose the "Dropbox API" option followed by the "App folder" option. After this name your folder where the scanned images will be stored.
Enter the App Key and App Secret provided into the dropbox_upload wizard in the command terminal and confirm.
You will then be given a confirmation URL in the the command terminal which you need to copy and paste in a browser inorder to activate the application.
Your Dropbox folder should now be setup.
Step 5: Main Code and Final Installation Cecks
Now on to the main code..
I have uploaded all the files required to a Dropbox folder. In order to
First download the "document-scanner" folder from this link and unzip the contents, and transfer them to your Raspberry Pi /home/pi directry using WinSCP if you are using the Pi through SSH.
Or directly download the document-scanner.zip file on to the Raspberry Pi and run:
unzip document-scanner.zip to extract the contents.
Then on your Raspberry Pi copy your dropbox_uploader.sh file into the "document-scanner" folder using either the Raspberrry Pi desktop or the command terminal. Your "document-scanner" folder should look like the one above in the picture.
You can also verify the contents of the "document-scanner" folder using the ls command in the terminal.
The following should be in your "document-scanner" directory:
After the above steps are complete you are all set for scanning documents.
The code that we will run will be Document_Scanner.py . The code uses the os module in python in order to execute terminal commands. This code will perform the following functions:
- Take the image using the raspberry pi camera and save it in the /document-scanner/images directory
- Run the scan_main.py code that does the image processing and outputs the scanned image.
The scan_main.py program is the guts of the image scanning procedure. I have used this Pyimagesearch project for the image processing and added the dropbox uploading commands at the end of the code.The scan_main.py program performs the following functions:
- Identifies the Edges in the image.
- Detects the contours of the document.
- Applies a perspective transform to properly orient the document.
- Saves the output in the /document-scanner directory.
- Uploads the scanned image to the Dropbox folder.
The final scanned image is saved with a filename that has the day,month,year and minute of the moment the image was taken. For example : sc_Wed_08_06_2016_10.png (implying the image was taken on Wednesday, 8th June 2016).
This prevents overwriting of an image as they will not have the same name.
Step 6: Running the Code
You are now ready to run the document scanner code.
First open the command terminal and change directory to /document-scanner:
Then run the Document_Scanner.py program using:
The program outputs an acknowledgment after the completion of each step in the command terminal. Please have a look at the video of the demo of the program as a supplement to the execution steps mentioned above. I have also included an image of a receipt I scanned using this scanner.
Step 7: Mount the Camera and Scan Away !
I would recommend building a stable base for the Camera Module so that the pictures taken are clear and steady. For making a quick and temporary camera mount I used an acrylic sheet which I bent over a candle flame in order to make an L mount. Then I drilled holes in order to attach the Raspberry Pi camera module.
The result was not very pretty but it did the job and worked well for a student budget ! :D
You should now be set to scan all those bills, notes or documents that you need to make a copy of!