将NumberRecognizer移入recog,简化各处number写法
This commit is contained in:
parent
f084957e1e
commit
429f919d61
20 changed files with 101 additions and 501 deletions
|
@ -3,9 +3,9 @@ from typing import Literal
|
|||
import cv2
|
||||
|
||||
from mower.models import Digtal
|
||||
|
||||
from . import typealias as tp
|
||||
from .image import cropimg
|
||||
from mower.utils import config
|
||||
from mower.utils import typealias as tp
|
||||
from mower.utils.image import cropimg, thres2
|
||||
|
||||
|
||||
class NumberRecognizer:
|
||||
|
@ -71,3 +71,54 @@ class NumberRecognizer:
|
|||
value += str(score.index(min(score)))
|
||||
|
||||
return value
|
||||
|
||||
def number(
|
||||
self,
|
||||
font: Literal["riic_base", "noto", "secret_front"] = "noto",
|
||||
scope: tp.Scope | None = None,
|
||||
height: int = 25,
|
||||
thres: int = 127,
|
||||
img: tp.GrayImage | None = None,
|
||||
rect_st: int | None = None,
|
||||
rect_ed: int | None = None,
|
||||
rect_limits: list[dict] = [],
|
||||
target_range: list = range(10),
|
||||
) -> str:
|
||||
try:
|
||||
if img is None:
|
||||
img = cropimg(config.recog.gray, scope)
|
||||
default_height = 25
|
||||
if height != default_height:
|
||||
scale = 25 / height
|
||||
img = cv2.resize(img, None, None, scale, scale)
|
||||
img = thres2(img, thres)
|
||||
rect = self.segment(img)
|
||||
rect = self.filter_rectangles(rect, rect_st, rect_ed, rect_limits)
|
||||
return self.number_match(img, rect, font, target_range)
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
def number_int(
|
||||
self,
|
||||
font: Literal["riic_base", "noto", "secret_front"] = "noto",
|
||||
scope: tp.Scope | None = None,
|
||||
height: int = 25,
|
||||
thres: int = 127,
|
||||
img: tp.GrayImage | None = None,
|
||||
rect_st: int | None = None,
|
||||
rect_ed: int | None = None,
|
||||
rect_limits: list[dict] = [],
|
||||
target_range: list = range(10),
|
||||
) -> int:
|
||||
res = self.number(
|
||||
font,
|
||||
scope,
|
||||
height,
|
||||
thres,
|
||||
img,
|
||||
rect_st,
|
||||
rect_ed,
|
||||
rect_limits,
|
||||
target_range,
|
||||
)
|
||||
return int(res) if res else 0
|
||||
|
|
|
@ -12,6 +12,7 @@ from mower.utils.csleep import MowerExit
|
|||
from mower.utils.image import bytes2img, cmatch, cropimg, loadres, thres2
|
||||
from mower.utils.log import logger
|
||||
from mower.utils.matcher import Matcher
|
||||
from mower.utils.number import NumberRecognizer
|
||||
from mower.utils.scene import Scene, SceneComment
|
||||
from mower.utils.vector import va
|
||||
|
||||
|
@ -36,6 +37,7 @@ class Recognizer:
|
|||
self._gray = None
|
||||
self._hsv = None
|
||||
self._matcher = None
|
||||
self._num = None
|
||||
self.scene = Scene.UNDEFINED
|
||||
|
||||
@property
|
||||
|
@ -62,6 +64,12 @@ class Recognizer:
|
|||
self._matcher = Matcher(self.gray)
|
||||
return self._matcher
|
||||
|
||||
@property
|
||||
def num(self):
|
||||
if self._num is None:
|
||||
self._num = NumberRecognizer()
|
||||
return self._num
|
||||
|
||||
def start(self, screencap: Optional[bytes] = None) -> None:
|
||||
"""init with screencap"""
|
||||
retry_times = config.MAX_RETRYTIME
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue