Gophering
Junior Member
- Mar 21, 2013
- 115
- 281
Ok, so I've been meaning to write an OCR tut for quite some time (long before coming across BHW). After checking out another thread in the "C, C++, C#" subforum, titled "C# - OCR - Tutorial" (great thread btw) I've decided to finally get this going and write down a theoretical/practical guide.
This might turn out to be somewhat long as we'll need to cover a couple of thing here so I'll probably break this down into multiple posts. All the information will be kept in this thread, I might make references to external sites as well as post code snippets to pastebin, etc, but generally speaking, you should be good just by following this thread.
Alright, lets get started!
Requirements
Now, whats needed in order to follow this tut? Firstly, I won't be using any external OCR/OCR-like engines (so no tesseract, etc.), we'll be building our own. Secondly, I'll be using Golang[1] (that Google language) to illustrate the methods involved. Why not C# or C or C++ or "insert language here" you ask? Well, quite honestly this can be accomplished in any more or less mature language. I chose Go because this way we won't waste time writing boilerplate image processing code, etc. Further on, the syntax is very readable especially if you are coming from a C background. Finally, I really want people to learn something from this rather than just copy-paste this into your project. So, read the code, study the material and you should be good to go.
If you've never worked with Go before, feel free to follow the excellent official tutorial[2]. Just familiarize yourself with the syntax so that you can follow along.
The Process
Next, lets have a look at the overall process involved.
We'll obviously need a captcha to crack first. It's best to start out slow and simple in regards to that so don't jump into cracking Recaptcha, you won't succeed. I'll provide a generic b&w captcha with some distortion/obfuscation later down the line, feel free to use your own though.
Generally speaking any OCR process involves the preparation of the image (simplification, cleaning), the separation of interesting content and the decryption of said content. So, in our case we'd need to get our captcha image, reduce its colour range to make things a bit easier, clean it, extract the text, break down the text into standalone letters, classify the letters, get a confidence rating on each letter, print out the result.
Note that, in terms of accuracy, theres usually no need to go for a very high rate. A 20% accuracy rate is already enough as theres usually no penalty involved. So for our purpose we'll consider a 20% accuracy rate a success. Of course, there are various things we can do to improve upon this, which I'll discuss later down the line.
Classification
How do we teach a machine to extract and more importantly recognize characters in an image? Commonly neural networks[1] are employed to solve this kind of problem. Without going into too many details and ultimately boring you to death (lol), a neural network is an adaptive algorithm designed to solve a computational problem without the programmer being specifically aware of the right solution involved. In other words, a number of solutions are derived from a constant adjustment/learning process.
A neural network can be represented like so:
input -> magic -> output
Obviously the magic part is where the machine learning happens and a number of solutions with varying degrees of accuracy are computed. Again, this is a very superficial explanation and I encourage you to read [1].
Interestingly enough however, we won't be using neural networks to solve our problem here. While neural networks will probably be much better suited towards solving more complicated captchas we should be able to achieve a fairly high accuracy rate by utilizing a vector space search engine[2]. In other words, we already know the right process towards solving this problem, so we'll utilize a somewhat "hacky"/fast solution instead of trying to write a more generic alternative.
This might turn out to be somewhat long as we'll need to cover a couple of thing here so I'll probably break this down into multiple posts. All the information will be kept in this thread, I might make references to external sites as well as post code snippets to pastebin, etc, but generally speaking, you should be good just by following this thread.
Alright, lets get started!
Requirements
Now, whats needed in order to follow this tut? Firstly, I won't be using any external OCR/OCR-like engines (so no tesseract, etc.), we'll be building our own. Secondly, I'll be using Golang[1] (that Google language) to illustrate the methods involved. Why not C# or C or C++ or "insert language here" you ask? Well, quite honestly this can be accomplished in any more or less mature language. I chose Go because this way we won't waste time writing boilerplate image processing code, etc. Further on, the syntax is very readable especially if you are coming from a C background. Finally, I really want people to learn something from this rather than just copy-paste this into your project. So, read the code, study the material and you should be good to go.
If you've never worked with Go before, feel free to follow the excellent official tutorial[2]. Just familiarize yourself with the syntax so that you can follow along.
Code:
[1] golang [.] org
[2] tour [.] golang [.] org
The Process
Next, lets have a look at the overall process involved.
We'll obviously need a captcha to crack first. It's best to start out slow and simple in regards to that so don't jump into cracking Recaptcha, you won't succeed. I'll provide a generic b&w captcha with some distortion/obfuscation later down the line, feel free to use your own though.
Generally speaking any OCR process involves the preparation of the image (simplification, cleaning), the separation of interesting content and the decryption of said content. So, in our case we'd need to get our captcha image, reduce its colour range to make things a bit easier, clean it, extract the text, break down the text into standalone letters, classify the letters, get a confidence rating on each letter, print out the result.
Note that, in terms of accuracy, theres usually no need to go for a very high rate. A 20% accuracy rate is already enough as theres usually no penalty involved. So for our purpose we'll consider a 20% accuracy rate a success. Of course, there are various things we can do to improve upon this, which I'll discuss later down the line.
Classification
How do we teach a machine to extract and more importantly recognize characters in an image? Commonly neural networks[1] are employed to solve this kind of problem. Without going into too many details and ultimately boring you to death (lol), a neural network is an adaptive algorithm designed to solve a computational problem without the programmer being specifically aware of the right solution involved. In other words, a number of solutions are derived from a constant adjustment/learning process.
A neural network can be represented like so:
input -> magic -> output
Obviously the magic part is where the machine learning happens and a number of solutions with varying degrees of accuracy are computed. Again, this is a very superficial explanation and I encourage you to read [1].
Interestingly enough however, we won't be using neural networks to solve our problem here. While neural networks will probably be much better suited towards solving more complicated captchas we should be able to achieve a fairly high accuracy rate by utilizing a vector space search engine[2]. In other words, we already know the right process towards solving this problem, so we'll utilize a somewhat "hacky"/fast solution instead of trying to write a more generic alternative.
Code:
[1] wikipedia [.] org/wiki/Artificial_neural_network
[2] la2600 [.] org/talks/files/20040102/Vector_Space_Search_Engine_Theory.pdf


