The Bytecode Club
Log inJoin the club
Video Game Reverse Engineering

Developing Bot

Started by Antidote · Dec 5, 2014

1,033 replies21,297,466 viewsLocked
Dec 11, 2014#31

@odySSeys: Glad it works for you! I also experienced random 1 added in front of the result, though not very often. I think this has to do with the screen region you choose for tesseract to read from. It seems to read much more accurately when you choose a bigger region, but then you'll have to deal with extra characters, and the patterns are not always 100% consistent to realiably extract number from. For your case, try to edit the $left value in the script, change it from 42 to 45 or sth and then test again. It will reduce the region a bit to the right to remove the extra 1. My BS resolution is also 800x600, but I did get the script to detect resolution and auto adjust so resolution should not be a problem. My reading is also not perfect, about 90%. Hope everyone can try and tweak around until we can find the optimal region for reading 🙂

@HAX4DAYS: My guess is that it has to do with your tesseract. In the folder I provided above, I put Tesseract-OCR folder inside and modify the shell command inside Tesseract.au3 into

ShellExecuteWait("Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename)

so that it will use Tesseract in that folder instead of the one installed. Then in the script I called #include "Tesseract.au3" so it will include the file Tesseract.au3 in that folder instead of the one in Autoit3/Include.

Alternatively, to use the one installed on your computer, make sure the following:

  1. Copy this Tesseract.au3 I provided into Autoit3/Include
    https://mega.co.nz/#!VwERWCKC!naMcu0iyGLOY-XqlABKVNYC3dsx_u6jhrgJ3-7Eud34

  2. Look for tesseract.exe in your Tesseract-OCR installed folder, either "C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" or "C:\Program Files\Tesseract-OCR\tesseract.exe"

  3. Open Tesseract.au3 in Autoit3/Include and look for this line

ShellExecuteWait(@ProgramFilesDir & "\tesseract\tesseract.exe", $capture_filename & " " & $ocr_filename & " " & "-l coc" & " " & "digits", "", "", @SW_HIDE)

Replace @ProgramFilesDir & "\tesseract\tesseract.exe" with your absolute path to tesseract.exe (in number 2)

  1. Make sure coc.traineddata is inside your Tesseract-OCR/tessdata

  2. Make sure it is #include <Tesseract.au3> in the script, not #include "Tesseract.au3"

  3. That's it, your script should work now 🙂

Dec 11, 2014#32

Hi All,
Antidote thanks for your posts. They've been very helpful.
I just had a query relating to coc.traineddata. How did you go about making it? Can it be improved?
I also had a similar issue where it would add numbers or not quite work properly and just trying to fine tune it even more.
Cheers!

Sorry probably should have googled a bit harder first: http://blog.cedric.ws/how-to-train-tesseract-301

Let me know if im on the wrong track, or specifically how you did it.

Cheers

Dec 11, 2014#33

Sorry being real lazy here as new to AutoIT.

Is there a way to easily show the captured bitmap/png in a popup box to compare what the script sees vs what the OCR data is returning?

Will help with adjusting the capture window.

Cheers and sorry for the lazy q's in advance.

Dec 11, 2014#34

@Antidote, I have tried unistall the tesseract installer and purely use the file that you compiled. The result, it pause after clicked search. I mean it paused at the window "Press Space Bar to Stop" and no gold and elixer amount.
After i installed the tesseract installer, everything back to normal. Somehow the installer have registry that effected the tesseract file.

Outcome:
I did try reducing the screen region to

Global $Left = 50
Global $Top = 90
Global $Right = 720
Global $Bottom = 550

and it crash with error as shown below

"C:\Users\user\Downloads\TesseractTest\Test.au3" (53) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Isn't it suppose to be much more accurate when reduced screen region. The larger the screen region, the more unnecessary info will be captured.

Predict
Somehow, I believe the screen region isn't the main issue. The issue is from the traindata. I believe need to train tessaract to know gold icon, elixer icon because when I enlarge the screen region, tessaract treated the gold icon as 2.

@BoredCoder, did u installed tesseract installer or just purely used the antidote compiled file?

Dec 11, 2014#35

Hi odySSeys,

I did both... I have tesseract installed still, but am just using the package because i couldn't be stuffed changing all the paths and parameters in the installed version.
If you're having problems still, make sure you change the temp path to a writeable location. C:\ isn't writeable unless running the script as administrator on Win7/8, so you might want to change it to mydocs or something like that.

So in Tesseract.au3 file find the bit:

Global $tesseract_temp_path = "C:"

Change to

Global $tesseract_temp_path = "C:\Users\yourusername\Documents"

Where yourusername is your user profile folder name.

That might fix your problem.

Cheers

Dec 11, 2014#36

@odySSeys: Yea I think you're right, so you still need Tesseract installed anw. Wonder if there's any Tesseract dll or library we can call with autoit?
The crash message is because when I use my indent: 45, 70, 660, 490, the raw output produced will look something like this

Although there are lots of extra number, the pattern is very consistent, which is random number, blank lines, gold, blank lines, elixir .. dark .. trophy .. random number .. So I strip out all blank lines, spaces,... using StringRegExpReplace and get a few lines of number, I then get gold from the second line and elixir from the third line. Your indent is different meaning your output is different so it will likely return error. You might try to display raw output first and see if you can get a consistent pattern? I tried narrow down to only the region with gold & elixir but sometimes Tesseract return nothing or very poor reading. In my experience if there's other text around it will get the number reading more accurately, that's why I choose a bigger region. If anyone can improve tesseract traineddata or find better screen region it'd be great!

@BoredCoder: I followed this guide to train tesseract: https://code.google.com/p/tesseract-ocr/wiki/TrainingTesseract3
However, I found out this tool which makes training much faster, it helps you create tif and box file and automate training for you
http://vietocr.sourceforge.net/training.html
The official guide is rather lengthy but quite straight forwards, for the jTessBoxEditor tool, you need to install the font in your system first, then open the tool, go to create box file, choose font, import txt or type in something, it will generate tif & box file for you. Then you have to prepare wordlist file, font properties file, ... and let it does training for you. I'm just talking out of memory so if you're stuck any where just let me know and I'll write a detailed guide.
To show captured image, change the last 0 in "$Read = _TesseractWinCapture($HWnD,"",0,"",1,2,$Left*$x_ratio,$Top*$y_ratio,$Right*$x_ratio,$Bottom*$y_ratio,0)" and replace with 1. However, it will return error "Variable must be of Object type" so I managed to fix it by opening Tesseract.au3, look for "$obj1" and replace that function with this one:

$Obj1 = ObjCreate("WMPlayer.OCX") ;NEW OBJECT REPLACING ORIGINAL IN UDF
 $Obj1_ctrl = GUICtrlCreateObj($Obj1, 0, 0, 640, 480)
 With $Obj1
            .URL = $capture_filename
            .fullScreen = True
            .windowlessVideo = True
            .stretchToFit = True
            .enableContextMenu = True
            .enabled = True
            .uiMode = "full"; none / mini full
            .settings.autostart = True
            .settings.mute = False
            .settings.volume = 100; 0 - 100
            .settings.Balance = 0; -100 to 100
Dec 11, 2014#37

I made a very simple GUI for the script, when started it asks you to click on barrack to record position then train troops according to your input and proceed to attack. Gonna start with attack function and image compare soon.

Btw anyone knows how to interupt a running script? something like togglePause function but using GUI button and not Hot Key?

Dec 11, 2014#38

Hello though I just registered on this site I have some time following this great work , I've signed nothing more than to give my congratulations and let them know that the lf bot robot in directotio windows creates two folders:
1 c: windows \ coc
2 c: windows \ coc - in \

In these two folders there are more folders and in them there are terminated coc files:
example ejemplo.coc

Well arrived here these files can be opened with the paint from windows giving as a result catches couple makes lf robot program

I hope you understand my English is the translator of google

thank you

Dec 11, 2014#39

@Antidote, you missed the "EndWith" at the end of "with $Obj1".

So far, the script reduced the chances of showing "1" at the front of the figure. However, random number appear at the back of the figure.

After couple of try, the tesseract installer has nothing to do with the executing. After I uninstall the tesseract, I copied your packaged tesseract-ocr and move into program folder. Add @ProgramFilesDir into the tesseract.au3 file and it work perfectly fine. This i can conclude is that the folder properties play a big role. Will try @ScriptDir instead.

Dec 11, 2014#40

Predict
Somehow, I believe the screen region isn't the main issue. The issue is from the traindata. I believe need to train tessaract to know gold icon, elixer icon because when I enlarge the screen region, tessaract treated the gold icon as 2.

I don't think Tesseract can read icon, you have to do image compare to see if the image match. I believe that is how lazypressing and lf robot do to recognize barrack, army camp, town hall,.. but I think you're right about traineddata not good enough. I'm not sure how to improve it further though. Also because I use the "digits" parameter it will only return digits no matter what the image is.

@Lobo: thanks for the info, all those files are bitmap of various buildings in clash of clans it use to compare, there's a similar folder created by lazypressing as well. I'm still reading on it but I think I'm gonna sleep now. This stuff is fun but takes too much time lol

This discussion is closed.