Version 1 of Tcl does OCR online

Updated 2019-03-25 23:18:29 by ABU

ABU 25-mar-2019

The problem: extract text from images using a web-service.

UNDER CONSTRUCTION

The problem: extract text from images using a web-service.

UNDER CONSTRUCTION

A practical example

Let's take a picture like this OCR-Image1 and try to extract the text ....

We are going to use an OCR web-service provided by "ocr.space" web-site. We need a registration key ( it's free, see https://ocr.space/ocrapi ), a bunch of code (attached at the end of thi page), then we could run:

  set MYAPIKEY "xxxxxx"     ;# <-- insert your free API-key here
  set URL      "http://api.ocr.space/Parse/Image"
  
   # prepare parameters for http::POST ...
  set header [list apikey $MYAPIKEY]
  set form {
    {file     -file c:/tmp/myImage.png}
    {language eng}
    {scale    true}
  }
  set token [http::POST $URL -header $header -post $form]
  set response [http::data $token]
  http::cleanup $token
  set txt [ocr.space.decodeResponse $response]
  puts "-------------------------"
  puts $txt
  puts "-------------------------"

response is the following: (NOTE: I highlighted errors in bold)

CHAPTER 
Software starts as an Idea. 
Let's assume It's a good Idea—an idea that could make the world a better 
place, or at least make someone some money. me challenge of the software 
developer is to take the idea and make It real. into something that actually 
delivers that benefit. 
The original Idea is perfect, beautiful. Ifthe person who has the idea happens 
to be a talented software developer. then we might be in luck: the Idea could 
be turned Into working software without ever needing to be explained to 
anyone else. More often, though, the person with the original idea doesn't 
have the necessary programming skill to make It real. Now the idea has to 
travel from that person's mind Into other people's. It needs to be communicated. 

Response is fast, less than half second (including the upload time), but if you don't want to wait for the response, you can issue an asynchronous call.

Just add a callback procedure, and add a -command option to http::POST:

proc onResponse {token} {
     ... get the token,
     ..  extract the response
     ... don't forget to clenaup/free the token
     ... decode the token and store the result somewhere
}
set header { .. same as previous ..}  
set form { ..  same as previos ... }
http::POST http://api.ocr.space/Parse/Image -headers $header -form $form -command onResponse
puts "OCR launched ... result will be saved somehere ..."

Of course you need two special commands: http::POST and ocr.space.decodeResponse ; they are attached at the end of this page, but first let's examine the above code deeper ...