def snapshot(self, filename=None): """ Take a screenshot and save it in ST.LOG_DIR folder Args: filename: name of the file to give to the screenshot, {time}.jpg by default Returns: display the screenshot """ if self.handle: screen = screenshot(filename, self.handle) else: screen = screenshot(filename) if self.app: rect = self.get_rect() screen = aircv.crop_image(screen, [rect.left, rect.top, rect.right, rect.bottom]) if not screen.any(): if self.app: rect = self.get_rect() screen = aircv.crop_image(screenshot(filename), [rect.left, rect.top, rect.right, rect.bottom]) if self._focus_rect != (0, 0, 0, 0): height, width = screen.shape[:2] rect = (self._focus_rect[0], self._focus_rect[1], width + self._focus_rect[2], height + self._focus_rect[3]) screen = aircv.crop_image(screen, rect) if filename: aircv.imwrite(filename, screen) return screen
def snapshot(self, filename=None, quality=10, max_size=None): """ Take a screenshot and save it in ST.LOG_DIR folder Args: filename: name of the file to give to the screenshot, {time}.jpg by default quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: display the screenshot """ if self.handle: screen = screenshot(filename, self.handle) else: screen = screenshot(filename) if self.app: rect = self.get_rect() screen = aircv.crop_image( screen, [rect.left, rect.top, rect.right, rect.bottom]) if not screen.any(): if self.app: rect = self.get_rect() screen = aircv.crop_image( screenshot(filename), [rect.left, rect.top, rect.right, rect.bottom]) if self._focus_rect != (0, 0, 0, 0): height, width = screen.shape[:2] rect = (self._focus_rect[0], self._focus_rect[1], width + self._focus_rect[2], height + self._focus_rect[3]) screen = aircv.crop_image(screen, rect) if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def snapshot(self, filename="tmp.png"): """ Take a screenshot and save it to `tmp.png` filename by default Args: filename: name of file where to store the screenshot Returns: display the screenshot """ if not filename: filename = "tmp.png" if self.handle: screen = screenshot(filename, self.handle) else: screen = screenshot(filename) if self.app: rect = self.get_rect() screen = aircv.crop_image(screen, [rect.left, rect.top, rect.right, rect.bottom]) if self._focus_rect != (0, 0, 0, 0): height, width = screen.shape[:2] rect = (self._focus_rect[0], self._focus_rect[1], width + self._focus_rect[2], height + self._focus_rect[3]) screen = aircv.crop_image(screen, rect) if filename: aircv.imwrite(filename, screen) return screen
def _find_keypoint_result_in_predict_area(self, func, image, screen): G.LOGGING.debug("teststtsstt") if not self.record_pos: return None # calc predict area in screen image_wh, screen_resolution = aircv.get_resolution( image), aircv.get_resolution(screen) xmin, ymin, xmax, ymax = Predictor.get_predict_area( self.record_pos, image_wh, self.resolution, screen_resolution) G.LOGGING.debug("111111111111111111111try match with %s" % xmin) # crop predict image from screen predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) if not predict_area.any(): return None # keypoint matching in predicted area: ret_in_area = func(image, predict_area, threshold=self.threshold, rgb=self.rgb) # calc cv ret if found if not ret_in_area: return None ret = deepcopy(ret_in_area) if "rectangle" in ret: for idx, item in enumerate(ret["rectangle"]): ret["rectangle"][idx] = (item[0] + xmin, item[1] + ymin) ret["result"] = (ret_in_area["result"][0] + xmin, ret_in_area["result"][1] + ymin) return ret
def find_best_result(self): """函数功能:找到最优结果.""" if self.resolution!=(): # 第一步:校验图像输入 check_source_larger_than_search(self.im_source, self.im_search) if self.resolution[0]<self.im_search.shape[1] or self.resolution[1]<self.im_search.shape[0]: raise TemplateInputError("error: resolution is too small.") # 第二步:计算模板匹配的结果矩阵res if not self.record_pos is None: area, self.resolution = self._get_area_scope(self.im_source, self.im_search, self.record_pos, self.resolution) self.im_source = aircv.crop_image(self.im_source, area) check_source_larger_than_search(self.im_source, self.im_search) r_min, r_max = self._get_ratio_scope( self.im_source, self.im_search, self.resolution) s_gray, i_gray = img_mat_rgb_2_gray(self.im_search), img_mat_rgb_2_gray(self.im_source) confidence, max_loc, w, h, _ = self.multi_scale_search( i_gray, s_gray, ratio_min=r_min, ratio_max=r_max, step=self.scale_step, threshold=self.threshold) if not self.record_pos is None: max_loc = (max_loc[0] + area[0], max_loc[1] + area[1]) # 求取识别位置: 目标中心 + 目标区域: middle_point, rectangle = self._get_target_rectangle(max_loc, w, h) best_match = generate_result(middle_point, rectangle, confidence) LOGGING.debug("[%s] threshold=%s, result=%s" % (self.METHOD_NAME, self.threshold, best_match)) return best_match if confidence >= self.threshold else None else: return None
def _find_sift_in_predict_area(self, image, screen): if not self.record_pos: return None # calc predict area in screen image_wh, screen_resolution = aircv.get_resolution( image), aircv.get_resolution(screen) xmin, ymin, xmax, ymax = Predictor.get_predict_area( self.record_pos, image_wh, self.resolution, screen_resolution) # crop predict image from screen predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) if not predict_area.any(): return None # find sift in that image ret_in_area = aircv.find_sift(predict_area, image, threshold=self.threshold, rgb=self.rgb) # calc cv ret if found if not ret_in_area: return None ret = deepcopy(ret_in_area) if "rectangle" in ret: for idx, item in enumerate(ret["rectangle"]): ret["rectangle"][idx] = (item[0] + xmin, item[1] + ymin) ret["result"] = (ret_in_area["result"][0] + xmin, ret_in_area["result"][1] + ymin) return ret
def crop_image(self, rect: list): """局部截图 :param rect = [x_min, y_min, x_max ,y_max]. :return filepath 图片路径 """ # 局部截图 img = G.DEVICE.snapshot() crop_screen = crop_image(img, rect) # 生成截图路径 filename = "%(time)d.jpg" % {'time': timestamp() * 1000} filepath = os.path.join(ST.LOG_DIR, filename) # 保存局部截图到logs文件夹中 pil_image = cv2_2_pil(crop_screen) pil_image.save(filepath, quality=99, optimize=True) return filepath
def Record(self, bbox): import time import airtest.core.win.screen as screen threading.current_thread.name = 'recording' while True: if not self.m_bRecording: break self.m_Lock.acquire() print('Recording...', (bbox[2] - bbox[0], bbox[3] - bbox[1])) try: im = aircv.crop_image(screen.screenshot(None), bbox) self.video.write(numpy.array(im)) # 将img convert ndarray except: pass self.m_Lock.release() time.sleep(INTERVAL)
def _find_sift_in_predict_area(self, image, screen): if not self.record_pos: return None # calc predict area in screen screen_resolution = aircv.get_resolution(screen) xmin, ymin, xmax, ymax = Predictor.get_predict_area(self.record_pos, screen_resolution) # crop predict image from screen predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) # aircv.show(predict_area) # find sift in that image ret_in_area = aircv.find_sift(predict_area, image, threshold=self.threshold, rgb=self.rgb) # calc cv ret if found if not ret_in_area: return None ret = deepcopy(ret_in_area) ret["result"] = (ret_in_area["result"][0] + xmin, ret_in_area["result"][1] + ymin) return ret
def loop_find3(total_pic, query, posi, tag=None, timeout=ST.FIND_TIMEOUT, threshold=None, interval=0.5, intervalfunc=None): # print("ST.FIND_TIMEOUT",ST.FIND_TIMEOUT) G.LOGGING.info("Try finding:\n%s", query.record_pos) G.LOGGING.info(query._imread().shape) # screen_resolution = aircv.get_resolution(screen) part_size_width = query._imread().shape[0] part_size_height = query._imread().shape[1] part_resolution_width = query.resolution[0] part_resolution_height = query.resolution[1] # pairt_origin_xmin_relative = posi[0]; # pairt_origin_xmax_relative = posi[2] ; pairt_origin_ymin_relative = posi[0] / part_resolution_height pairt_origin_ymax_relative = posi[1] / part_resolution_height # print("香菇:",(pairt_origin_ymin_relative,pairt_origin_ymax_relative)) posi_x = query.record_pos[0] posi_y = query.record_pos[1] # print("posi_x",posi_x) start_time = time.time() screen = aircv.imread(total_pic) G.LOGGING.debug("query path %s" % query.filepath) # predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) screen_resolution = aircv.get_resolution(screen) # print("总图片的分辨率",screen_resolution); total_resolution_width = screen_resolution[0] total_resolution_height = screen_resolution[1] width_scale = round(total_resolution_width / part_resolution_width, 2) height_scale = round(total_resolution_height / part_resolution_height, 2) # print("width_scale",width_scale) # print("width_scale",height_scale) # print("总图的分辨率",(total_resolution_width,total_resolution_height)) # print("缩放之后的相对坐标",(pairt_origin_xmin_relative * width_scale,pairt_origin_ymin_relative * height_scale,pairt_origin_xmax_relative * width_scale,pairt_origin_ymax_relative * height_scale)) # pairt_end_xmin_absolute = round( (pairt_origin_xmin_relative * width_scale) * total_resolution_width ); pairt_end_xmin_absolute = 0 # pairt_end_xmax_absolute = round( (pairt_origin_xmax_relative * width_scale) * total_resolution_width ); pairt_end_xmax_absolute = total_resolution_width pairt_end_ymin_absolute = round( (pairt_origin_ymin_relative * height_scale) * total_resolution_height) - 30 pairt_end_ymax_absolute = round( (pairt_origin_ymax_relative * height_scale) * total_resolution_height) + 30 # print("enddddd",(pairt_end_xmin_absolute,pairt_end_ymin_absolute,pairt_end_xmax_absolute,pairt_end_ymax_absolute)) # part_resolution_width_scaled = part_size_width * width_scale; # part_resolution_height_scaled = part_size_height * height_scale; # part_end_xmax = ((posi_x + 0.5) if (posi_x + 0.5)<=1 else 1)*total_resolution_width # part_end_ymax = ((posi_y + 0.5) if (posi_y + 0.5)<=1 else 1)*total_resolution_height # part_end_xmin = part_end_xmax - part_resolution_width_scaled # part_end_ymin = part_end_ymax - part_resolution_height_scaled predict_area = aircv.crop_image( screen, (pairt_end_xmin_absolute, pairt_end_ymin_absolute, pairt_end_xmax_absolute, pairt_end_ymax_absolute)) screen[0:pairt_end_ymin_absolute, 0:total_resolution_width] = 0 screen[pairt_end_ymax_absolute:total_resolution_height, 0:total_resolution_width] = 0 # print("黑素区域",(pairt_end_ymin_absolute,pairt_end_ymax_absolute)) # screen[200:250,0:50] = 0; aircv.imwrite("part_black.png", screen, 99) # predict_area = aircv.crop_image(screen, (0, 0, 1126, 2436)); # print("hahahahah") # print((part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax)) # print(predict_area.shape) aircv.imwrite("predict_area3.png", predict_area, 99) while True: if predict_area is None: G.LOGGING.warning("预测区域有问题!") else: if threshold: query.threshold = threshold match_pos = query.match_in(screen) if match_pos: try_log_screen(screen) return match_pos if intervalfunc is not None: intervalfunc() # 超时则raise,未超时则进行下次循环: if (time.time() - start_time) > timeout: try_log_screen(screen) # raise TargetNotFoundError('Picture %s not found in screen' % query) G.LOGGING.warning("图片查找超时!") break else: time.sleep(interval)
def loop_find2(total_pic, query, timeout=ST.FIND_TIMEOUT, threshold=None, interval=0.5, intervalfunc=None): G.LOGGING.info("Try finding:\n%s", query.record_pos) G.LOGGING.info(query._imread().shape) part_size_width = query._imread().shape[0] part_size_height = query._imread().shape[1] part_resolution_width = query.resolution[0] part_resolution_height = query.resolution[1] # print("part_resolution_width:" + str(part_resolution_width)) # print("part_resolution_height:" + str(part_resolution_height)) posi_x = query.record_pos[0] posi_y = query.record_pos[1] print("posi_x", posi_x) start_time = time.time() screen = aircv.imread(total_pic) G.LOGGING.debug("query path %s" % query.filepath) # predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) screen_resolution = aircv.get_resolution(screen) total_resolution_width = query.resolution[0] total_resolution_height = query.resolution[1] width_scale = total_resolution_width / part_resolution_width height_scale = total_resolution_height / part_resolution_height print("test_scale:" + str(1 / 2)) print("width_scale:" + str(width_scale)) print("height_scase:" + str(height_scale)) part_resolution_width_scaled = part_size_width * width_scale part_resolution_height_scaled = part_size_height * height_scale print("part_resolution_width_scaled:" + str(part_resolution_width_scaled)) print("part_resolution_height_scaled:" + str(part_resolution_height_scaled)) G.LOGGING.debug("图片的分辨率") G.LOGGING.debug(screen_resolution) part_end_xmax = ((posi_x + 0.5) if (posi_x + 0.5) <= 1 else 1) * total_resolution_width part_end_ymax = ((posi_y + 0.5) if (posi_y + 0.5) <= 1 else 1) * total_resolution_height part_end_xmin = part_end_xmax - part_resolution_width_scaled part_end_ymin = part_end_ymax - part_resolution_height_scaled predict_area = aircv.crop_image( screen, (part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax)) print("hahahahah") print((part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax)) print(predict_area.shape) aircv.imwrite("predict_area.png", screen, 99) while True: if predict_area is None: G.LOGGING.warning("预测区域有问题!") else: if threshold: query.threshold = threshold match_pos = query.match_in(screen) if match_pos: try_log_screen(screen) return match_pos if intervalfunc is not None: intervalfunc() # 超时则raise,未超时则进行下次循环: if (time.time() - start_time) > timeout: try_log_screen(predict_area) # raise TargetNotFoundError('Picture %s not found in screen' % query) G.LOGGING.warning("图片查找超时!") break else: time.sleep(interval)