model与部分图片移至static下

This commit is contained in:
zhbaor 2024-11-12 12:46:09 +08:00
parent 78e6cca458
commit 40dd9c1f32
108 changed files with 106 additions and 365 deletions

View file

@ -3,7 +3,7 @@ from functools import partial
import cv2
import numpy as np
from mower.models import avatar, portrait
from mower.static import avatar, portrait
from mower.utils import typealias as tp
from mower.utils.image import cmatch, cropimg, loadres, thres2
from mower.utils.log import logger

View file

@ -1,117 +1,51 @@
import os
from pathlib import Path
import cv2
import numpy as np
from .image import loadres
from mower.utils.image import cropimg, load_static
drone_template = [load_static(f"drone_count/{i}", True) for i in range(10)]
time_template = [load_static(f"orders_time/{i}", True) for i in range(10)]
class DigitReader:
def __init__(self, template_dir=None):
if not template_dir:
template_dir = Path(os.path.dirname(os.path.abspath(__file__))) / Path(
"templates"
)
if not isinstance(template_dir, Path):
template_dir = Path(template_dir)
self.time_template = []
self.drone_template = []
for i in range(10):
self.time_template.append(loadres(f"orders_time/{i}", True))
self.drone_template.append(loadres(f"drone_count/{i}", True))
def get_drone(gray):
gray = cropimg(gray, ((1144, 32), (1225, 76)))
result = {}
for j in range(10):
res = cv2.matchTemplate(gray, drone_template[j], cv2.TM_CCORR_NORMED)
threshold = 0.95
loc = np.where(res >= threshold)
for i in range(len(loc[0])):
offset = loc[1][i]
accept = True
for o in result:
if abs(o - offset) < 5:
accept = False
break
if accept:
result[loc[1][i]] = j
return int("".join([str(result[k]) for k in sorted(result)]))
def get_drone(self, img_grey, h=1080, w=1920):
drone_part = img_grey[
h * 32 // 1080 : h * 76 // 1080, w * 1144 // 1920 : w * 1225 // 1920
]
drone_part = cv2.resize(drone_part, (81, 44), interpolation=cv2.INTER_AREA)
result = {}
for j in range(10):
res = cv2.matchTemplate(
drone_part,
self.drone_template[j],
cv2.TM_CCORR_NORMED,
)
threshold = 0.95
loc = np.where(res >= threshold)
for i in range(len(loc[0])):
offset = loc[1][i]
accept = True
for o in result:
if abs(o - offset) < 5:
accept = False
break
if accept:
result[loc[1][i]] = j
ch = [str(result[k]) for k in sorted(result)]
return int("".join(ch))
def get_time(self, img_grey, h, w):
digit_part = img_grey[h * 510 // 1080 : h * 543 // 1080, w * 499 // 1920 : w]
digit_part = cv2.resize(digit_part, (1421, 33), interpolation=cv2.INTER_AREA)
result = {}
for j in range(10):
res = cv2.matchTemplate(
digit_part,
self.time_template[j],
cv2.TM_CCOEFF_NORMED,
)
threshold = 0.85
loc = np.where(res >= threshold)
for i in range(len(loc[0])):
x = loc[1][i]
accept = True
for o in result:
if abs(o - x) < 5:
accept = False
break
if accept:
if len(result) == 0:
digit_part = digit_part[:, loc[1][i] - 5 : loc[1][i] + 116]
offset = loc[1][0] - 5
for m in range(len(loc[1])):
loc[1][m] -= offset
result[loc[1][i]] = j
ch = [str(result[k]) for k in sorted(result)]
return f"{ch[0]}{ch[1]}:{ch[2]}{ch[3]}:{ch[4]}{ch[5]}"
def 识别制造加速总剩余时间(self, img_grey, h, w):
时间部分 = img_grey[
h * 665 // 1080 : h * 709 // 1080, w * 750 // 1920 : w * 960 // 1920
]
时间部分 = cv2.resize(
时间部分, (210 * 58 // 71, 44 * 58 // 71), interpolation=cv2.INTER_AREA
)
result = {}
for j in range(10):
res = cv2.matchTemplate(
时间部分,
self.drone_template[j],
cv2.TM_CCOEFF_NORMED,
)
threshold = 0.85
loc = np.where(res >= threshold)
for i in range(len(loc[0])):
offset = loc[1][i]
accept = True
for o in result:
if abs(o - offset) < 5:
accept = False
break
if accept:
result[loc[1][i]] = j
ch = [str(result[k]) for k in sorted(result)]
print(ch)
if len(ch) == 6:
return (
int(f"{ch[0]}{ch[1]}"),
int(f"{ch[2]}{ch[3]}"),
int(f"{ch[4]}{ch[5]}"),
)
else:
return (
int(f"{ch[0]}{ch[1]}{ch[2]}"),
int(f"{ch[3]}{ch[4]}"),
int(f"{ch[5]}{ch[6]}"),
)
def get_time(gray):
gray = cropimg(gray, ((499, 510), (1920, 543)))
result = {}
for i in range(10):
res = cv2.matchTemplate(gray, time_template[i], cv2.TM_CCOEFF_NORMED)
threshold = 0.85
loc = np.where(res >= threshold)
for i in range(len(loc[0])):
x = loc[1][i]
accept = True
for o in result:
if abs(o - x) < 5:
accept = False
break
if accept:
if len(result) == 0:
gray = gray[:, loc[1][i] - 5 : loc[1][i] + 116]
offset = loc[1][0] - 5
for m in range(len(loc[1])):
loc[1][m] -= offset
result[loc[1][i]] = i
ch = [str(result[k]) for k in sorted(result)]
return f"{ch[0]}{ch[1]}:{ch[2]}{ch[3]}:{ch[4]}{ch[5]}"

View file

@ -42,6 +42,14 @@ def loadres(res: tp.Res, gray: bool = False) -> Union[tp.Image, tp.GrayImage]:
return loadimg(res2path(res), gray)
def load_static(name: str, gray: bool = False) -> tp.Image | tp.GrayImage:
filename = f"@install/mower/static/{name}"
if not filename.endswith(".jpg"):
filename += ".png"
filename = get_path(filename)
return loadimg.__wrapped__(filename, gray)
@lru_cache(maxsize=256)
def loadimg(
filename: str, gray: bool = False, bg: tuple[int] = (255, 255, 255, 255)

View file

@ -30,7 +30,7 @@ def keypoints(img: tp.GrayImage, mask: tp.GrayImage | None = None):
return ORB_no_pyramid.detectAndCompute(img, mask)
with lzma.open(f"{__rootdir__}/models/svm.model", "rb") as f:
with lzma.open(f"{__rootdir__}/static/svm.model", "rb") as f:
SVC = pickle.loads(f.read())

View file

@ -2,7 +2,7 @@ from typing import Literal
import cv2
from mower.models import Digtal
from mower.static import Digital
from mower.utils import config
from mower.utils import typealias as tp
from mower.utils.image import cropimg, thres2
@ -48,11 +48,11 @@ class NumberRecognizer:
target_range: list = range(10),
) -> str:
if font == "riic_base":
templates = Digtal().riic_base_digits
templates = Digital().riic_base_digits
elif font == "noto":
templates = Digtal().noto_sans
templates = Digital().noto_sans
elif font == "secret_front":
templates = Digtal().secret_front
templates = Digital().secret_front
value = ""
for i in range(len(rect)):
if len(rect[i]) != 4:

View file

@ -27,6 +27,7 @@ class Recognizer:
def __init__(self, screencap: Optional[bytes] = None) -> None:
self.w = 1920
self.h = 1080
self.num = NumberRecognizer()
self.clear()
self.start(screencap)
self.loading_time = 0
@ -64,12 +65,6 @@ 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

View file

@ -239,7 +239,7 @@ class Calc:
LEVELS: List[Level] = []
with lzma.open(f"{__rootdir__}/models/levels.pkl", "rb") as f:
with lzma.open(f"{__rootdir__}/static/levels.pkl", "rb") as f:
level_table = pickle.load(f)
for data in level_table:
LEVELS.append(Level.from_json(data))

View file

@ -71,59 +71,6 @@ Res = Literal[
"credit_visiting",
"depot",
"depot_empty",
"depot_num/digit_0",
"depot_num/digit_1",
"depot_num/digit_2",
"depot_num/digit_3",
"depot_num/digit_4",
"depot_num/digit_5",
"depot_num/digit_6",
"depot_num/digit_7",
"depot_num/digit_8",
"depot_num/digit_9",
"depot_num/digit_91",
"depot_num/digit_point",
"digtal/noto_sans/0",
"digtal/noto_sans/1",
"digtal/noto_sans/2",
"digtal/noto_sans/3",
"digtal/noto_sans/4",
"digtal/noto_sans/5",
"digtal/noto_sans/6",
"digtal/noto_sans/7",
"digtal/noto_sans/8",
"digtal/noto_sans/9",
"digtal/riic_base_digits/0",
"digtal/riic_base_digits/1",
"digtal/riic_base_digits/2",
"digtal/riic_base_digits/3",
"digtal/riic_base_digits/4",
"digtal/riic_base_digits/5",
"digtal/riic_base_digits/6",
"digtal/riic_base_digits/7",
"digtal/riic_base_digits/8",
"digtal/riic_base_digits/9",
"digtal/secret_front/0",
"digtal/secret_front/1",
"digtal/secret_front/1A",
"digtal/secret_front/2",
"digtal/secret_front/2A",
"digtal/secret_front/2B",
"digtal/secret_front/3",
"digtal/secret_front/3A",
"digtal/secret_front/3B",
"digtal/secret_front/3C",
"digtal/secret_front/4",
"digtal/secret_front/5",
"digtal/secret_front/6",
"digtal/secret_front/7",
"digtal/secret_front/8",
"digtal/secret_front/9",
"digtal/secret_front/结局A",
"digtal/secret_front/结局B",
"digtal/secret_front/结局C",
"digtal/secret_front/结局D",
"digtal/secret_front/结局E",
"double_confirm/exit",
"double_confirm/explore",
"double_confirm/friend",
@ -139,16 +86,6 @@ Res = Literal[
"double_confirm/sss_abandon_drop",
"double_confirm/voice",
"drone",
"drone_count/0",
"drone_count/1",
"drone_count/2",
"drone_count/3",
"drone_count/4",
"drone_count/5",
"drone_count/6",
"drone_count/7",
"drone_count/8",
"drone_count/9",
"episode",
"factory_accelerate",
"factory_collect",
@ -331,16 +268,6 @@ Res = Literal[
"order_label",
"order_ready",
"order_switching_notice",
"orders_time/0",
"orders_time/1",
"orders_time/2",
"orders_time/3",
"orders_time/4",
"orders_time/5",
"orders_time/6",
"orders_time/7",
"orders_time/8",
"orders_time/9",
"originite",
"product/先锋双芯片",
"product/医疗双芯片",