Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OCR
#1
Function Function1
Code:
Select All      Help
function

str s1
str fBmp="$temp$\qm_modi.bmp"

if(scan("resource:<Image>image:h1B7A5E0B" w 0 16))
,fBmp.expandpath
,CaptureImageOnScreen(655 340 85 35(fBmp))
,typelib MODI {A5EDEDF4-2BBC-45F3-822B-E60C278A1A79} 13.0
,MODI.Document doc._create
,doc.Create(fBmp)
,doc.OCR(MODI.miLANG_ENGLISH -1 0)
,MODI.Image im=doc.Images.Item(0)
,s1=im.Layout.Text

This was my original OCR code. However, I have a 2 minor issues with this code.

1. I want to capture a image based on where the previous image is located. E.g. image:h1B7A5E0B is located at x: 620, y: 320 I want it to capture the location x:620, y: 320+50. What do I change to do this?

2. I've issues with MODI ever since I upgraded my operating system to windows 10. Sharepoint 2007 cannot be installed anymore. I found a open sources OCR https://github.com/tesseract-ocr/tesseract but I need some guide on how to in-cooperate it into the code.
#2
Macro Macro2761
Code:
Select All      Help
str s1
str fBmp="$temp$\qm_modi.bmp"

RECT r
if(scan("resource:<Image>image:h1B7A5E0B" w r 16|128))
,fBmp.expandpath
,OffsetRect &r 0 50
,CaptureImageOnScreen(r.left r.top r.right-r.left r.bottom-r.top fBmp)
,typelib MODI {A5EDEDF4-2BBC-45F3-822B-E60C278A1A79} 13.0
,MODI.Document doc._create
,doc.Create(fBmp)
,doc.OCR(MODI.miLANG_ENGLISH -1 0)
,MODI.Image im=doc.Images.Item(0)
,s1=im.Layout.Text

2. Tesseract does not OCR screen text well. At least when I tested it last time, 1 year ago. It works well only with bigger font text. We can resize captured screen text, but OCR results are still poor. MODI is much better. I have the test macro, can post if you need.

For example, I tried to OCR your post with tesseract.
Results without resizing, time 7.5 s:
Code:
Select All      Help
miswasutyarigitaiocncode.ttmever,tttaveazmiturissrresvdnrnriscode.

I I want [a capture a iuage based on were me previms iuage is located. Lg. iuage:trttt7A5(ott is located at x: 510, y: 310 I want. [a capture the hcatiuu x:610,y:310-ó50.wltaldnldtange
[a do IItis?

1. We issues wilIt wont ever sitce I upgraded my npemt'llg system In witdrms I0. sttarepairt 1007 carmtbe itslaled anymore. I '01-GÇ£ a open Sources ocn Films:llgimubmnmliesseracr
acr/tesseractbut I need some guide on him In 'lirooopemle it itln the code.
Results when resized 3 times, time 0.9 s:
Code:
Select All      Help
This was my original OCR code. However, I have a 2 minor issues with this code.

1 . I want to capture a image based on where the previous image is located. E.g. image:h1B7A5EOB is located at x: 620, y: 320 I want it to capture the location x:620, y: 320460. What do I diange
to do this?

2. I've issues with MODI ever since I upgraded my operating system to windows 10. Sharepoint 2007 cannot be installed anymore. I found a open sources OCR https:llgithub.comltesseract-
ocrltesseract but I need some guide on how to in-cooperate it into the code.
#3
Will be great if you can share the code as well. I'll play around with it. Thanks!
#4
And for the rectangle. Is it possible change the capture size of the rectangle? E.g. It's 300x100 at the start I need to to be 200x80.
#5
Change r.right etc with operator + or -. Or use function InflateRect.
#6
EDIT: this is obsolete. New version: http://www.quickmacros.com/forum/showthr...9#pid31529
-----------------------

1. Download/install tesseract. I downloaded from here: https://github.com/UB-Mannheim/tesseract/wiki
2. Download/import FreeImage: Image manipulation with FreeImage graphics library

Macro tesseract
Code:
Select All      Help
;Tested 2017-03-15 with the latest tesseract version 3.05-dev.
;Too much OCR errors to be useful when the image is captured from screen, with small font.
;MODI works without errors with same image.

out
;change these values if need
str fTes.expandpath("$Program Files$\Tesseract-OCR\tesseract.exe")
int scale=3 ;;how much to resize. Tesseract is very sensitive to text size. Usually with 2-4 works best. Fastest 2, slightly slower 3, then 4, slowest 1.
int captureNow=1 ;;if 0, will use previously captured file

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

str language;;="lit" ;;does not work
str fBmp.expandpath("$temp$\qm_tesseract.bmp") ;;this macro captures screen image and saves it in this temporary file
str fBmp2.expandpath("$temp$\qm_tesseract2.bmp") ;;this macro resizes the image, saves in this file, and passes this file to tesseract.exe

;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
;resize with FreeImage. Also tried GflAx, worse.
#compile "__FreeImage"
if scale>1
,FI_ShowMoreErrorInfo ;;optional
,FiBitmap x
,x.Load(FIMG.FIF_BMP fBmp)
,int newWidth=FIMG.FreeImage_GetWidth(x)*scale
,int newHeight=FIMG.FreeImage_GetHeight(x)*scale
,;FiBitmap x2.Attach(FIMG.FreeImage_Rescale(x newWidth newHeight FIMG.FILTER_BSPLINE)) ;;too much filtering
,;FiBitmap x2.Attach(FIMG.FreeImage_Rescale(x newWidth newHeight 0)) ;;FIMG.FILTER_BOX, bad OCR
,FiBitmap x2.Attach(FIMG.FreeImage_Rescale(x newWidth newHeight FIMG.FILTER_LANCZOS3)) ;;good
,
,FIMG.FreeImage_AdjustContrast(x2 10)
,
,fBmp=fBmp2
,x2.Save(FIMG.FIF_BMP fBmp)
,PerfNext ;;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
PerfNext ;;measure speed
PerfOut ;;measure speed
out so ;;show results

ret ;;disable this line if want to see the image passed to tesseract.exe

str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 1000 500 "Shows the resized image for 3 seconds"
;3 Static 0x5400000E 0x0 0 0 1158 174 ""
;END DIALOG
;DIALOG EDITOR: "" 0x2040308 "*" "" "" ""

str controls = "3"
str sb3
sb3=fBmp
if(!ShowDialog(dd &sub.DlgProc &controls)) ret


#sub DlgProc
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,int x y w h
,GetWorkArea x y w h
,MoveWindow hDlg x y w h 0
,SetTimer hDlg 1 3000 0
,case WM_TIMER
,clo hDlg
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1
#7
When I install and run this macro, I get an error message saying 'item not found.' referring to the line #compile "__FreeImage"

I put FreeImage.dll in this location C:\Program Files (x86)\Quick Macros 2\FreeImage.dll

what did i do wrong?
#8
Download and import the attachment.
http://www.quickmacros.com/forum/showthr...p?tid=5046
#9
thanks. I didn't notice that attachments. works really well.
#10
Actually if there is still the same problem if you set the scale variable is > 1 even after the attachment is downloaded.
#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:
Select All      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:
Select All      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


Forum Jump:


Users browsing this thread: 1 Guest(s)