|
@@ -133,7 +133,7 @@ class pcacc_img:
|
|
def find_all_img(image_path):
|
|
def find_all_img(image_path):
|
|
|
|
|
|
# 设置查找的置信度(confidence)阈值,范围从0到1,默认为0.999
|
|
# 设置查找的置信度(confidence)阈值,范围从0到1,默认为0.999
|
|
- confidence_threshold = 0.85
|
|
|
|
|
|
+ confidence_threshold = 0.95
|
|
|
|
|
|
# 查找模糊图片在屏幕上的位置
|
|
# 查找模糊图片在屏幕上的位置
|
|
positions = pyautogui.locateAllOnScreen(image_path, confidence=confidence_threshold)
|
|
positions = pyautogui.locateAllOnScreen(image_path, confidence=confidence_threshold)
|
|
@@ -142,6 +142,43 @@ class pcacc_img:
|
|
return False, []
|
|
return False, []
|
|
|
|
|
|
return True, positions
|
|
return True, positions
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def find_img_cv(image_path, region=None):
|
|
|
|
+ # 读取模板和屏幕截图
|
|
|
|
+ confidence_threshold = 0.95
|
|
|
|
+
|
|
|
|
+ template = cv2.imread(image_path, cv2.IMREAD_COLOR)
|
|
|
|
+ screenshot = cv2.cvtColor(np.array(pyautogui.screenshot(region=region)), cv2.COLOR_RGB2BGR)
|
|
|
|
+
|
|
|
|
+ # 模板匹配
|
|
|
|
+ result = cv2.matchTemplate(screenshot, template, cv2.TM_CCOEFF_NORMED)
|
|
|
|
+ _, max_val, _, max_loc = cv2.minMaxLoc(result)
|
|
|
|
+
|
|
|
|
+ if max_val > confidence_threshold: # 置信度阈值
|
|
|
|
+ left, top = max_loc # 左上角坐标
|
|
|
|
+ width, height = template.shape[1], template.shape[0] # 模板宽高
|
|
|
|
+
|
|
|
|
+ #mid
|
|
|
|
+ mid_left = int(left + width / 4)
|
|
|
|
+ mid_right = int(left + width / 4 * 3)
|
|
|
|
+ mid_top = int(top + height / 4)
|
|
|
|
+ mid_bottom = int(top + height / 4 * 3)
|
|
|
|
+
|
|
|
|
+ #want_position
|
|
|
|
+ want_x = random.randint(mid_left, mid_right)
|
|
|
|
+ want_y = random.randint(mid_top, mid_bottom)
|
|
|
|
+
|
|
|
|
+ #dst_position
|
|
|
|
+ dst_x = random.randint(int(want_x - width / 3), int(want_x + width / 3))
|
|
|
|
+ dst_y = random.randint(int(want_y - height / 3), int(want_y + height / 3))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ print(f"{image_path}:({dst_x}, {dst_y})")
|
|
|
|
+ return True, (dst_x, dst_y)
|
|
|
|
+ else:
|
|
|
|
+ print(f"{image_path} cv图片未找到")
|
|
|
|
+ return False, (-1, -1)
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def find_img(image_path):
|
|
def find_img(image_path):
|
|
@@ -154,7 +191,6 @@ class pcacc_img:
|
|
|
|
|
|
if position is not None:
|
|
if position is not None:
|
|
# 图片找到了,获取图片的中心点坐标
|
|
# 图片找到了,获取图片的中心点坐标
|
|
-
|
|
|
|
#mid
|
|
#mid
|
|
mid_left = int(position.left + position.width / 4)
|
|
mid_left = int(position.left + position.width / 4)
|
|
mid_right = int(position.left + position.width / 4 * 3)
|
|
mid_right = int(position.left + position.width / 4 * 3)
|
|
@@ -179,7 +215,7 @@ class pcacc_img:
|
|
@staticmethod
|
|
@staticmethod
|
|
def find_imgArr(imgArr):
|
|
def find_imgArr(imgArr):
|
|
for cur_img in imgArr:
|
|
for cur_img in imgArr:
|
|
- ret, pos = pcacc_img.find_img(cur_img)
|
|
|
|
|
|
+ ret, pos = pcacc_img.find_img_cv(cur_img)
|
|
if ret:
|
|
if ret:
|
|
return ret, pos
|
|
return ret, pos
|
|
return False, None
|
|
return False, None
|
|
@@ -187,7 +223,7 @@ class pcacc_img:
|
|
@staticmethod
|
|
@staticmethod
|
|
def find_imgs(images):
|
|
def find_imgs(images):
|
|
if isinstance(images, str):
|
|
if isinstance(images, str):
|
|
- return pcacc_img.find_img(images)
|
|
|
|
|
|
+ return pcacc_img.find_img_cv(images)
|
|
elif isinstance(images, (list, tuple)):
|
|
elif isinstance(images, (list, tuple)):
|
|
return pcacc_img.find_imgArr(images)
|
|
return pcacc_img.find_imgArr(images)
|
|
else:
|
|
else:
|