Skip to content

Commit

Permalink
#59 适配了下低画质下ocr失败的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorReid committed Oct 23, 2023
1 parent 8322f32 commit 6b24941
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/sr/cal_pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def cal_character_pos(im: ImageMatcher,
center_x = offset_x + result.w // 2
center_y = offset_y + result.h // 2

if True:
if show:
cv2_utils.show_overlap(lm_info.origin, mm_info.origin, offset_x, offset_y, template_scale=scale, win_name='overlap')

log.debug('计算当前坐标为 (%s, %s) 使用缩放 %.2f 置信度 %.2f', center_x, center_y, scale, result.confidence)
Expand Down
6 changes: 3 additions & 3 deletions src/sr/constants/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def another_floor(self):
P02_R02 = Region("cjxy", "城郊雪原", P02_YYL)
P02_R03 = Region("bytl", "边缘通路", P02_YYL)
P02_R04 = Region("twjq", "铁卫禁区", P02_YYL)
P02_R05 = Region("cxhl", "残响回廊", P02_YYL)
P02_R05 = Region("cxhl", "残响回廊", P02_YYL, ocr_str='回廊')
P02_R06 = Region("ydl", "永冬岭", P02_YYL)
P02_R07 = Region("zwzz", "造物之柱", P02_YYL)
P02_R08_L2 = Region("jwqsyc", "旧武器试验场", P02_YYL, level=2)
Expand Down Expand Up @@ -249,7 +249,7 @@ def __str__(self):
# 雅利洛 - 残响回廊
P02_R05_SP01 = TransportPoint('zcly', '筑城领域', P02_R05, 'mm_tp_03', (770, 442))
P02_R05_SP02 = TransportPoint('wrgc', '污染广场', P02_R05, 'mm_tp_03', (381, 655))
P02_R05_SP03 = TransportPoint('zzzhs', '作战指挥室', P02_R05, 'mm_tp_03', (495, 856))
P02_R05_SP03 = TransportPoint('zzzhs', '作战指挥室', P02_R05, 'mm_tp_03', (495, 856), ocr_str='作战')
P02_R05_SP04 = TransportPoint('gzcqx', '古战场前线', P02_R05, 'mm_tp_03', (570, 1243))
P02_R05_SP05 = TransportPoint('mlzx', '鸣雷之形', P02_R05, 'mm_tp_06', (526, 640))
P02_R05_SP06 = TransportPoint('sjzx', '霜晶之形', P02_R05, 'mm_tp_06', (681, 1231))
Expand All @@ -260,7 +260,7 @@ def __str__(self):
# 雅利洛 - 永冬岭
P02_R06_SP01 = TransportPoint('gzc', '古战场', P02_R06, 'mm_tp_03', (366, 776))
P02_R06_SP02 = TransportPoint('zwpt', '造物平台', P02_R06, 'mm_tp_03', (784, 571))
P02_R06_SP03 = TransportPoint('rzzj', '睿治之径', P02_R06, 'mm_tp_09', (585, 663))
P02_R06_SP03 = TransportPoint('rzzj', '睿治之径', P02_R06, 'mm_tp_09', (585, 663), ocr_str='治之径')
P02_R06_SP04 = TransportPoint('cxhl', '残响回廊', P02_R06, 'mm_sp_02', (338, 793))
P02_R06_SP05 = TransportPoint('hcdlm', '寒潮的落幕', P02_R06, 'mm_boss_02', (814, 701))

Expand Down
2 changes: 1 addition & 1 deletion src/sr/control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def open_map(self) -> bool:
pass

def click_ocr(self, screen: MatLike, word: str, threshold: float = 0.5, rect: tuple = None, click_offset: tuple = None,
press_time: int = 0, same_word: bool = False,
press_time: float = 0, same_word: bool = False,
) -> bool:
"""
在屏幕中点击关键词所在位置 多个关键词时随机点击一个
Expand Down
3 changes: 3 additions & 0 deletions src/sr/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def match_template_with_rotation(self, source: MatLike, template_id: str,

class OcrMatcher:

def ocr_for_single_line(self, image: MatLike, threshold: float = 0.5) -> str:
pass

def run_ocr(self, image: MatLike, threshold: float = 0.5) -> dict:
"""
对图片进行OCR 返回所有匹配结果
Expand Down
10 changes: 8 additions & 2 deletions src/sr/image/cnocr_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class CnOcrMatcher(OcrMatcher):
"""

def __init__(self,
det_model_name: str = 'ch_PP-OCRv2_det',
rec_model_name: str = 'densenet_lite_136-fc'):
det_model_name: str = 'ch_PP-OCRv2_det', # 实测这个比 ch_PP-OCRv3_det 更适合
rec_model_name: str = 'densenet_lite_136-fc'): # densenet_lite_136-gru
try:
self.ocr = CnOcr(det_model_name=det_model_name,
rec_model_name=rec_model_name,
Expand All @@ -23,6 +23,12 @@ def __init__(self,
except Exception:
log.error('OCR模型加载出错', exc_info=True)

def ocr_for_single_line(self, image: MatLike, threshold: float = 0.5) -> str:
result = self.ocr.ocr_for_single_line(image)
log.debug('OCR结果 %s', result)
if result['score'] > threshold:
return result['text']

def run_ocr(self, image: MatLike, threshold: float = 0.5) -> dict:
"""
对图片进行OCR 返回所有匹配结果
Expand Down
10 changes: 8 additions & 2 deletions src/sr/image/sceenshot/large_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from sr.image.image_holder import ImageHolder
from sr.image.sceenshot import LargeMapInfo

PLANET_NAME_RECT = (100, 60, 260, 100)
CUT_MAP_RECT = (200, 190, 1300, 900) # 截取大地图的区域
EMPTY_MAP_POS = (1350, 800) # 地图空白区域 用于取消选择传送点 和 拖动地图
TP_BTN_RECT = (1500, 950, 1800, 1000) # 右侧显示传送按钮的区域
Expand All @@ -31,8 +32,12 @@ def get_planet(screen: MatLike, ocr: OcrMatcher) -> Planet:
:param ocr: ocr
:return: 星球名称
"""
word: str
result = ocr.run_ocr(screen[30:100, 90:250], threshold=0.4)
planet_name_part, _ = cv2_utils.crop_image(screen, PLANET_NAME_RECT)
lower_color = np.array([220, 220, 220], dtype=np.uint8)
upper_color = np.array([255, 255, 255], dtype=np.uint8)
white_part = cv2.inRange(planet_name_part, lower_color, upper_color)
# cv2_utils.show_image(white_part, win_name='white_part')
result = ocr.run_ocr(planet_name_part, threshold=0.4)
log.debug('屏幕左上方获取星球结果 %s', result.keys())
for word in result.keys():
if word.find(gt(constants.map.P01_KZJ.ocr_str)) > -1:
Expand Down Expand Up @@ -168,6 +173,7 @@ def get_active_region_name(screen: MatLike, ocr: OcrMatcher) -> str:
upper = 255
part, _ = cv2_utils.crop_image(screen, REGION_LIST_RECT)
bw = cv2.inRange(part, (lower, lower, lower), (upper, upper, upper))
# cv2_utils.show_image(bw, win_name='get_active_region_name')
km = ocr.run_ocr(bw)
if len(km) > 0:
return km.popitem()[0]
Expand Down
2 changes: 1 addition & 1 deletion src/sr/operation/unit/choose_planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def choose_planet(self, screen) -> bool:
# 二值化后更方便识别字体
gray = cv2.cvtColor(screen, cv2.COLOR_RGB2GRAY)
_, mask = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
return self.ctx.controller.click_ocr(mask, gt(self.planet.ocr_str), click_offset=(0, -100), press_time=1)
return self.ctx.controller.click_ocr(mask, gt(self.planet.ocr_str), click_offset=(0, -100), press_time=1.5)
4 changes: 2 additions & 2 deletions src/sr/operation/unit/choose_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run(self) -> int:
# 判断当前选择区域是否目标区域
current_region = large_map.get_active_region_name(screen, self.ctx.ocr)
log.info('当前选择区域 %s', current_region)
if current_region != gt(self.region.ocr_str):
if current_region is None or current_region.find(gt(self.region.ocr_str)) == -1:
find = self.click_target_region(screen)
if not find: # 向下滚动5次 再向上滚动5次
log.info('当前界面未发现 %s 准备滚动', self.region.cn)
Expand Down Expand Up @@ -82,7 +82,7 @@ def click_target_region(self, screen) -> bool:
:param screen:
:return:
"""
return self.ctx.controller.click_ocr(screen, self.region.ocr_str, rect=large_map.REGION_LIST_RECT)
return self.ctx.controller.click_ocr(screen, self.region.ocr_str, rect=large_map.REGION_LIST_RECT, threshold=0.4)

def scroll_region_area(self, d: int = 1):
"""
Expand Down

0 comments on commit 6b24941

Please sign in to comment.