Vis2 - Image to Text OCR()

Post your working scripts, libraries and tools
paulpma
Posts: 39
Joined: 08 Sep 2018, 22:05

Re: Vis2 - Image to Text OCR()

19 May 2020, 01:22

@AHKMode
use

Code: Select all

 MouseClickDrag
https://www.autohotkey.com/docs/commands/MouseClickDrag.htm

or

Code: Select all

SendEvent {Click 6, 52, down}{click 45, 52, up}
or

Code: Select all

text:=OCR(x,y,x1,y1)
; see help file.. (last example most likely to work)
AHKMode
Posts: 10
Joined: 20 Apr 2020, 20:21

Re: Vis2 - Image to Text OCR()

20 May 2020, 01:06

Thanks for the reply @paulpma

^w::
text := OCR()
Sleep, 1000
SendEvent {Click 6, 52, down}{click 45, 52, up}
Sleep, 1000
MsgBox, % text

this is my script and not working.. the line SendEvent {Click 6, 52, down}{click 45, 52, up} was execute after i click left of the mouse button. i want to know what command should i do to make the OCR() function and mouseclickdrag method will be together. I've search on the google and i cannot compose the script of OCR() with somethings line like controlclick or controlsend or control.. because i want to know what should i put on the script combined the OCR() function and mouseclickdrag/sendevent..

hopefully you understand me. thanks again!
paulpma
Posts: 39
Joined: 08 Sep 2018, 22:05

Re: Vis2 - Image to Text OCR()

22 May 2020, 08:50

Hi, @AHKMode
I have tested your given script and getting same results.... I think its not working because of the way VIS script was designed... Have you tried the third method??
paulpma
Posts: 39
Joined: 08 Sep 2018, 22:05

Re: Vis2 - Image to Text OCR()

22 May 2020, 09:03

I am using GDIP with VIS and other application as well and getting an error. This happends all the time when after other application has used GDIP library and when i am to manually selecting text in VIS.. Error occurs immediately after selection. See error image for details. The line of error in GDIP is the following:

Code: Select all

DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
	
which is part of function:

Code: Select all

Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality:=75)
Image

Any idea what can cause this issue?

This is the code that is executed prior to using VIS..

Code: Select all

fileName :=  A_YYYY "-" A_MM "-" A_DD "-" A_Hour "-" A_Min "-" A_Sec ".png"
pBitmap := Gdip_Startup()
pBitmap := Gdip_BitmapFromScreen()
saveFileTo := fileName                   
Gdip_SaveBitmapToFile(pBitmap, saveFileTo)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pBitmap)
the whole Gdip_SaveBitmapToFile function.

Code: Select all

