Scanning documents is a vital function utilized in almost every field today. However, manually scanning and digitizing large volumes of documents can be time-consuming and labor-intensive. Automating this process can lead to increased efficiency and productivity. In this blog, we will explore how to automate the scanning of documents using OpenCV, an open-source computer vision library, and build and deploy this application on BrainyPi. By the end of this blog, users will have a clear understanding of the tools and techniques required to develop a document scanner using Opencv on BrainyPi which can help save time and effort in various industries.
docScanner.py
-
This is a Python script for a document scanner that uses OpenCV. The script starts by importing the necessary libraries and defining a main() function.
-
In the
main()
function, the script initializes thecamera cv2.VideoCapture()
module and waits for 2 seconds for the camera to warm up. Then it initializestrackbars
usingdocScannerUtils.initializeTrackbars()
. -
The script then enters an infinite loop where it captures an image from the camera, scans the document in the image using
docScannerUtils.scanDocumentsInImage()
, and displays the resulting image usingdocScannerUtils.stackImages()
. -
If the
s
key is pressed, the script saves the scanned image to a file in the “Scanned” directory with a name like “qmyImage0.jpg”, “qmyImage1.jpg”, and so on. It then displays a green rectangle with text “Scan Saved” on the image.
If the q
key is pressed, the script turns off the camera, closes all windows, and ends the program.
-
Overall, the script provides a simple way to scan documents using OpenCV.
docScannerUtils.py
-
The
scanDocumentsInImage()
function is the main function that performs the document scanning.-
Inputs:
-
inputImage ([opencv Mat]): Input image
-
-
Outputs:
-
imageArray ([array of opencv Mat]): Contains array of images, with the last image being the scanned document.
-
-
Running Code for Document scanner using OpenCV on BrainyPi
-
We also need to get the code before proceeding with the dependencies installation.
git clone https://github.com/brainypi/brainypi-opencv-examples.git
cd document-scanner
-
Installing the dependencies
pip install -r requirements.txt
-
We can now run the code and start scanning the QR codes.
python docScanner.py
-
Press
q
exit out of the program.