LoginSignup
6
6

More than 5 years have passed since last update.

AutoIT 3で画像認識をしてみよう

Last updated at Posted at 2018-07-04

はじめに

AutoITをRPA的に使うには手法として、画像認識ができない点がネックだった。
海外のフォーラムを回ってみると、ImageSearchという外部ライブラリがあるとのことだったが、調べたところ、どうも最終メンテが2008年で止まっているらしく、場合によっては使えない可能性が高かった

環境

  • Windows 7 64bit
  • AutoIT 3.3.14.5
  • SciTE 3.4.5

調査

  • ImageSearchは入手可能か?

ImageSearchがforkされているレポジトリは発見することができた
https://github.com/MyBotRun/Libraries/tree/master/ImageSearchDLL

ただ、ビルドが通らない、フォーラムを彷徨ってみたところ、バイナリを配布している人がいたので、こちらのバイナリを拝借。

Centrally - ImageSearch Usage Explanation
https://www.autoitscript.com/forum/topic/148005-imagesearch-usage-explanation/

テストスクリプトを通してみる、スクリプト中のbtn5.bmp ( btn5.png )は画面をキャプチャして作ったもの。

test_img_search.au3
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         @hirohiro77 (https://qiita.com/hirohiro77)

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <ImageSearch.au3>

HotKeySet("p", "checkForImage") ; ホットキーを設定、pを押すと関数checkForImageを実行
HotKeySet("e", "endScript") ; ホットキーを設定、eを押すと関数endScriptを実行

global $y = 0, $x = 0

Run ("calc.exe") ; 電卓を実行
Sleep (200) ; 無条件待ち 200 ms
WinWait("電卓") ; 電卓のウインドウ待ち

Func checkForImage()
;Local $search = _ImageSearch('btn5.bmp', 0, $x, $y, 0) ; 0は画像の左上
Local $search = _ImageSearch('btn5.bmp', 1, $x, $y, 0) ; 1は画像の中央
 If $search = 1 Then
  MouseMove($x, $y, 10) ; 10は移動速度
  MouseClick("left", $x, $y , 1 , 10) ; 1はクリック数,10は動作速度
 EndIf
EndFunc

Func endScript()
 Exit 0 ;
EndFunc

while 1
sleep(200) ; 実行待機ループ  無条件待ち 200 ms タスクバーに常駐
WEnd

まず、32Bitバイナリを展開し、作ったサンプルスクリプト(test_img_search.au3)と同じパスにおいて
Run Script (x86)でスクリプトを実行してみるも、クラッシュして走らず

  問題イベント名:    APPCRASH
  アプリケーション名:  AutoIt3.exe
  アプリケーションのバージョン:   3.3.14.5
  アプリケーションのタイムスタンプ: 5aaa71bf
  障害モジュールの名前:   AutoIt3.exe
  障害モジュールのバージョン:  3.3.14.5
  障害モジュールのタイムスタンプ:    5aaa71bf

次に、64Bitバイナリを展開し、作ったサンプルスクリプト(test_img_search.au3)と同じパスにおいて
Run Script (x64)でスクリプトを実行してみたところ、動作を確認した。
以下のGifアニメのように、Pキーを押すごとに、マウスカーソルが btn5.png のボタンを認識、移動してクリックしているのがわかる

run_x64.gif

  • ImageSearchDLLによる追加された関数について
関数名 用途
_ImageSearchArea 指定範囲内で画像検索
_ImageSearch 画面内で画像検索
  • 用法

Values to put in for _ImageSearch function (entire screen search)

_ImageSearch($findImage,$resultPosition,ByRef $x, ByRef $y,$tolerance, $HBMP=0)

Values to put in for _ImageSearchArea function (you declare part of screen to be searched)

_ImageSearchArea($findImage,$resultPosition,$x1,$y1,$right,$bottom,ByRef $x, ByRef $y, $tolerance,$HBMP=0)
; Description:      Find the position of an image on the desktop
; Syntax:           _ImageSearchArea, _ImageSearch
; Parameter(s):
;                   $findImage - the image to locate on the desktop
;                   $tolerance - 0 for no tolerance (0-255). Needed when colors of
;                                image differ from desktop. e.g GIF
;                   $resultPosition - Set where the returned x,y location of the image is.
;                                     1 for centre of image, 0 for top left of image
;                   $x $y - Return the x and y location of the image
;
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
;
; Note: Use _ImageSearch to search the entire desktop, _ImageSearchArea to specify
;       a desktop region to search

とりあえず、BMP限定ながらも、画像検索ができることは確認できた。
気になるのはtolerance(公差)が色にしか適用できないのかが気になる…検証する機会があったらやってみようと思う

6
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6