Gdip_SaveBitmapToFile(pBitmap, sOutput, Quality:=75)
{
	Ptr := A_PtrSize ? "UPtr" : "UInt"
	nCount := 0
	nSize := 0
	_p := 0

	SplitPath sOutput,,, Extension
	if !RegExMatch(Extension, "^(?i:BMP|DIB|RLE|JPG|JPEG|JPE|JFIF|GIF|TIF|TIFF|PNG)$")
		return -1
	Extension := "." Extension

	DllCall("gdiplus\GdipGetImageEncodersSize", "uint*", nCount, "uint*", nSize)
	VarSetCapacity(ci, nSize)
	DllCall("gdiplus\GdipGetImageEncoders", "uint", nCount, "uint", nSize, Ptr, &ci)
	if !(nCount && nSize)
		return -2

	If (A_IsUnicode){
		StrGet_Name := "StrGet"

		N := (A_AhkVersion < 2) ? nCount : "nCount"
		Loop %N%
		{
			sString := %StrGet_Name%(NumGet(ci, (idx := (48+7*A_PtrSize)*(A_Index-1))+32+3*A_PtrSize), "UTF-16")
			if !InStr(sString, "*" Extension)
				continue

			pCodec := &ci+idx
			break
		}
	} else {
		N := (A_AhkVersion < 2) ? nCount : "nCount"
		Loop %N%
		{
			Location := NumGet(ci, 76*(A_Index-1)+44)
			nSize := DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "uint", 0, "int",  0, "uint", 0, "uint", 0)
			VarSetCapacity(sString, nSize)
			DllCall("WideCharToMultiByte", "uint", 0, "uint", 0, "uint", Location, "int", -1, "str", sString, "int", nSize, "uint", 0, "uint", 0)
			if !InStr(sString, "*" Extension)
				continue

			pCodec := &ci+76*(A_Index-1)
			break
		}
	}

	if !pCodec
		return -3

	if (Quality != 75)
	{
		Quality := (Quality < 0) ? 0 : (Quality > 100) ? 100 : Quality
		if RegExMatch(Extension, "^\.(?i:JPG|JPEG|JPE|JFIF)$")
		{
			DllCall("gdiplus\GdipGetEncoderParameterListSize", Ptr, pBitmap, Ptr, pCodec, "uint*", nSize)
			VarSetCapacity(EncoderParameters, nSize, 0)
			DllCall("gdiplus\GdipGetEncoderParameterList", Ptr, pBitmap, Ptr, pCodec, "uint", nSize, Ptr, &EncoderParameters)
			nCount := NumGet(EncoderParameters, "UInt")
			N := (A_AhkVersion < 2) ? nCount : "nCount"
			Loop %N%
			{
				elem := (24+(A_PtrSize ? A_PtrSize : 4))*(A_Index-1) + 4 + (pad := A_PtrSize = 8 ? 4 : 0)
				if (NumGet(EncoderParameters, elem+16, "UInt") = 1) && (NumGet(EncoderParameters, elem+20, "UInt") = 6)
				{
					_p := elem+&EncoderParameters-pad-4
					NumPut(Quality, NumGet(NumPut(4, NumPut(1, _p+0)+20, "UInt")), "UInt")
					break
				}
			}
		}
	}

	if (!A_IsUnicode)
	{
		nSize := DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, 0, "int", 0)
		VarSetCapacity(wOutput, nSize*2)
		DllCall("MultiByteToWideChar", "uint", 0, "uint", 0, Ptr, &sOutput, "int", -1, Ptr, &wOutput, "int", nSize)
		VarSetCapacity(wOutput, -1)
		if !VarSetCapacity(wOutput)
			return -4
		_E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &wOutput, Ptr, pCodec, "uint", _p ? _p : 0)
	}
	else
		_E := DllCall("gdiplus\GdipSaveImageToFile", Ptr, pBitmap, Ptr, &sOutput, Ptr, pCodec, "uint", _p ? _p : 0)
	return _E ? -5 : 0
}

My knowldge in AHK is not that advanced to troubleshoot this, anyone can point me out into right direction? So far I have updated my GDIP library to 1.54.. Note this occurs on WIN 7.
iseahound
Posts: 551
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: Vis2 - Image to Text OCR()

22 May 2020, 15:04

Don't call Gdip_Shutdown() and see if that works.

I wrote a new image library to do what you're doing now :)
paulpma
Posts: 39
Joined: 08 Sep 2018, 22:05

Re: Vis2 - Image to Text OCR()

22 May 2020, 23:23

iseahound wrote:
22 May 2020, 15:04
Don't call Gdip_Shutdown() and see if that works.
Yes, it worked.. I am just wondering what happened in background that created this issue?
I wrote a new image library to do what you're doing now :)
That is awesome that you can do that. I am happy for you. I am wondering why write another one if GDIP exists? My level is no where near of creating library like that or working with DLL calls, but I am learning slowly..

Thank you Iseahound for your help. I really appreciate it.
stfur
Posts: 1
Joined: 18 Jun 2020, 19:28

Re: Vis2 - Image to Text OCR()

18 Jun 2020, 19:50

Hey, is there any way to set the array of OCR but relative to the specific window?
Something like OCR([X,Y,W,H], "Notepad") or so.
Or maybe set CoordMode to Window?
tom098656
Posts: 1
Joined: 22 Jun 2020, 18:34

Re: Vis2 - Image to Text OCR()

22 Jun 2020, 18:57

stfur wrote:
18 Jun 2020, 19:50
Hey, is there any way to set the array of OCR but relative to the specific window?
Something like OCR([X,Y,W,H], "Notepad") or so.
Or maybe set CoordMode to Window?
I think the use of screen coordinates is baked into the GDip library used by Vis2. Your best bet is probably to do something like this:

Code: Select all

WindowOCR(x1,y1,x2,y2, windowName)
{
	WinActivate, %windowName%
	WinGetPos, WinX,WinY,,,,,,
	x := WinX + x1
	y := WinY + y1
	w := x2-x1
	h := y2-y1
	coords := [x,y,w,h]
	return OCR(coords)
}
andy5566888
Posts: 10
Joined: 07 May 2020, 02:20

Re: Vis2 - Image to Text OCR()

23 Jun 2020, 04:40

i have some problem i have a photo the photo is the number one in photo but the backgroud have many color, and i use ocr but it can't find any text will ouput NULL
i want to know how to ocr the number one photo

Return to “Scripts and Functions”

Who is online

Users browsing this forum: mattcrf, skydive241 and 63 guests