def test_get_location(self): il = ImageLocation() src_img_rgb = cv2.imread(JOIN2, 1) tgt_img_rgb = cv2.imread(JOIN2_T, 1) pt = il.get_location(JOIN2, JOIN2_T) _, w, h = tgt_img_rgb.shape[::-1] draw = il.draw_rect(pt, w, h, src_img_rgb, OUT) self.assertEqual(draw, True)
def test_draw_rect_wait5(self): il = ImageLocation() src_img_rgb = cv2.imread(WAIT5) tgt_img_rgb = cv2.imread(WAIT5_T, 0) pt = il.get_location(WAIT5, WAIT5_T, 0.5) w, h = tgt_img_rgb.shape[::-1] draw = il.draw_rect(pt, w, h, src_img_rgb, OUT) self.assertEqual(draw, True)
def test_draw_rect(self): il = ImageLocation() src_img_rgb = cv2.imread(SRC) tgt_img_rgb = cv2.imread(TARGET, 0) pt = il._get_location_obj(src_img_rgb, tgt_img_rgb) w, h = tgt_img_rgb.shape[::-1] draw = il.draw_rect(pt, w, h, src_img_rgb, OUT) self.assertEqual(draw, True)
class BaseRCProcessor(object): __metaclass__ = ABCMeta img_tools = ImageLocation() name = None def __new__(cls, *args, **kwargs): ins = getattr(cls, '_ins', None) if not ins: ins = super(BaseRCProcessor, cls).__new__(cls, *args, **kwargs) setattr(cls, '_ins', ins) return ins @abstractmethod def rc(self): raise NotImplemented def get_location(self, source, target, threshold=0.8): x, y = ImageLocation.get_location(source, target, threshold) w, h = ImageLocation.get_target_shape(target) return x, y, w, h def match(self, source): """查看是否与当前rc相符合, 符合返回loc和source 否则抛出异常""" assert isinstance(self.rc(), (str, unicode)) self.get_location(source, self.rc()) def action(self, hwnd, source, *args, **kwargs): pw = win32gui.GetWindowRect(hwnd) x, y, w, h = self.get_location(source, self.rc()) MouseTools.setCursorPos([pw[0] + x + w / 2, pw[1] + y + h / 2]) MouseTools.clickLeftCur() return True
def get_state(self, hwnd, source, *args, **kwargs): for p in PROCESSOR: try: p.match(source) return p, source except errors.LocationDoesNotFound: logging.info("LocationDoesNotFound") # 得到图片的颜色占比, radio = ImageLocation.get_color_mean(source, (0, 0, 0), 10) # 如果颜色占比大于80%, 则说明是纯色, 需要等待 if radio > 0.8: return processor.WaitProcessor(), source continue # 保存新状态到RC中,以便之后处理. source = self.w_tools.window_capture(hwnd, "unknown.jpg", base_dir=RC_PATH) return "unknown", source
def get_location(self, source, target, threshold=0.8): x, y = ImageLocation.get_location(source, target, threshold) w, h = ImageLocation.get_target_shape(target) return x, y, w, h
def test_get_color_mean_with_str(self): il = ImageLocation() ratio = il.get_color_mean(MEAN, (0, 0, 0)) self.assertEqual(ratio > 0.8, True)
def test_get_color_mean(self): il = ImageLocation() img = cv2.imread(MEAN) ratio = il.get_color_mean(img, (0, 0, 0)) self.assertEqual(ratio > 0.8, True)
def test_get_location_obj(self): il = ImageLocation() x = il.get_location_obj(SRC, TARGET) self.assertIsNotNone(x)