Hi, welcome back to the course.
We're going to take a look at OCR, which stands for optical character recognition using the PI Tesseract
Library.
So let's take a look.
Open this notebook and let's take a look.
So Pi Tesseract is a open source library that's available for Python and 50 languages as well.
Where where does where it takes an image input, recognizes, detects and returns a text as a string?
This is a high level overview of how that API works.
The PI Tesseract API takes an input image, does some processing on it, uses a Lepton account open
source image processing library to along with trained data to do its OCR engine to implement that algorithm
here.
And then it goes to a post processor, which probably probably cleans up the text a bit and adds spaces
and removes errors as well, and then provides a tax here as the output.
So you can take a look at this blog that actually explains it more in more detail if you want.
But now let's go down to actually using this Tesseract library, so to install it, we have to install
it.
The Tesseract, we'll see.
Our library is library files first and then do the pipe install of PI Tesseract.
So we've already done it here.
It takes about 12 seconds to run.
Not that long.
So let's go ahead and do that.
And know what we do here.
We are going to import PI Tesseract because you remember previously we just put it to a number of my
plot Lab. Now we're importing PI Tesseract, just like we import a dylib and we create the point to
this year.
This is the PI Tesseract Tesseract command line.
So we just point to where the Tesseract library was installed.
That's what we installed in the first line here in the above code Tesseract OCR.
This is how we are able to use it within Python or listening within CoLab.
It depends on which on your local system.
It depends on.
If you're doing it in Windows and Mac, you may not lead this line of code here anymore.
This is our initial function and is our test images here.
So let's load all little libraries.
There we go.
So now let's try our first OCR trial.
So the image we're testing here is welcome to OCR here, and you can see how simple data right to is.
Once we've loaded it, then you just take an image that you load using open TV here, and that is the
IMG file right here.
You just point that IMG variable to the page to select image, to string function, and that's it.
You get the output text so you can split it off the text here and there we go, so you can see the picture
struct extracted.
Text is welcome to OCR, and that's what was in the image.
This is an image here.
You can't actually highlight the text here, so that looks pretty good.
It does it work?
Do on white text, on black background.
So it's actually we didn't run this separately from this, just so we have it.
And this is one where it's on white text, on black background and it works.
As well as you can see, Tesseract has pretty versatile performance still.
But what about a more messy backgrounds like a back mess like this with a bunch of colors in between?
Let's see.
Let's see if it works.
It didn't work, unfortunately.
So patents are actors only so good.
They're pretty much more advanced libraries.
No easy OCR, which works quite well, but by Tesseract field at this example here.
So let's try, though, on a real world scan.
So let's do one this image here, which is a real world scan from one of my text textbooks.
And you can see it worked, okay, ish, wasn't that accurate and text looks weirdly formatted, but
it worked to an extent.
Now let's let's see something.
Let's see if we actually clean up this image.
So we're going to perform some trash only here using the image filters instead of the open sea view
one, because this one tends to look better for scan texts.
You can see the output here, and let's take a look at home page to select performed.
So you can immediately see that it looks a lot better.
It's not perfect, but it did look a lot better.
So thresholding helps a lot of other processes that we can do would be blurring to clean up the image.
Definitely try showing this skewing because this image is quite skewed.
Have this kind of curve in the middle here.
This boulder should see dilation, erosion, close opening, closing hopes, as well as other noise
removal.
So let's try to correct on another image.
Let's try it on a receipt would receive receipts, which is an American story, I believe so we load
the receipt.
We are going to we're going to do some pre-processing here.
So we're going to actually visit the agency here and then we're going to treasure it differently.
But they're using a scale for this here.
And then just get up with here.
So we get a nice treasure that image here and we run it through by Tesseract at the end.
And you can see that's actually run this stuff sort of looking at the saved output and you can see it
would almost perfectly.
That's quite impressive.
So, you know, you can develop your own receipts scanner and get the data out and run some post-processing
on it.
Do some data analysis using receipts.
It's pretty cool.
So let's take a look at something here.
Let's take a look at the output for this year.
So Pi Tesseract gives this output image to data as a dictionary of keys right here.
So you can use these keys now.
The reason I'm showing you this is that 19 actually use this to get the exact bounding boxes in an image
where you taught text was located, where the parties are up to a text was located.
So let's run this here I'm using.
We're looping through all the boxes.
It returns here.
So you can see this here.
Actually, this key here gives you level page.
No block.
I'm not sure what portion is line number with number and left topped with tight and confidence is that
is the basically the bounding box of the text and the confidence it establishes.
So let's plot this, and it took a look at something very cool.
Oops, we didn't actually look this.
So now we know this there, and you can see the all of the bounding boxes where patents are up to it.
Text was located, you can see it's got some textures here, unfortunately, when it wasn't.
And it leaves out Woolworths here, as well as these two W's and some numbers here and there.
But overall, this performance is performing pretty good.
It, if you were to set a little lower thresholds, could probably get more boxes.
So that's it for disarray.
So no, let's move on to using easy OCR to do the same text detection and with optical character recognition.
However, easy OCR is a much better library than pie Tesseract.
However, it's a bit slow when running on the CPU, but it can luckily run on GPU systems.
If you were to change just to keep you by going to settings and changing into GPU, you would have to.
It would work much quicker, but you'd have to reload the images.
So just remember that.
But now I'm going to leave it as CPU for you guys.
So to run it and install easy OCR.
Just run this.
Click this on Arrow or just run this code block here, and it will download it with easy OCR and it
will ask you to restart runtime.
So to do that actually the most, let's just reinstall this anyway.
It shouldn't take that long for me.
Oh, OK.
Didn't ask me to restart one time because I already did, however, restart your run time when the prompt
comes up and click Yes, and that's it.
You will have to reload your packages telling us.
So that's why we import these packages again here.
So let's firstly, let's just play our test image and we're going to run easy OCR using the detect method.
Oops.
This initial function isn't defined, so let's get it from here and place it up here.
So this is.
Well, probably the name is different.
I'm not sure why.
So this is the impediment here.
This is just a WhatsApp conversation with my friend Sophia.
And what it does to tells you firstly, CUDA is unavailable, so using the CPU and then it tells you
it's detecting OCR in the texts and this do this, this cleaning up, it's soon to be around 14 seconds.
You can see that they're quite a well of the output, however, is in results and we actually don't
display our results.
So let's just put the results here and take a look at it.
And you can see these are the results here.
So what is this?
This is basically remember what pilots are after we got boning boxes.
Confidence scores and texts.
Well, this is all of it here.
So we're going to process this afterward and put the text box on the image just like we did with the
pilot throughout the Millwood scan.
But for now, you can see this is how we run easy OCR.
It's quite simple.
We import to read a function here.
We know the imaging and we just declare that we're going to look at English here.
I have G view that it's true, but I'm I'm actually using GPUs, so that's going to be an error, which
is what the area.
So it was an error.
It was a warning.
So just put this to fall to true, depending if you're using a GPU inside of a list here, you can see
it's a list it tells you is looking for English language tests and then we just have a timer here just
to get the time it's sticking to process this.
And that's not really necessary.
So this is just for statistical purposes.
If you want to monitor the efficiency of your code and we use a read text function, which we imported
here from Rita.
So this is sorry from read it here and then descriptors reader objects here.
And then we have a reader that read text, text, input image, and Rita gets all the output results
that we saw here.
So let's take a look and displaying the text on the image.
So we're going to use this function well.
This block of code here that basically goes to the results, gets the money box text and the probability
scores, and it just draws bounding box gets extracts, the voting box coordinates.
Oh, right here you can see this is top left, top right, bottom right, bottom left, and then we
just clean up the text of it using this little line of code here, and we just put the bong in boxes
and the text onto the image.
So let's do that now.
So again, just an image.
There we go.
So you can see this wood almost perfectly well, it pretty much got all the ducks.
I mean, the system detects from a screenshot, so it should work fairly well.
But it's actually pretty good.
So that's it for easy OCR.
No.
Let's take a look at how easy OCR compares to protests are on the same littlebit image.
So this block of code takes a little while to run because it's a lot of text.
So just look at the opportunity to run this in this video, but you can see particular look.
It is far better than fighter, sir.
I've got all the text that even find some little hidden text in the background here, but we don't really
want this.
It is Arabic for previously.
Such is ignore that for now, but you can see it got almost everything here, and it looks like it got
all the text correct is a bit hard to see if we can zoom in, but it looks like everything seems to
be perfectly perfectly correct.
One was perfectly correct in this image, so this is quite good performance here.
I'm fairly impressed with this.
So if you were to use any losartan applications, I would encourage you to use easy OCR as opposed to
by Tesseract.
However, if speed is a concern and you don't have a GPU available, Titus or up might be a better solution.
So that's it for this lesson.
The next lesson?
We're going to take a look at Barcoo generation and reading.
This is actually backward and QR code.
So let me just rename this appropriately.
So stay tuned and I'll see you in the next section.
Thank you.