将NumberRecognizer移入recog,简化各处number写法

This commit is contained in:
Elaina 2024-10-27 16:26:58 +08:00
parent f084957e1e
commit 429f919d61
20 changed files with 101 additions and 501 deletions

View file

@ -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