Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OCR
#11
Now I tested tesseract 4.0.0 alpha. Works almost without errors, even with very small text, without resizing. Now don't need FreeImage.

Download/install: https://github.com/UB-Mannheim/tesseract/wiki


Macro tesseract 4.0.4 beta
Code:
Copy      Help
;Tested 2018-06-20 with tesseract 4.0.0 alpha 64-bit.
;Download/install: https://github.com/UB-Mannheim/tesseract/wiki
;This macro uses the installed command-line program tesseract.exe.

out
;change these values if need
str fTes.expandpath("$Program Files$\Tesseract-OCR\tesseract.exe")
int captureNow=1 ;;if 0, will use previously captured file

;---------------------

str language;;="lit" ;;note: for non-English languages use RunConsoleUTF8 instead of RunConsole2. And probably the language must be installed when installing tesseract-ocr. And in Options/General must be checked 'Unicode'.
str fBmp.expandpath("$temp$\qm_tesseract.bmp") ;;this macro captures screen image and saves it in this temporary file

;capture screen image and save in file fBmp
if captureNow
,if(!CaptureImageOrColor(0 0 _hwndqm fBmp)) ret
,;if(!CaptureImageOnScreen(610 400 800 45 fBmp)) ret

PerfFirst ;;measure speed

;OCR (convert image to text)
str cl.format("''%s'' ''%s'' stdout" fTes fBmp) so
if(language.len) cl+F" -l {language}"
;out cl
if(RunConsole2(cl so "" 0x200)) end so ;;for non-English languages use RunConsoleUTF8 instead

PerfNext ;;measure speed
PerfOut ;;measure speed
out so ;;show results

Function RunConsoleUTF8
Code:
Copy      Help
;/
function# $cl [str&sout] [$curDir] [flags] ;;flags: 1 show window, 0x100 correct newlines, 0x200 exclude stdError

;Runs a console program, waits and captures its output.
;Returns the exit code of the process.
;Error if fails or the file does not exist.

;cl - program name or full path, optionally followed by command line parameters.
;;;Must be exe, not a document.
;;;Example: "$desktop$\folder\program.exe /a ''c:\new folder\file.txt''".
;;;Program path should be enclosed in quotes if contains spaces.
;;;QM 2.4.2. Expands path even if program path is enclosed in quotes. Example: "''$my qm$\an.exe'' /a".
;sout  - str variable that receives the output. If omitted or 0, displays in QM output pane.
;curDir - current directory for the program.
;flags:
;;;0-255 - console window show state, like with ShowWindow API. If 0 (default), it is hidden.
;;;0x100 (QM 2.3.5) - replace nonstandard newlines in output. For example, replaces "line1[10]line2[13]" with "line1[]line2[]".
;;;0x200 (QM 2.4.4) - don't get standard error output.

;REMARKS
;Expands special folder string in program's path and in curDir, but not in command line arguments.
;While waiting, this thread cannot receive window/dialog messages, COM events, hooks. If need, call this function from separate thread (mac).

;If current process is a QM console exe and you want the child process to use its console (like cmd.exe does), instead use <help>_spawnl</help> or similar function, or <help>CreateProcess</help>. See example.

;EXAMPLES
;if(RunConsoleUTF8("schtasks.exe /?")) out "failed"


str sout2; if(&sout) sout.all; else &sout=sout2

;create pipe
__Handle hProcess hOutRead hOutWrite
SECURITY_ATTRIBUTES sa.nLength=sizeof(SECURITY_ATTRIBUTES); sa.bInheritHandle=1

if(!CreatePipe(&hOutRead &hOutWrite &sa 0)) end "" 16
SetHandleInformation(hOutRead HANDLE_FLAG_INHERIT 0)

;create process
PROCESS_INFORMATION pi
STARTUPINFOW si.cb=sizeof(STARTUPINFOW)
si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW
si.hStdOutput=hOutWrite
if(flags&0x200=0) si.hStdError=hOutWrite
si.wShowWindow=flags&255
str s1 s2
if(cl and cl[0]=34) s1.expandpath(cl+1); s1-"''"; else s1.expandpath(cl)
if(!empty(curDir)) s2.expandpath(curDir)

if(!CreateProcessW(0 @s1 0 0 1 CREATE_NEW_CONSOLE 0 @s2 &si &pi)) end "" 16

hOutWrite.Close
CloseHandle(pi.hThread)
hProcess=pi.hProcess

;read pipe
s1.all(10000)
int r
rep
,if(!PeekNamedPipe(hOutRead 0 0 0 &r 0)) break ;;makes easier to end thread etc
,if(r<10000) 0.01
,if(!r) continue
,if(!ReadFile(hOutRead s1 10000 &r 0)) break
,sout.geta(s1.lpstr 0 r)

int ec
if(!GetExitCodeProcess(hProcess &ec)) ec=-1000

if sout.len
,;OemToChar sout sout ;;convert from OEM character set. Use A because W incorrectly converts newlines etc.
,;if(_unicode) sout.unicode(sout 0); sout.ansi
,if(flags&0x100) sout.replacerx("\r(?!\n)" "[]"); sout.replacerx("(?<!\r)\n" "[]") ;;single \r or \n to \r\n
,if(&sout=&sout2) out sout

ret ec

;TODO: support 64-bit System folder, like run does


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)