"if" with image recognition

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
jerb67
Newbie
Posts: 7
Joined: Thu May 03, 2018 11:27 am

"if" with image recognition

Post by jerb67 » Thu May 03, 2018 2:53 pm

Hello everyone,

I have a little problem,
There are 4 images of which one different from the others, I have to click on this image, when I do it, there is a new page which appears and the images change of place, but sometimes, there is a lag and it clicks but nothing happens, you have to click again a second time ..

here's the picture:
https://imgur.com/1JJaWUO

I had thought, inserted an "if" statement,
so if it clicks and the image changes (since the new page is out) it continues, but if the page did not appear, it would re-click a second time.

How can I do this ( if it's possible ) ?
Thank you !

Here's my code :

Label>start

//Wait for
let>WSI_TIMEOUT=210
WaitScreenImage>%BMP_DIR%\image_4.bmp,0.7,CCOEFF


Wait>2

ScreenCapture>36,360,380,580,%TEMP_DIR%\screenrect.bmp
//Find and Left Click Center of
FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
MouseMove>{%XArr_0%+36},{%YArr_0%+360}
LClick
Endif

WaitRectChanged>43,404,40,403,5

(( the "if" would be here ))

Wait>40

SetFocus>KIICAA POWER
CapsOff
Press Esc

Goto>start

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: "if" with image recognition

Post by Marcus Tettmar » Fri May 04, 2018 11:40 am

Not sure I understand what you want to check for. You can do any kind of logical "if" condition. You can use FindImagePos and then use If to determine what to do based on whether anything was found.

WaitRectChanged waits. It doesn't return anything so there's nothing to compare. However, you can set a time out and then use If to check whether it timed out or not by looking at the WRC_RESULT variable.

I'm not sure if this helps you. It's hard to follow what you want to do and what your "if" condition should be looking at.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

jerb67
Newbie
Posts: 7
Joined: Thu May 03, 2018 11:27 am

Re: "if" with image recognition

Post by jerb67 » Fri May 04, 2018 12:59 pm

Thank you very much for answering my question !
I'm sorry if it wasn't clear enough,

currently, I managed to make sure that it detects the right logo and clicks on it thanks to the "Image Recognition Wizard"
it's the white logo, and he randomly change his place every time : https://imgur.com/1JJaWUO

Code: Select all

ScreenCapture>36,360,380,580,%TEMP_DIR%\screenrect.bmp
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+36},{%YArr_0%+360}
  LClick
Endif
But sometimes, when he clicks on the logo he changes his place (randomly) and do nothing, instead of opening a new tab, then he remains stuck on the spot (the script does not continue anymore) ..

I had thought to put an "if" so that it checks that the new page has opened well ( and go to "wait>40" of the script), and if not, re-clicked again on the right logo, the white one ( go to "wait>2" of the script).

Here is what I try to do with the "if":

Code: Select all

If>findImagePos>C:\User\Nancy\Document\Macro Sheduler 14\BLU\screencapture.bmp,SCREEN,0,1,XArr,YArr,Numfound,CCOEFF

  goto>wait>40

ELSE

  goto>Wait>2
and this is the full script :

Code: Select all

Label>start

//Wait for 
let>WSI_TIMEOUT=210
WaitScreenImage>%BMP_DIR%\image_4.bmp,0.7,CCOEFF


Wait>2

ScreenCapture>36,360,380,580,%TEMP_DIR%\screenrect.bmp
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+36},{%YArr_0%+360}
  LClick
Endif

If>findImagePos>C:\User\Nancy\Document\Macro Sheduler 14\BLU\screencapture.bmp,SCREEN,0,1,XArr,YArr,Numfound,CCOEFF

  goto>wait>40

ELSE

  goto>Wait>2


Wait>40

SetFocus>KIICAA POWER
CapsOff
Press Esc

Goto>start
I don't really know what to do ! I hope I was clearer than the first time!

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: "if" with image recognition

Post by Marcus Tettmar » Fri May 04, 2018 1:04 pm

The first thing I would do is put a wait between the MouseMove and the LClick :

Code: Select all

FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+36},{%YArr_0%+360}
  Wait>1
  LClick
Endif
Then you could wait a bit and then look for the image to see if it is still there. You could just loop back:

Code: Select all

Label>check_again
FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+36},{%YArr_0%+360}
  Wait>1
  LClick
  Goto>check_again
Endif
What this will do is keep checking if the image is there. If it is it will click on it again. If it's not the script will simply continue.

You can't use If>FindImagePos. That makes no sense. FindImagePos returns a value - in the above case NumFound. We use If> to check if NumFound is larger than zero.

You could either do the FindImagePos again and then do another If>NumFound>0 or just loop back around like in my example. So it will keep looking and trying until the image is no longer found.

You may not want an infinite loop like this so you could add a counter and make it only try a few times. But save that until you've got something working.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

jerb67
Newbie
Posts: 7
Joined: Thu May 03, 2018 11:27 am

Re: "if" with image recognition

