Sign in to follow this  
Followers 0
eskermeko

Character Recognition or OCR Advice

19 posts in this topic

#1 ·  Posted (edited)

Hi

This is my first post here and I would just like to write how wonderful this software is. Its helped me alot.

I have been writing my first OCR. But I am using PixelChecksum and with a gradient background on the pictures I am finding it very hard.

Was wondering can I count the pixels of a certain colour in a specified block of a picture.

I would like to count the white pixels.

Any help welcome.

Edited by eskermeko

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Since you didn't get any inteligent answers, I'll give you one of mine instead... :shocked:

OCR is hard, very hard. Differences in fonts, font size, bold, italics, underlining, color, superscript, subscript, etc., etc. make it an extremely hard thing to code. If you use the Search on this forum for OCR, you'll get a lot of discussion about the dificulties. You could, just, create OCR functions in AutoIT. But they will be very limited, to a particular font and size for example, and likely very slow. Search will find you some examples, but I think you will find they were coded for very limited special circumstances.

For some idea of the level of effort that goes into and OCR project, take a look at Tesseract or the more recent OCRopus projects.

As for your actual AutoIT question, you can walk through a graphic space on the screen with For/Next loops doing PixelGetColor() and count pixels with a given color. You can even do it inside a BMP file of an image fairly straight forwardly. But interpreting what you get will become horribly complicated realy quickly, so think about other ways to get whatever it is done, too. :(

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Since you didn't get any inteligent answers, I'll give you one of mine instead... :shocked:

OCR is hard, very hard. Differences in fonts, font size, bold, italics, underlining, color, superscript, subscript, etc., etc. make it an extremely hard thing to code. If you use the Search on this forum for OCR, you'll get a lot of discussion about the dificulties. You could, just, create OCR functions in AutoIT. But they will be very limited, to a particular font and size for example, and likely very slow. Search will find you some examples, but I think you will find they were coded for very limited special circumstances.

For some idea of the level of effort that goes into and OCR project, take a look at Tesseract or the more recent OCRopus projects.

As for your actual AutoIT question, you can walk through a graphic space on the screen with For/Next loops doing PixelGetColor() and count pixels with a given color. You can even do it inside a BMP file of an image fairly straight forwardly. But interpreting what you get will become horribly complicated realy quickly, so think about other ways to get whatever it is done, too. :(

Thanks I quickly put this together

$xstart = 253
$ystart = 64
$xend = 262
$yend = 74



$x = $xstart
$y = $ystart


$count = 0
while $xend > $x    
        
if PixelSearch( $x, $y, $y+1, $y+1, 0xFF0000 ) Then
    $count = $count + 1
    EndIf


$x = $x + 1
$y = $y +1

Wend

MsgBox(0,"Count=", $count)

Im new at this. Somethings wrong, dunno what.

I keep on getting same number(9) when I count.

Edited by eskermeko

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Oops no wait thats totally wrong :shocked: ,Think PixelgetColour right. I realized the Pixelsearchcolor should be:

$pixel = PixelSearch( $x, $y, $y+1, $y+1, 0xFFCC00 )

If Not @error Then

$count = $count + 1

EndIf

And Im doing the x y wrong also

Edited by eskermeko

Share this post


Link to post
Share on other sites

Well I had to do this:

while $yend > $y

  $z = $y + 1

while $xend > $x
        

$var = PixelGetColor( $x , $z )

If $var = "16763904" then
    $count = $count + 1
    EndIf


$x = $x + 1


    Wend
    $x = $xstart
    $y = $y + 1
    
Wend

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If you will look for the pixels more than once, you want it in a function you call over and over again. Here is an example of search a rectangular area for three different colors:

; Count pixels of a certain color in a 100x40 box starting at 200,300
Dim $xstart = 200, $width = 100
Dim $ystart = 3000, $height = 40

$SearchColor = 0
$Result = _PixelGetColorCount($xstart, $ystart, $width, $height, $SearchColor)
MsgBox(64, "Results", "Black pixels found:  " & $Result)

$SearchColor = 0xFFFFFF
$Result = _PixelGetColorCount($xstart, $ystart, $width, $height, $SearchColor)
MsgBox(64, "Results", "White pixels found:  " & $Result)

$SearchColor = 16763904
$Result = _PixelGetColorCount($xstart, $ystart, $width, $height, $SearchColor)
MsgBox(64, "Results", "Pixels found of color '16763904':  " & $Result)

;-----------------------------
; Function _PixelGetColorCount()
;   Returns the number of pixel of a certain color
;   Call with:  _PixelGetColorCount($x, $y, $w, $h, $color)
;   Returns count of matching pixels on success
;-----------------------------
Func _PixelGetColorCount($x, $y, $w, $h, $color)
    Local $count = 0, $IntX, $IntY
    For $IntX = $x To $x + $w
        For $IntY = $y To $y + $h
            If PixelGetColor($IntX, $IntY) = $color Then $count += 1
        Next
    Next
    Return $count
EndFunc   ;==>_PixelGetColorCount

:shocked:

Edit: From another post, there is a reference to the MODI OCR, which is the Microsoft Office Document Imaging interface. Gives you some more options if you have MS Office 2003 or later installed, but would require learning to use the COM object capabilities of AutoIT to take advantage of.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

THanks I was working on this today

What a mission.

But all for nothing cause some of the letters has the same amount of pixels.

Maybe I can search by shade of colours

With the code I scanned every vertical line and when I found a blank vertical I would reset the counters for a new Letter.

So it doesn't just read one letter.

$xstart = 161
$ystart = 21
$yend   = 31
$count = 0
$height = $yend - $ystart
$width = 24
$colour = "16763904"

$x = $xstart
$y = $ystart



$count2 = 0
$crashProtect = 0

FOR $a = $width to 1 Step -1    

;Init count
$count = 0
;count Vertical Lines 
$count = CountVertical($x,$y,$height,$colour)
;Move on xaxis
$x = $x + 1


;Add Vertical lines together
$count2 = $count2 + $count

;Print & reset counters on new line
IF $count = 0 THEN 
    
    IF $count2 > 1 THEN
ControlSend("Untitled - Notepad", "", "Edit1", " . ") 
ControlSend("Untitled - Notepad", "", "Edit1", $count2)
ControlSend("Untitled - Notepad", "", "Edit1", " . ") 
    ENDIF
    
    $a = $width
        $crashProtect = $crashProtect + 1
         $count2 = 0
Else
    $crashProtect = 0
ENDIF


;Protect against infinite loop
IF $crashProtect > 25 Then
    ExitLoop
EndIf

Next


    


;Functions
Func CountVertical($x,$y,$height,$colour)

    
FOR $i = $height to 1 Step -1   

$var = PixelGetColor( $x , $y )


If PixelGetColor($x, $y) = $colour Then $count += 1


$y = $y + 1

Next

return $count

EndFunc

Share this post


Link to post
Share on other sites

I changed the function to see if I can get shades

But this does not seem to work

;Functions
Func CountVertical($x,$y,$height,$colour)

$z = $y 
FOR $i = $height to 1 Step -1   
$z = $z + 1

$var = PixelSearch($x, $y,$x,$z,$colour, 10) 
If Not @error Then
  $count += 1
EndIf

Next

return $count

EndFunc

Am I doin it right?

Any help appreciated

Share this post


Link to post
Share on other sites

I changed the function to see if I can get shades

But this does not seem to work

;Functions
Func CountVertical($x,$y,$height,$colour)

$z = $y 
FOR $i = $height to 1 Step -1   
$z = $z + 1

$var = PixelSearch($x, $y,$x,$z,$colour, 10) 
If Not @error Then
  $count += 1
EndIf

Next

return $count

EndFunc

Am I doin it right?

Any help appreciated

Share this post


Link to post
Share on other sites

I changed the function to see if I can get shades

But this does not seem to work

;Functions
Func CountVertical($x,$y,$height,$colour)

$z = $y 
FOR $i = $height to 1 Step -1   
$z = $z + 1

$var = PixelSearch($x, $y,$x,$z,$colour, 10) 
If Not @error Then
  $count += 1
EndIf

Next

return $count

EndFunc

Am I doin it right?

Any help appreciated

Share this post


Link to post
Share on other sites

Something went wrong with browser and that s why it printed three posts

Share this post


Link to post
Share on other sites

I changed the function to see if I can get shades

But this does not seem to work

Am I doin it right?

Any help appreciated

This works to count one vertical stripe of pixels:

;Functions
Func CountVertical($x, $y, $height, $colour)
    Local $PixY, $count = 0

    For $PixY = $y To $y + $height
        PixelSearch($x, $PixY, $x, $PixY, $colour, 10)
        If Not @error Then $count += 1
    Next

    Return $count
EndFunc   ;==>CountVertical

Something went wrong with browser and that s why it printed three posts

You realize writing your own web browser might be easier than your own OCR?

:shocked:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Thanks very much

LOL!

I have written a simple web browser with C# but this is insane man :shocked:

Edited by eskermeko

Share this post


Link to post
Share on other sites

Thanks for the help, I finally got it working.

I was adding the vertical lines all this time instead of just putting them next to each other. That fixed the whole thing.

It was pretty frustrating and hard to do but in the end worth it.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

You could check out my Tesseract OCR UDF if you are after a free OCR solution:

Tesseract UDF

Cheers,

Sean.

Edited by seangriffin

Cheers, Sean.

See my other UDFs:

Chrome UDF - Automate Chrome | SAP UDF - Automate SAP | Java UDF - Automate Java Applications & Applets | Tesseract (OCR) UDF - Capture text from applications, controls and the desktop | Textract (OCR) UDF - Capture text from applications and controls | FileSystemMonitor UDF - File, Folder, Drive and Shell Monitoring | VLC (Media Player) UDF - Creating and controlling a VLC control in AutoIT | Google Maps UDF - Creating and controlling Google Maps (inc. GE) in AutoIT | SAPIListBox (Speech Recognition) UDF - Speech Recognition via the Microsoft Speech (SAPI) ListBox | eBay UDF - Automate eBay using the eBay API | ChildProc (Parallel Processing) UDF - Parallel processing functions for AutoIT | HyperCam (Screen Recording) UDF - Automate the HyperCam screen recorder | Twitter UDF - Automate Twitter using OAuth and the Twitter API | cURL UDF - a UDF for transferring data with URL syntax

See my other Tools:

Rapid Menu Writer - Add menus to DVDs in seconds | TV Player - Automates the process of playing videos on an external TV / Monitor | Rapid Video Converter - A tool for resizing and reformatting videos | [topic130531]Rapid DVD Creator - Convert videos to DVD fast and for free | ZapPF - A tool for killing processes and recycling files | Sean's eBay Bargain Hunter - Find last minute bargains in eBay using AutoIT | Sean's GUI Inspector - A scripting tool for querying GUIs | TransLink Journey Planner with maps - Incorporating Google Maps into an Australian Journey Planner | Automate Qt and QWidgets | Brisbane City Council Event Viewer - See what's going on in Brisbane, Australia

Share this post


Link to post
Share on other sites

I've just uploaed this native autoit screen OCR (working reasonably well, but still not totally bug-free):-

Also this can search for a pixel pattern (eg a word), but doesn't return the actual text:

May be useful.

David

Share this post


Link to post
Share on other sites

@dgm5555

Why did you post this here? This thread is 5 years dead, with a 3 year old necroing attempt on it already. Please don't do that again.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

dgm5555,

You have necro-posted twice today - and both times hijacked the threads to publicize your own. Please do not do it a third time. :naughty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0