質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
PyAutoGUI

PyAutoGUIは、Windows、Mac OS、Linuxに対応した、Python用のGUI自動化ライブラリです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

810閲覧

pyautoGUIで、画面を認識して、画像の方向に従ったキーを押せるようにしたい

H.K2

総合スコア88

PyAutoGUI

PyAutoGUIは、Windows、Mac OS、Linuxに対応した、Python用のGUI自動化ライブラリです。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/03/05 11:07

前提・実現したいこと

以下の画像のように、画面内に出てくる矢印をもとに左から順に読み取って、これに対応するキーボードのキーを押せるようにしたいと考えています。

イメージ説明

画像の例では、↑→↑←↓↑となっていますので、その通りに
キーを押したいと考えています。

発生している問題・エラーメッセージ

上記を実現しようとして、スクリプト直下にpictureフォルダを置き、
その中に上下左右に対応するpng画像を配置し、pyautoguiのlocateAllOnScreenでリスト取り出しした後、
x軸方向の値をもとにソートして、data列(取り出した矩形領域)とdirection列(上下左右の識別文字列)を持ったdataframeを作ったのですが、ここからx軸の値をもとにソートすることができないので、
左にあるものから順に並び替えができず困っております。
dataframeから取り出す良い方法があるか、それかもっとスマートに
やりたいことができる方法があれば、ご教示いただけましたら幸甚です。

該当のソースコード

python3

1check_picture_list = [ 2 Path("picture") / Path("up.png"), 3 Path("picture") / Path("down.png"), 4 Path("picture") / Path("left.png"), 5 Path("picture") / Path("right.png"), 6] 7directions = ["up", "down", "left", "right"] 8 9tmp_result_list = [] 10tmp_direction_list = [] 11sorted_list = [] 12result_list = [] 13direction_list = [] 14result_df = pd.DataFrame([], columns=["data", "direction"]) 15for pict, direction in zip(check_picture_list, directions): 16 tmp_result_list = list(pyautogui.locateAllOnScreen(str(pict), confidence=0.98)) 17 tmp_direction_list = [direction]*len(tmp_result_list) 18 tmp_df = pd.DataFrame(list(zip(tmp_result_list, tmp_direction_list)), columns = ['data', 'direction']) 19 # tmp_df["direction"] = direction 20 print(f"pict:{pict}, tmp_df:{tmp_df}") 21 result_df = pd.concat([result_df, tmp_df])

試したこと

やりたいことをできるようにするため、pyautoGUIのリファレンスや関連URLを確認した。

補足情報(FW/ツールのバージョンなど)

python 3.9.6
pandas 1.3.0
PyAutoGUI 0.9.53

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

H.K2

2022/03/20 08:53

上記質問について、まずは↑要素だけでも取りたいと思い、下記テストコードを書きました。 generatorなので、そのままでは取り出せないのでlistに変換して、locateAllOnScreenで 取り出されるgenerator各要素の、属性xだけのlistに変換し、xの値が近い要素を削除したいです。 (Xの値が近い(差が50未満)ものは、同じ画像を多重検知しているためです) pict = Path("picture") / Path("up.png") aaa = list(pyautogui.locateAllOnScreen(str(pict), confidence=conf)) for i in range(len(aaa)): print(pyautogui.center(aaa[i]).x)
guest

回答1

0

自己解決

目的の内容は、下記コードで一部実現できました。
ただし、静止画像ではうまくいくのですが、実際のゲーム上はうまくいかないため、別の質問として提示させてください。

Python3

1def get_sequence_direction(offset=50): 2 # 画像から、上下左右の向きを順番に取得する。 3 conf = 0.75 4 check_picture_list = [ 5 Path("picture") / Path("maiou_up.png"), 6 Path("picture") / Path("maiou_down.png"), 7 Path("picture") / Path("maiou_left.png"), 8 Path("picture") / Path("maiou_right.png"), 9 ] 10 directions = ["up", "down", "left", "right"] 11 12 tmp_result_list = [] 13 tmp_direction_list = [] 14 sorted_list = [] 15 result_list = [] 16 direction_list = [] 17 result_df = pd.DataFrame([], columns=["x", "direction"]) 18 for pict, direction in zip(check_picture_list, directions): 19 tmp_result_list = list(pyautogui.locateAllOnScreen(str(pict), confidence=conf)) 20 tmp_direction_list = [direction]*len(tmp_result_list) 21 x_list = [] 22 y_list = [] 23 for pict_c in tmp_result_list: 24 center = pyautogui.center(pict_c) 25 x_list.append(center.x) 26 y_list.append(center.y) 27 28 df = pd.DataFrame(zip(x_list, y_list, tmp_direction_list), columns=["x", "y", "direction"]) 29 dfsort = df.sort_values(by="x") 30 dfsort = dfsort[(dfsort.x - dfsort.shift().fillna(0).x) > offset].copy() 31 result_df = pd.concat([dfsort, result_df]) 32 33 return result_df.sort_values(by="x")["direction"].tolist() 34 35 36# メイン処理ループ部分 37# COUNT_ENDの回数画像判定し、その通りクリックしたら抜ける。 38up_center = pyautogui.locateCenterOnScreen(str(Path("picture") / Path("maiou_push_up.png")), confidence=0.7) 39down_center =pyautogui.locateCenterOnScreen(str(Path("picture") / Path("maiou_push_down.png")), confidence=0.65) 40left_center = pyautogui.locateCenterOnScreen(str(Path("picture") / Path("maiou_push_left.png")), confidence=0.75) 41right_center = pyautogui.locateCenterOnScreen(str(Path("picture") / Path("maiou_push_right.png")), confidence=0.75) 42print(f"up:{up_center}, down:{down_center}, left:{left_center}, right:{right_center}") 43select_dir_dict ={ 44 "up": up_center, 45 "down": down_center, 46 "left": left_center, 47 "right": right_center 48} 49count = 0 50COUNT_END = 100 51while(True): 52 count = count + 1 53 dir_list = get_sequence_direction(30) 54 print(dir_list) 55 for dir in dir_list: 56 print(select_dir_dict[dir]) 57 pyautogui.moveTo(select_dir_dict[dir]) 58 time.sleep(0.05) 59 pyautogui.click() 60 time.sleep(0.05) 61 62 time.sleep(0.2) 63 64 if count > COUNT_END: 65 break 66 67

投稿2022/04/29 03:34

H.K2

総合スコア88

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問