Post by jerb67 » Fri May 04, 2018 1:22 pm

Oh! Thank you so much !

But I forgot to say that the white logo only randomly change when we click on him, so the problem here, is that sometime even when we click on him, it's doing nothing exept changing his place, and we have to click on him again for opening the new tab.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: "if" with image recognition

Post by Marcus Tettmar » Fri May 04, 2018 1:25 pm

Well, FindImagePos *FINDS* the image. That's the whole point. Doesn't matter where it is. So if changes place, when it loops back and looks again it will find it in the new place. My suggestion works for what you are describing.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

jerb67
Newbie
Posts: 7
Joined: Thu May 03, 2018 11:27 am

Re: "if" with image recognition

Post by jerb67 » Fri May 04, 2018 1:38 pm

I'm so sorry, I didn't understand at first !
Thank you so much for your precious help !

jerb67
Newbie
Posts: 7
Joined: Thu May 03, 2018 11:27 am

Re: "if" with image recognition

Post by jerb67 » Fri May 04, 2018 1:43 pm

I have a another problem now, I added this :

Code: Select all

Label>check_again
FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+36},{%YArr_0%+360}
  Wait>1
  LClick
  Goto>check_again
Endif
But now, he click on the right logo but it doesn't continue the script anymore.. I did something wrong ?

Code: Select all

Label>start

//Wait for 
let>WSI_TIMEOUT=210
WaitScreenImage>%BMP_DIR%\image_4.bmp,0.7,CCOEFF


Wait>2

Label>check_again
FindImagePos>%BMP_DIR%\image_5.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+36},{%YArr_0%+360}
  Wait>1
  LClick
  Goto>check_again
Endif

Wait>40

SetFocus>KIICAA POWER
CapsOff
Press Esc

Goto>start

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: "if" with image recognition

Post by Marcus Tettmar » Fri May 04, 2018 4:23 pm

Perhaps it is looping indefinitely. What does the debugger show?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

jerb67
Newbie
Posts: 7
Joined: Thu May 03, 2018 11:27 am

Re: "if" with image recognition

Post by jerb67 » Fri May 04, 2018 7:55 pm

Hey !
Thank you for your answer ! ( but I don't know what the debugger is.. sorry I'm new to this )

I have found another way to verify that the script click on the right white logo everytime,
I have put a "waitrectchanged" with a 'if' :

Code: Select all

label>FIP

Let>WSI_TIMEOUT=210
//Wait for 
WaitScreenImage>%BMP_DIR%\image_6.bmp,0.7,CCOEFF

wait>2

ScreenCapture>29,340,394,579,%TEMP_DIR%\screenrect.bmp
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_2.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+29},{%YArr_0%+340}
  LClick
Endif

Label>WRC
Let>tmp0=45,124
Let>tmp1=391,126
Let>tmp2=33,288
Let>tmp3=400,288
WaitRectChanged>tmp0,tmp1,tmp2,tmp3,3
if>NumFound>0
  goto>FIP
Else
  goto>Wait
endif

It works perfectly fine,I told him with the "waitrectchanged"and the "if" that if a selected part of the screen stays the same ( because the new tab did not open ) it start again from the beginning ( goto>FIP )

Code: Select all

Label>WRC
Let>tmp0=45,124
Let>tmp1=391,126
Let>tmp2=33,288
Let>tmp3=400,288
WaitRectChanged>tmp0,tmp1,tmp2,tmp3,3
if>NumFound>0
  goto>FIP
Else
  goto>Wait
 endif 
And it worked, but the problem, is that when the new page is up, he did not continu the script..
It looks like he's blocked at the "ELSE" part..

Did I do something wrong with the "if" ?

full script :

Code: Select all

//Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
//If using IGNORESPACES quote strings in {" ... "}
//Let>IGNORESPACES=1


label>FIP

Let>WSI_TIMEOUT=210
//Wait for 
WaitScreenImage>%BMP_DIR%\image_6.bmp,0.7,CCOEFF

wait>2

ScreenCapture>29,340,394,579,%TEMP_DIR%\screenrect.bmp
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_2.bmp,%TEMP_DIR%\screenrect.bmp,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>{%XArr_0%+29},{%YArr_0%+340}
  LClick
Endif

Label>WRC
Let>tmp0=45,124
Let>tmp1=391,126
Let>tmp2=33,288
Let>tmp3=400,288
WaitRectChanged>tmp0,tmp1,tmp2,tmp3,3
if>NumFound>0
  goto>FIP
Else
  goto>Wait
endif

Label>Wait
wait>40

SetFocus>samsung_SM-J320F_RV8J9043G2K - TeamViewer
CapsOff
Press Esc


User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: "if" with image recognition

Post by Marcus Tettmar » Sat May 05, 2018 9:37 pm

Your NumFound has nothing to do with your WaitRectChanged. NumFound is being set by the FindImagePos line further up and has not changed.

Please see:
https://help.mjtnet.com/article/7-using-the-debugger
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts