Skip to content

Commit a523bfa

Browse files
authored
feat: 允许上传文件作为待匹配图片 (#145)
1 parent 4609e0d commit a523bfa

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

src/MaaDebugger/maafw/__init__.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
import re
2+
import io
23
from pathlib import Path
34
from typing import Callable, List, Optional, Tuple, Union
45

56
from asyncify import asyncify
67
from PIL import Image
7-
from maa.controller import AdbController, Win32Controller
8+
from maa.controller import AdbController, Win32Controller, CustomController
89
from maa.context import Context, ContextEventSink
910
from maa.tasker import Tasker, RecognitionDetail
1011
from maa.resource import Resource, ResourceEventSink
1112
from maa.toolkit import Toolkit, AdbDevice, DesktopWindow
1213
from maa.agent_client import AgentClient
1314
from maa.library import Library
1415
from maa.event_sink import NotificationType
16+
import numpy as np
1517

16-
from ..utils import cvmat_to_image
18+
from ..utils.img_tools import cvmat_to_image, rgb_to_bgr
19+
20+
21+
class MyCustomController(CustomController):
22+
def __init__(self, img_bytes: bytes):
23+
super().__init__()
24+
25+
img = Image.open(io.BytesIO(img_bytes))
26+
self.ndarray = rgb_to_bgr(np.array(img))
27+
28+
def connect(self) -> bool:
29+
return True
30+
31+
def request_uuid(self) -> str:
32+
return "0"
33+
34+
def screencap(self) -> np.ndarray:
35+
return self.ndarray
1736

1837

1938
class MaaFW:
2039

2140
resource: Optional[Resource]
22-
controller: Union[AdbController, Win32Controller, None]
41+
controller: Union[AdbController, Win32Controller, CustomController, None]
2342
tasker: Optional[Tasker]
2443
agent: Optional[AgentClient]
2544
context_event_sink: Optional[ContextEventSink]
@@ -89,6 +108,12 @@ def connect_win32hwnd(
89108

90109
return True, None
91110

111+
def connect_custom_controller(self, img_bytes) -> Tuple[bool, Optional[str]]:
112+
self.controller = MyCustomController(img_bytes)
113+
self.controller.post_connection().wait()
114+
115+
return True, None
116+
92117
@asyncify
93118
def load_resource(self, dir: List[Path]) -> Tuple[bool, Optional[str]]:
94119
if not self.resource:
@@ -149,7 +174,20 @@ def run_task(
149174
if not AgentClient().register_sink(self.resource, self.controller, self.tasker):
150175
return False, "Failed to register Agent sink."
151176

152-
return self.tasker.post_task(entry, pipeline_override).wait().succeeded, None
177+
if isinstance(self.controller, CustomController):
178+
# disable action
179+
pipeline_override.update(
180+
{entry: {"action": {"type": "DoNothing"}, "next": []}}
181+
)
182+
return (
183+
self.tasker.post_task(entry, pipeline_override).wait().succeeded,
184+
None,
185+
)
186+
else:
187+
return (
188+
self.tasker.post_task(entry, pipeline_override).wait().succeeded,
189+
None,
190+
)
153191

154192
@asyncify
155193
def stop_task(self) -> None:
@@ -176,6 +214,9 @@ def click(self, x, y) -> bool:
176214
if not self.controller:
177215
return False
178216

217+
if isinstance(self.controller, CustomController):
218+
return False
219+
179220
return self.controller.post_click(x, y).wait().succeeded
180221

181222
@asyncify
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ def cvmat_to_image(cvmat: ndarray) -> Image.Image:
66
pil = Image.fromarray(cvmat)
77
b, g, r = pil.split()
88
return Image.merge("RGB", (r, g, b))
9+
10+
11+
def rgb_to_bgr(arr: ndarray) -> ndarray:
12+
"""RGB -> BGR 转换"""
13+
if arr.ndim == 3 and arr.shape[2] >= 3:
14+
return arr[:, :, ::-1].copy()
15+
else:
16+
return arr

src/MaaDebugger/webpage/index_page/master_control.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def connect_control():
3939
with ui.tabs() as tabs:
4040
adb = ui.tab("Adb")
4141
win32 = ui.tab("Win32")
42+
custom = ui.tab("Custom")
4243

4344
tab_panels = ui.tab_panels(tabs, value="Adb").bind_value(STORAGE, "controller_type")
4445
with tab_panels:
@@ -49,6 +50,10 @@ def connect_control():
4950
with ui.row(align_items="center").classes("w-full"):
5051
connect_win32_control()
5152

53+
with ui.tab_panel(custom):
54+
with ui.row(align_items="center").classes("w-full"):
55+
connect_custom_control()
56+
5257
os_type = system.get_os_type()
5358
if os_type != system.OSTypeEnum.Windows:
5459
win32.disable()
@@ -303,6 +308,24 @@ def on_change_hwnd_select(value: Optional[str]):
303308
hwnd_input.value = value
304309

305310

311+
def connect_custom_control():
312+
def on_upload(e):
313+
GlobalStatus.ctrl_connecting = Status.RUNNING
314+
try:
315+
maafw.connect_custom_controller(e.content.read())
316+
except Exception as e:
317+
GlobalStatus.ctrl_connecting = Status.FAILED
318+
ui.notify(
319+
f"Failed to load image. {e}", position="bottom-right", type="negative"
320+
)
321+
return
322+
323+
GlobalStatus.ctrl_connecting = Status.SUCCEEDED
324+
325+
StatusIndicator(GlobalStatus, "ctrl_connecting")
326+
ui.upload(auto_upload=True, on_upload=lambda e: on_upload(e))
327+
328+
306329
def screenshot_control():
307330
with (
308331
ui.row()

src/MaaDebugger/webpage/reco_page/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from nicegui import ui
44

5-
from ...utils import cvmat_to_image
5+
from ...utils.img_tools import cvmat_to_image
66
from ...maafw import maafw, RecognitionDetail
77

88

0 commit comments

Comments
 (0)