utils.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import cv2
  2. import numpy as np
  3. from module.logger import logger
  4. def retry(func, max_retry=3, *args, **kwargs):
  5. while True:
  6. try:
  7. result = func(*args, **kwargs)
  8. if result is not None or max_retry <= 0:
  9. return result
  10. max_retry -= 1
  11. except Exception as e:
  12. if max_retry <= 0:
  13. raise e
  14. max_retry -= 1
  15. def detect_safe_area(image, target_gray_value=255, tolerance=0):
  16. """
  17. 在灰度图片中查找与目标灰度值在容差范围内的纯色区块,并返回这些区块的坐标。
  18. :param image_path: 图片路径
  19. :param target_gray_value: 目标灰度值,默认为255(最亮)
  20. :param tolerance: 灰度差异容忍度,默认为0(即完全匹配)
  21. :return: 包含找到的纯色区块坐标元组的列表
  22. """
  23. # 加载图片并转换为灰度图
  24. # img = Image.open(image_path).convert('L') # 'L' 表示转换为灰度图
  25. # img_arr = np.array(img) # 转换为numpy数组以便高效处理
  26. # 将图片转换为灰度图
  27. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  28. _, img_arr = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
  29. # 初始化结果列表
  30. solid_regions = []
  31. # 遍历图片的每个像素
  32. for y in range(img_arr.shape[0]): # 高度
  33. for x in range(img_arr.shape[1]): # 宽度
  34. pixel_gray_value = img_arr[y, x]
  35. # 检查当前像素灰度值是否与目标灰度值在容差范围内匹配
  36. if abs(pixel_gray_value - target_gray_value) <= tolerance:
  37. # 检查周围是否也是同样的灰度值,以确定这是一个区块而非单个像素
  38. is_solid_region = True
  39. for dy in range(-1, 2): # 检查3x3区域
  40. for dx in range(-1, 2):
  41. if dy == 0 and dx == 0:
  42. continue # 跳过中心点
  43. ny, nx = y + dy, x + dx
  44. if 0 <= ny < img_arr.shape[0] and 0 <= nx < img_arr.shape[1]:
  45. if abs(img_arr[ny, nx] - target_gray_value) > tolerance:
  46. is_solid_region = False
  47. break
  48. if not is_solid_region:
  49. break
  50. if is_solid_region:
  51. # 记录纯色区块的左上角坐标和右下角坐标
  52. start_x, start_y = x, y
  53. while start_y > 0 and abs(img_arr[start_y - 1, x] - target_gray_value) <= tolerance:
  54. start_y -= 1
  55. while start_x > 0 and abs(img_arr[y, start_x - 1] - target_gray_value) <= tolerance:
  56. start_x -= 1
  57. end_x, end_y = x, y
  58. while end_y < img_arr.shape[0] - 1 and abs(img_arr[end_y + 1, x] - target_gray_value) <= tolerance:
  59. end_y += 1
  60. while end_x < img_arr.shape[1] - 1 and abs(img_arr[y, end_x + 1] - target_gray_value) <= tolerance:
  61. end_x += 1
  62. solid_regions.append(((start_x, start_y), (end_x + 1, end_y + 1)))
  63. return solid_regions
  64. def detect_safe_area2(image, safe_color_lower, safe_color_upper, num_areas=3, debug=False):
  65. """
  66. 找出当前截图里最大的纯色区域,找出来的这个区域将被用于随机点击的区域。
  67. :note: 注意拿到这个区域后要马上点击,不要sleep!
  68. :param image: 截图图像
  69. :return: 最大纯色区域的坐标
  70. """
  71. # 将图片从BGR转换到HSV色彩空间
  72. hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  73. # 定义颜色范围
  74. # 注意:这里的颜色范围需要根据实际图片中的安全区域颜色进行调整
  75. mask = cv2.inRange(hsv, safe_color_lower, safe_color_upper)
  76. # 应用形态学操作来消除噪点和填补小的孔洞
  77. kernel = np.ones((5, 5), np.uint8)
  78. mask = cv2.dilate(mask, kernel, iterations=1)
  79. mask = cv2.erode(mask, kernel, iterations=1)
  80. # 查找轮廓
  81. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  82. # 计算每个轮廓的面积并排序
  83. # areas = [(cv2.contourArea(contour), contour) for contour in contours]
  84. areas = []
  85. for contour in contours:
  86. area = cv2.contourArea(contour)
  87. print(f"area={area}")
  88. areas.append((area, contour))
  89. # areas.sort(reverse=True)
  90. # 按面积降序排序
  91. areas_sorted = sorted(areas, key=lambda x: x[0], reverse=True)
  92. if debug:
  93. # 绘制面积排名前3的区域
  94. for area, contour in areas_sorted[:num_areas]:
  95. x1, y1, w1, h1 = cv2.boundingRect(contour)
  96. # 使用蓝色边框,线宽为4
  97. cv2.rectangle(image, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 4)
  98. # 使用红色边框,线宽为3
  99. cv2.drawContours(image, [contour], -1, (0, 0, 255), 3)
  100. # 假设最大的轮廓是安全区域
  101. if contours:
  102. safe_area = max(contours, key=cv2.contourArea)
  103. x, y, w, h = cv2.boundingRect(safe_area)
  104. if debug:
  105. # 绘制安全区域
  106. cv2.drawContours(image, [safe_area], -1, (0, 255, 0), 3)
  107. return (x, y, w, h)
  108. return (0, 0, 0, 0)
  109. def test_ocr_locate_dokan_target():
  110. import cv2
  111. from module.atom.ocr import RuleOcr
  112. logger.info("========================================>>test 2")
  113. O_DOKAN_MAP = RuleOcr(roi=(270, 130, 740, 460), area=(270, 130, 740, 460), mode="Full", method="Default",
  114. keyword="万", name="dokan_map")
  115. image = cv2.imread("g:/yys/oas/tests/1.png")
  116. pos = O_DOKAN_MAP.ocr_full(image)
  117. print(f"image1: {pos}")
  118. image = cv2.imread("g:/yys/oas/tests/2.png")
  119. pos = O_DOKAN_MAP.ocr_full(image)
  120. print(f"image2: {pos}")
  121. image = cv2.imread("g:/yys/oas/tests/3.png")
  122. pos = O_DOKAN_MAP.ocr_full(image)
  123. print(f"image3: {pos}")
  124. image = cv2.imread("g:/yys/oas/tests/4.png")
  125. pos = O_DOKAN_MAP.ocr_full(image)
  126. print(f"image4: {pos}")
  127. if pos == (0, 0, 0, 0):
  128. logger.info("failed to find {self.O_DOKAN_MAP.keyword}")
  129. else:
  130. # 取中间
  131. x = pos[0] + pos[2] / 2
  132. # 往上偏移20
  133. y = pos[1] - 20
  134. logger.info(f"ocr detect result pos={pos}, try click pos, x={x}, y={y}")
  135. def test_anti_detect_random_click():
  136. import cv2
  137. # image_list = ['g:/yys/oas/tests/1.png', 'g:/yys/oas/tests/2.png', 'g:/yys/oas/tests/3.png', 'g:/yys/oas/tests/4.png', 'g:/yys/oas/tests/5.png']
  138. image_list = ['g:/yys/oas/tests/a.png', 'g:/yys/oas/tests/b.png', 'g:/yys/oas/tests/c.png']
  139. for item in image_list:
  140. print(f"item: {item}")
  141. image = cv2.imread(item)
  142. # 注意:这里的颜色范围需要根据实际图片进行调整
  143. # http://kb.rg4.net/docs/omassistant/omassistant-1fnirpstkhdnh
  144. # 0<=h<20, 红色
  145. # 30<=h<45,黄色
  146. # 45<=h<90,绿色
  147. # 90<=h<125,青色
  148. # 125<=h<150,蓝色
  149. # 150<=h<175,紫色
  150. # 175<=h<200,粉红色
  151. # 200<=h<220,砖红色
  152. # 220<=h<255,品红色
  153. # 示例使用:假设安全区域是绿色的
  154. safe_color_lower = np.array([45, 25, 25]) # HSV颜色空间的绿色下界
  155. safe_color_upper = np.array([90, 255, 255]) # HSV颜色空间的绿色上界
  156. # 读取图片
  157. image = cv2.imread('c.png')
  158. if image is None:
  159. print("Error: Image not found.")
  160. else:
  161. # 调用函数
  162. pos = detect_safe_area2(image, safe_color_lower, safe_color_upper, 3, True)
  163. print(f"pos={pos}")
  164. # 显示结果
  165. cv2.imshow('Safe Area', image)
  166. cv2.waitKey(0)
  167. cv2.destroyAllWindows()