Skip to content

Commit 1f88bb0

Browse files
格式修改
1 parent 8a059dc commit 1f88bb0

1 file changed

Lines changed: 65 additions & 20 deletions

File tree

agent/custom/action/map_locator.py

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from maa.context import Context
1414
from maa.custom_action import CustomAction
1515

16-
1716
logger = get_logger(__name__)
1817

1918

@@ -44,7 +43,9 @@ def __init__(
4443
debug_map_width: int = 900,
4544
max_processing_long_side: int = 6144,
4645
):
47-
self.big_map_path = Path(big_map_path) if big_map_path else self.default_big_map_path()
46+
self.big_map_path = (
47+
Path(big_map_path) if big_map_path else self.default_big_map_path()
48+
)
4849
self.mini_map_roi = mini_map_roi or [24, 14, 159, 157]
4950
self.debug = debug
5051
self.ratio_thresh = ratio_thresh
@@ -98,8 +99,12 @@ def locate(self, frame: np.ndarray) -> MapLocationResult:
9899
good_matches.append(m)
99100

100101
if len(good_matches) >= self.min_matches:
101-
src_pts = np.float32([kp_mini[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
102-
dst_pts = np.float32([self.big_points[m.trainIdx] for m in good_matches]).reshape(-1, 1, 2)
102+
src_pts = np.float32(
103+
[kp_mini[m.queryIdx].pt for m in good_matches]
104+
).reshape(-1, 1, 2)
105+
dst_pts = np.float32(
106+
[self.big_points[m.trainIdx] for m in good_matches]
107+
).reshape(-1, 1, 2)
103108

104109
transform, mask = cv2.estimateAffinePartial2D(
105110
src_pts,
@@ -115,15 +120,19 @@ def locate(self, frame: np.ndarray) -> MapLocationResult:
115120
).reshape(-1, 1, 2)
116121
polygon = cv2.transform(corners, transform)
117122

118-
player_src = np.float32([[mw * 0.5, mh * 0.5]]).reshape(-1, 1, 2)
123+
player_src = np.float32([[mw * 0.5, mh * 0.5]]).reshape(
124+
-1, 1, 2
125+
)
119126
player_dst = cv2.transform(player_src, transform)[0, 0]
120127
player_point = (
121128
int(player_dst[0] / self.big_map_scale),
122129
int(player_dst[1] / self.big_map_scale),
123130
)
124131
raw_player_point = player_point
125132

126-
player_point, polygon = self._filter_point(player_point, polygon, inliers, len(good_matches))
133+
player_point, polygon = self._filter_point(
134+
player_point, polygon, inliers, len(good_matches)
135+
)
127136
if player_point is None:
128137
player_point = self.last_center
129138

@@ -183,7 +192,9 @@ def show_debug(self, masked_minimap: np.ndarray, result: MapLocationResult) -> N
183192
interpolation=cv2.INTER_AREA,
184193
)
185194

186-
mini_view = cv2.resize(masked_minimap, (280, 280), interpolation=cv2.INTER_NEAREST)
195+
mini_view = cv2.resize(
196+
masked_minimap, (280, 280), interpolation=cv2.INTER_NEAREST
197+
)
187198
cv2.putText(
188199
mini_view,
189200
"mini map",
@@ -198,12 +209,24 @@ def show_debug(self, masked_minimap: np.ndarray, result: MapLocationResult) -> N
198209
canvas_height = max(mini_view.shape[0], map_view.shape[0])
199210
if mini_view.shape[0] < canvas_height:
200211
mini_view = np.concatenate(
201-
[mini_view, np.zeros((canvas_height - mini_view.shape[0], mini_view.shape[1], 3), dtype=np.uint8)],
212+
[
213+
mini_view,
214+
np.zeros(
215+
(canvas_height - mini_view.shape[0], mini_view.shape[1], 3),
216+
dtype=np.uint8,
217+
),
218+
],
202219
axis=0,
203220
)
204221
if map_view.shape[0] < canvas_height:
205222
map_view = np.concatenate(
206-
[map_view, np.zeros((canvas_height - map_view.shape[0], map_view.shape[1], 3), dtype=np.uint8)],
223+
[
224+
map_view,
225+
np.zeros(
226+
(canvas_height - map_view.shape[0], map_view.shape[1], 3),
227+
dtype=np.uint8,
228+
),
229+
],
207230
axis=0,
208231
)
209232

@@ -229,13 +252,18 @@ def _load_big_map(self) -> None:
229252
if self.big_map_scale < 1.0:
230253
big_map = cv2.resize(
231254
big_map,
232-
(int(self.origin_w * self.big_map_scale), int(self.origin_h * self.big_map_scale)),
255+
(
256+
int(self.origin_w * self.big_map_scale),
257+
int(self.origin_h * self.big_map_scale),
258+
),
233259
interpolation=cv2.INTER_AREA,
234260
)
235261

236262
self.big_map = cv2.convertScaleAbs(big_map, alpha=2.5, beta=-20)
237263
big_gray = cv2.cvtColor(self.big_map, cv2.COLOR_BGR2GRAY)
238-
cache_path = self.big_map_path.with_name(f"{self.big_map_path.stem}.sift_cache.npz")
264+
cache_path = self.big_map_path.with_name(
265+
f"{self.big_map_path.stem}.sift_cache.npz"
266+
)
239267
cache_meta = {
240268
"map_size": int(self.big_map_path.stat().st_size),
241269
"map_mtime_ns": self.big_map_path.stat().st_mtime_ns,
@@ -253,10 +281,14 @@ def _load_big_map(self) -> None:
253281
with np.load(cache_path, allow_pickle=False) as cache:
254282
cache_meta_raw = cache["meta"]
255283
saved_meta = json.loads(
256-
cache_meta_raw.item() if hasattr(cache_meta_raw, "item") else str(cache_meta_raw)
284+
cache_meta_raw.item()
285+
if hasattr(cache_meta_raw, "item")
286+
else str(cache_meta_raw)
257287
)
258288
if saved_meta == cache_meta:
259-
self.big_points = cache["keypoints"].astype(np.float32, copy=False)
289+
self.big_points = cache["keypoints"].astype(
290+
np.float32, copy=False
291+
)
260292
self.des_big = cache["descriptors"]
261293
except Exception as exc:
262294
logger.error(f"Failed to read feature cache: {exc}")
@@ -275,7 +307,11 @@ def _load_big_map(self) -> None:
275307
except Exception as exc:
276308
logger.error(f"Failed to save feature cache: {exc}")
277309

278-
if self.des_big is None or self.big_points is None or len(self.big_points) < self.min_matches:
310+
if (
311+
self.des_big is None
312+
or self.big_points is None
313+
or len(self.big_points) < self.min_matches
314+
):
279315
raise ValueError("Big map does not have enough feature points")
280316

281317
logger.debug(f"Big map keypoints: {len(self.big_points)}")
@@ -318,7 +354,10 @@ def _filter_point(
318354
accept_point = False
319355

320356
if accept_point and self.last_center is not None:
321-
jump = math.hypot(player_point[0] - self.last_center[0], player_point[1] - self.last_center[1])
357+
jump = math.hypot(
358+
player_point[0] - self.last_center[0],
359+
player_point[1] - self.last_center[1],
360+
)
322361
max_jump = 90 if inliers >= 8 or matches >= 14 else 60
323362

324363
if jump > max_jump:
@@ -342,7 +381,9 @@ def _filter_point(
342381
)
343382
accept_point = False
344383
else:
345-
logger.debug(f"Accept delayed jump raw={player_point} last={self.last_center} jump={jump:.1f}")
384+
logger.debug(
385+
f"Accept delayed jump raw={player_point} last={self.last_center} jump={jump:.1f}"
386+
)
346387
self.pending_center = None
347388
self.pending_count = 0
348389
else:
@@ -363,7 +404,9 @@ def _filter_point(
363404

364405
@AgentServer.custom_action("map_locator")
365406
class MapLocatorTestAction(CustomAction):
366-
def run(self, context: Context, argv: CustomAction.RunArg) -> CustomAction.RunResult:
407+
def run(
408+
self, context: Context, argv: CustomAction.RunArg
409+
) -> CustomAction.RunResult:
367410
params = _load_params(argv.custom_action_param)
368411
debug = bool(params.get("debug", False))
369412
frame_interval = float(params.get("frame_interval", 0.1))
@@ -374,14 +417,16 @@ def run(self, context: Context, argv: CustomAction.RunArg) -> CustomAction.RunRe
374417
mini_map_roi=params.get("mini_map_roi"),
375418
debug=debug,
376419
nfeatures=int(params.get("nfeatures", 0)),
377-
ratio_thresh=float(params.get("ratio_thresh", 0.8)),
420+
ratio_thresh=float(params.get("ratio_thresh", 0.85)),
378421
min_matches=int(params.get("min_matches", 8)),
379422
min_inliers=int(params.get("min_inliers", 4)),
380-
ransac_thresh=float(params.get("ransac_thresh", 12.0)),
423+
ransac_thresh=float(params.get("ransac_thresh", 14.0)),
381424
circle_padding=int(params.get("circle_padding", 15)),
382425
center_radius=int(params.get("center_radius", 11)),
383426
debug_map_width=int(params.get("debug_map_width", 900)),
384-
max_processing_long_side=int(params.get("max_processing_long_side", 6144)),
427+
max_processing_long_side=int(
428+
params.get("max_processing_long_side", 6144)
429+
),
385430
)
386431
except Exception as exc:
387432
logger.error(f"Map locator init failed: {exc}")

0 commit comments

Comments
 (0)