Source code for autoease.cv.airtest_cv

# Copyright, (C) 2000-2020 Quantic Dream SA
#
# These coded instructions, statements and computer programs contain
# unpublished information, proprietary to Quantic Dream SA and are
# protected by French and EEC copyright laws. They may not be
# disclosed to third parties or copied or duplicated, in whole or in part,
# without prior written consent of Quantic Dream SA
# Unpublished-rights reserved under the Copyright Laws of the EEC.
# ===========================================================
"""
@author: meili
@contact: meili02@corp.netease.com
@file: airtest_cv.py
@date: 2022/11/21 15:49
@desc: 
"""
import os
import time

import airtest.core.api as airtest
import numpy as np
from airtest.core.api import Template as Image
import cv2

from autoease.driver_control import mouse
from autoease.core.error import TargetNotFoundError
from autoease.core.error import FileNotFoundError


[docs]def init_airtest_cv(): return airtest.connect_device("Windows:///")
def _click(v, times=1, duration=1, **kwargs): pos = airtest.exists(v, **kwargs) if pos is False: raise TargetNotFoundError(f'Picture {v} not found in screen') mouse.mouse_move_and_click(pos=pos, duration=duration, repeat=times) return pos
[docs]def click(v, duration=1, **kwargs): return _click(v, times=1, duration=duration, **kwargs)
[docs]def double_click(v, duration=1): return _click(v, times=2, duration=duration)
[docs]def exists(v, **kwargs): return airtest.exists(v, **kwargs)
[docs]def assert_exists(v, **kwargs): return airtest.assert_exists(v, **kwargs)
[docs]def assert_not_exists(v, **kwargs): return airtest.assert_not_exists(v, **kwargs)
[docs]def exists_on_local_image(template_image: airtest.Template, local_image_path: str): if local_image_path is None or not os.path.exists(local_image_path): raise FileNotFoundError(f"Local_image_path is {local_image_path}, can not be None or exist!") try: pos = _find_on_local_image(template_image, local_image_path) except TargetNotFoundError: return False else: return pos
def _find_on_local_image(template_image: airtest.Template, local_image_path: str): open_cv_image = cv2.imread(local_image_path) open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) match_pos = template_image.match_in(open_cv_image) if match_pos: return match_pos raise TargetNotFoundError(f'Picture {template_image} not found in {local_image_path}')
[docs]def exists_from_bytes(template_image: airtest.Template, screenshot_bytes: bytes): arr = np.frombuffer(screenshot_bytes, np.uint8) open_cv_image = cv2.imdecode(arr, cv2.COLOR_BGR2RGB) match_pos = template_image.match_in(open_cv_image) if match_pos: return match_pos return False
[docs]def get_device(): return airtest.device()
# def _loop_find_on_local_image(template_image: airtest.Template, # local_image_path: str, # timeout_second=3, # interval=0.5): # open_cv_image = cv2.imread(local_image_path) # open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_BGR2RGB) # # start_time = time.time() # while True: # match_pos = template_image.match_in(open_cv_image) # if match_pos: # return match_pos # # if (time.time() - start_time) > timeout_second: # raise TargetNotFoundError(f'Picture {template_image} not found in {local_image_path}') # else: # time.sleep(interval)
[docs]def bytes_2_cv_img(img_bytes): arr = np.frombuffer(img_bytes, np.uint8) open_cv_image = cv2.imdecode(arr, cv2.COLOR_BGR2RGB) return open_cv_image
if __name__ == '__main__': from airtest.core.api import Template import cv2 airtest_instance = airtest.connect_device("Windows:///") template_img = Template("C:\\Users\\meili02\\Pictures\\Xbox One Manager\\Media\\light.png") screen_image_path = "C:\\Users\\meili02\\Pictures\\Xbox One Manager\\Media\\10.227.72.44_Image_0.png" res = _find_on_local_image(template_img, screen_image_path) print(res)