Optical character recognition (OCR) with UWP API
Re: Optical character recognition (OCR) with UWP API
yes.English (United States)
Re: Optical character recognition (OCR) with UWP API
While this is a very useful script, I think there may be some systemic reason of my computer why I decided to give it up. Thank you for your answer.
Re: Optical character recognition (OCR) with UWP API
Hi, first of all thanks a lot for this tool, as other say it is way better than tesseract. At the moment I'm trying to loop the ocr tool at my discord messages in less than 1 second intervals. Given the speed in which it did the job I thought it wouldn't pose a problem. But after a few loops it gets randomly stuck in a loop waiting for a dll call:
It will then receive status = 0 about 20 seconds later and resume, to go into this mode a few seconds later. I'm really not that savvy in windows dll calls, at first I thought that maybe I was overwriting the async task too fast so it wouldn't get to finish before I asked it to do it again. But I get the same result if I loop it in 200 ms, 1000 or 3000...
Does anyone has any idea why this is happening? Any hints on how to solve it?
Thanks!
Code: Select all
400: WaitForAsync(SoftwareBitmap)
462: AsyncInfo := ComObjQuery(Object, IAsyncInfo := "{00000036-0000-0000-C000-000000000046}")
463: Loop
465: DllCall(NumGet(NumGet(AsyncInfo+0)+7*A_PtrSize), "ptr", AsyncInfo, "uint*", status)
466: if (status != 0)
477: Sleep,10 (0.02)
Does anyone has any idea why this is happening? Any hints on how to solve it?
Thanks!
Re: Optical character recognition (OCR) with UWP API
Try this approach. Works for me.
Spoiler
Re: Optical character recognition (OCR) with UWP API
after running my script a bit i am getting a asnycinfo error is there somewhere to see what the error numbers mean? asyncinfo status error 2147942414
Re: Optical character recognition (OCR) with UWP API
It means E_OUTOFMEMORY.
-
- Posts: 502
- Joined: 03 Dec 2018, 20:02
Re: Optical character recognition (OCR) with UWP API
It seems that it will return blank for texts without much whitespace, is there any way to extend the whitespace or bypass this?
Re: Optical character recognition (OCR) with UWP API
This API does not have such method.
-
- Posts: 502
- Joined: 03 Dec 2018, 20:02
Re: Optical character recognition (OCR) with UWP API
I used Gdip to upscale the smaller selections and now it's working great
There is just one problem, does this api expose any settings like page segmentation mode = single character in tesseract? It doesn't seem to recognize single numbers or letters.
Code: Select all
area := GetArea()
pBitmap := Gdip_BitmapFromScreen(area.x "|" area.y "|" area.w "|" area.h)
pBitmap := Gdip_ResizeBitmap(pBitmap, 500, 500, true)
;Gdip_SaveBitmapToFile(pBitmap, "output_test.png")
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
pIRandomAccessStream := HBitmapToRandomAccessStream(hBitmap)
DllCall("DeleteObject", "Ptr", hBitmap)
Gdip_DisposeImage(pBitmap)
text := ocr(pIRandomAccessStream, "en-US")
MsgBox, % text
Return
Re: Optical character recognition (OCR) with UWP API
No.does this api expose any settings like page segmentation mode = single character in tesseract?
Re: Optical character recognition (OCR) with UWP API
Thank you for the code! I have tried to use that but I'm getting the following error: "asyncinfo status error 2291674960". Do you know what could be happening?william_ahk wrote: ↑28 Oct 2020, 08:58I used Gdip to upscale the smaller selections and now it's working greatThere is just one problem, does this api expose any settings like page segmentation mode = single character in tesseract? It doesn't seem to recognize single numbers or letters.Code: Select all
area := GetArea() pBitmap := Gdip_BitmapFromScreen(area.x "|" area.y "|" area.w "|" area.h) pBitmap := Gdip_ResizeBitmap(pBitmap, 500, 500, true) ;Gdip_SaveBitmapToFile(pBitmap, "output_test.png") hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap) pIRandomAccessStream := HBitmapToRandomAccessStream(hBitmap) DllCall("DeleteObject", "Ptr", hBitmap) Gdip_DisposeImage(pBitmap) text := ocr(pIRandomAccessStream, "en-US") MsgBox, % text Return
I'm trying to get the current number in this image:
[Mod edit: Image fixed.]
If someone have an advice for how I should use the OCR to identify that would be amazing, because I'm failing 100%.
Re: Optical character recognition (OCR) with UWP API
This api is for windows >= 8.1
Re: Optical character recognition (OCR) with UWP API
If You use the code from the first post will it work for You?
Re: Optical character recognition (OCR) with UWP API
Yep, It does work. I'm running with Gdip v1.85 and I have just included that and changed the beginning of the file:
Code: Select all
^X::
area := GetArea()
pBitmap := Gdip_BitmapFromScreen(area.x "|" area.y "|" area.w "|" area.h)
pBitmap := Gdip_ResizeBitmap(pBitmap, 500, 500, true)
;Gdip_SaveBitmapToFile(pBitmap, "output_test.png")
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
pIRandomAccessStream := HBitmapToRandomAccessStream(hBitmap)
DllCall("DeleteObject", "Ptr", hBitmap)
Gdip_DisposeImage(pBitmap)
text := ocr(pIRandomAccessStream, "pt-BR")
MsgBox, % text
Return
If you know a better way for that I am totally opened to suggestions.
Re: Optical character recognition (OCR) with UWP API
You have to load gdiplus library for such functions.
-
- Posts: 502
- Joined: 03 Dec 2018, 20:02
Re: Optical character recognition (OCR) with UWP API
Actually I'm using Capture2Text now since this api cannot recognize single letters/digits. Here's a simple function I've wrote for Capture2Text:
Code: Select all
Capture2Text(coords) {
static program_path := ".\Capture2Text\Capture2Text_CLI.exe"
text := RunCMD(program_path . " --screen-rect """ . coords[1] . " " . coords[2] . " " coords[3] . " " . coords[4] """")
;if an error occurred, return an empty string
if (text = "Error, OCR failure.`r`n<Error>`r`n") {
return ""
} else {
return RTrim(text, "`r`n")
}
}
Testing your image it appears Capture2Text can recognize it perfectly fine:
Re: Optical character recognition (OCR) with UWP API
Just insert Gdip_Startup() in Your code.
Re: Optical character recognition (OCR) with UWP API
Hum, understood! Thank you sir!
Thank you very very much!william_ahk wrote: ↑30 Nov 2020, 22:11Actually I'm using [url=http capture2text.sourceforge.net /]Capture2Text[/url] Broken Link for safety now since this api cannot recognize single letters/digits. Here's a simple function I've wrote for Capture2Text:
Also needs RunCMD by SKANCode: Select all
Capture2Text(coords) { static program_path := ".\Capture2Text\Capture2Text_CLI.exe" text := RunCMD(program_path . " --screen-rect """ . coords[1] . " " . coords[2] . " " coords[3] . " " . coords[4] """") ;if an error occurred, return an empty string if (text = "Error, OCR failure.`r`n<Error>`r`n") { return "" } else { return RTrim(text, "`r`n") } }
Testing your image it appears Capture2Text can recognize it perfectly fine:
c2t.png