Create a Document Scanner Using Opencv

by dhruvarora561 in Circuits > Computers

374 Views, 2 Favorites, 0 Comments

Create a Document Scanner Using Opencv

web-ga7634b8ab_640.jpg

1. Scanning documents is function which is used in virtually all fields.

2. After reading this blog the user will be able to automate scanning of the documents and learn about `opencv`.

3. The source code contains two files `docScanner.py` and `docScannerUtils.py`

docScanner.py

1. 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.

2. In the `main()` function, the script initializes the `camera cv2.VideoCapture()` module and waits for 2 seconds for the camera to warm up. Then it initializes `trackbars` using `docScannerUtils.initializeTrackbars()`.

3. 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 using `docScannerUtils.stackImages()`.

4. 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.

5. Overall, the script provides a simple way to scan documents using OpenCV.

docScannerUtils.py

1. It defines several functions, such as `stackImages`, which stacks multiple images in a window, reorder, which reorders points in the document, `biggestContour`, which finds the biggest contour in an image, `drawRectangle`, which draws a rectangle around the biggest contour, `initializeTrackbars`, which initializes `trackbars` used to tune the threshold values for Canny edge detection, `valTrackbars`, which gets the current values of the `trackbars`, and `scanDocumentsInImage`, which scans documents in an input image.

2. The `scanDocumentsInImage` function seems to be the main function that performs the document scanning. It takes an input image as argument, sets the image size to `640x480` pixels, applies some image processing techniques to detect the edges and contours of the document, finds the biggest contour, reorders its points, draws a rectangle around it, and returns an array of images containing the scanned documents.

Supplies

  1. Brainy Pi
  2. Keyboard
  3. Monitor
  4. Internet Connection

Setting Up

1. We need to install all the dependencies before we can run the code.

2. 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

3. Let us create a virtual environemnt

 python -m venv venv  

4. Now, activate the virtual environment

cd venv/Scripts
activate

5. Installing the dependencies

pip install -r requirements.txt

Running the Code

Now, we are in a position to run the code

python docScanner.py