Computer Vision: Document Scanner with OpenCV and Python | Yacine Rouizi | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Computer Vision: Document Scanner with OpenCV and Python

teacher avatar Yacine Rouizi

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Overview

      1:08

    • 2.

      Installation

      2:22

    • 3.

      Load the Image

      1:11

    • 4.

      Image Processing

      1:30

    • 5.

      Use the Edges to Find all the Contours

      1:44

    • 6.

      Select Only the Edges of the Document

      3:22

    • 7.

      Apply Warp Perspective to Get the Top Down View of the Document

      2:46

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

37

Students

--

Project

About This Class

In this class, you will create a simple document scanner using the OpenCV library and Python. This can be useful, for example, for scanning pages in a book.

Now to build our project we will need to:

  • Convert the input image into grayscale.
  • Find the edges in the image using the canny edge detector.
  • Next, we can use these edges to find the contours on the image.
  • And from there we will select only the contours of the document.
  • And finally, we can apply warp perspective to get the top-down view of the document.

Meet Your Teacher

Hi! My name is Yacine Rouizi. I have a Master's level in physics of materials and components and I am a passionate self-taught programmer. I've been programming since 2019 and I teach on my blog about programming, machine learning, and computer vision.

My goal is to make learning accessible to everyone and simplify complex topics, such as computer vision and deep learning, by following a hands-on approach.

See full profile

Level: Beginner

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Overview : Hi. In this class, you will create a simple document scanner using the OpenCV library and Python. This can be useful, for example, for scanning pages in a book. My name is Yassin and I will be your instructor in this course. I've been programming since 2019. And I am the author of the vlog. Don't repeat yourself.org, where I have over 5 thousand developers each month to learn more about Python, machine learning, and computer vision. Now, to build our projects, we will need to convert the input image into a grayscale and find the edges in the image using the canny edge detector. Next, we can use these edges to find the contours on the image. And from there, we will select only the contours of the document. And finally, we can apply what we call work perspective to get the top-down view of the document, feel free to look at the course description, and I look forward to see you inside. 2. Installation : The first thing we need to do is to install the required packages useful for image processing. So we are going to install OpenCV and the non pi. Let's first start with OpenCV, open a new terminal window and run the command pip, install OpenCV polygon. In my case, you can see that I have open CV already installed. Requirement, already satisfied. But I just want to create a virtual environment. So the trio can see what you get when you installed it. Virtual environment, or you can be stuck to the grid. Now, let's install open CV. Cv. Here you can see that OpenCV was successfully installed along with non pipe open CV. So I don't need to install. Here. You can see that I have open CV 4.54.5, inversion of OpenCV and division of non pi is 1.22.1. We need to install other packages. The first one is I am mutagens package with, and we also need sidebar, which is needed for Ionic style. Here you can see that the PI have the version 0.51.4. We'll do know the novel some soapy is the version 1.37. 3. Load the Image: So we will import Open CV and we can read our image CV to dot. And let's also resize our image. So we'll say CV tool, lot, resize image. Here we will say width and height. The headway, we'll define our width and height. So let's say 600 for the width and 800 for the height. Let's also define green color. So we can say green is equal to 2550. Now the image dot, we want to use this hair and we want to get the top down view of this document here. 4. Image Processing: So now we can start processing our image. So we will start by converting it to gray scale it. Then we will apply a Gaussian blur. We can write, good. I is equal to CV to Series D color image, CB2, color BGR to gray. And here we will read CB2 Gaussian blur. And the finally, we can detect the edges using the Canny detector. So we'll see V2 dot Carney, our image. And let's say 75 for the first threshold and 200 for the second threshold. Now we can display our image CB2 dot, I am sure, message. And we can also display the image. So then we go, here is our original image and our image. 5. Use the Edges to Find all the Contours: Now we can move on to the next step, which is to find and draw out the contours. So we can use the CB2 dot find contours function to find the contours. So we can say contours to CB2 point contours. And here we provide the county or the image. Here for the retrieval mode, we can say CV two dot three lists. And for the approximation method, we can say jane products symbol. Now we can display our image and then draw out the contours and then re display our image with the contours. So we can say CV to note, I'm sure image. And here we will draw the contours. We provide our image, our contours, and the minus one to do it all on the contours. Then you can use the green color. And three for the thickness. Let's see what we get so far. So this is our image and this is the contours not we have a gentleman on the image. 6. Select Only the Edges of the Document: Next we need to find the biggest contour in the image. So this contour here, because that will be our document. So what we can do is to sort our contours by area from biggest to smallest. We can use the sorted function, which is a built-in Python function. So we can write here, contours is equal to sorted. So here we want to sort our contours and we can specify the area of our contours Using the CV 2D contour area function. And we also want to sort them from biggest to smallest. So we can write here, reverse is equal to true. So in this list, we will have the biggest contours first than the smallest one at the end of the list. So next, what we can do is to loop over this list of contours. And then we will see mates each contour. Then if we found a contour with four points, we can assume that we have found our document so we can break out of the loop. So we can write here for contour. Contours. Now to approximate the contour, we can first compute its perimeter using the CV to the arc length function. So we can write perimeter is equal to c v2 dot r. And here we provide our contour. And the further the second argument we will say true to indicate that the contour is closed. Now we can approximate our contour using the CV to approx, poly D p function. So we can write flux is equal to Cv into the approx Pauly D p. And here we provide our contour. Then we need to specify the approximation accuracy. We can say 0.05 times the perimeter of the contour. And the header. We can say true to indicate that the contour is closed. Now we can check if the approximated contour has a four points. So we can write if the length of our approximation is equal to four. So what we can do is to store our contour in a variable. So let's say document contours is according to our approximation. And now we can break out of the loop. 7. Apply Warp Perspective to Get the Top Down View of the Document: Now we are ready to get the top-down view of our document. So for that we have the four-point transform function from the IM mutants package. So first we need to import our function. So we can say from utils perspective, four points times one. Now you don't have to worry a lot about this function. What you need to know is that it takes an image and a contour as input, and it returns the top-down view of the image. So we can write here, warped is equal to four points transformed. And here we pass in our original image, but here we already genome on our image. So what we can do is to take a copy of our image. So we can say image coffee is a call to our original image dot copy. And now we can get out on the copy of our original image. So we will say image copy the head. We have our contours and we reshape them four by two. And finally, we can display our image, CB2 dot, I'm sure. We will say on the image and the warped. And we will also resize it to get to the same size as our image. So we will say CV to resize our warped image. And we will use the width and the height of our original image. So there we go. Here is our perspective. Now, you can see that the quality isn't that good. And this is because we are using a lower resolution, which is 600 by a tangent. Now, if you use the original size of the image, you will of course get to better results.