Просмотр исходного кода

feat(Hyakkiyakou): Display graph by different weights multiplied by a Gaussian kernel

ryland 2 лет назад
Родитель
Сommit
462e885159

+ 3 - 1
tasks/Hyakkiyakou/.gitignore

@@ -1,3 +1,5 @@
 
 /utils/exp_gen.py
-/temp
+/temp
+/agent/tagent.py
+/agent/tfocus.py

+ 86 - 1
tasks/Hyakkiyakou/agent/agent.py

@@ -1,12 +1,97 @@
+import cv2
+import time
+import copy
+import numpy as np
 
+from datetime import datetime
+from pathlib import Path
+from numpy import uint8, fromfile
+
+from oashya.tracker import Tracker
+from module.logger import logger
+
+
+def generate_gaussian_patch(size=(300, 300), mean=0, std_dev=60):
+    x = np.linspace(-150, 150, size[1])
+    y = np.linspace(-150, 150, size[0])
+    x, y = np.meshgrid(x, y)
+    z = np.exp(-(((x - mean) ** 2 + (y - mean) ** 2) / (2 * std_dev ** 2)))
+    return z
+
+
+def embed_patch_in_canvas(canvas, patch, position=(0, 0), patch_size=(300, 300)):
+    canvas_height, canvas_width = 720, 1280
+    patch_height, patch_width = patch_size
+
+    x1 = position[0] - patch_width // 2
+    y1 = position[1] - patch_height // 2
+    x2 = position[0] + patch_width // 2
+    y2 = position[1] + patch_height // 2
+    x1 = max(0, x1)
+    y1 = max(0, y1)
+    x2 = min(canvas_width, x2)
+    y2 = min(canvas_height, y2)
+    embed_w = x2 - x1
+    embed_h = y2 - y1
+    if embed_w < patch_width or embed_h < patch_height:
+        _patch = patch[:embed_h, :embed_w]
+    else:
+        _patch = patch
+    # Embed the patch in the canvas
+    canvas[y1:y2, x1:x2] += _patch
+    return canvas
 
 
 
 
 class Agent:
-    pass
+    GAUSSIAN = generate_gaussian_patch()
+    MIN_BUFF: int = 0
+    MAX_BUFF: int = 6
+    MIN_N: int = 7
+    MAX_N: int = 18
+    MIN_G: int = 19
+    MAX_G: int = 35
+    MIN_R: int = 36
+    MAX_R: int = 72
+    MIN_SR: int = 73
+    MAX_SR: int = 137
+    MIN_SSR: int = 138
+    MAX_SSR: int = 183
+    MIN_SP: int = 184
+    MAX_SP: int = 218
+    BUFF_001 = 0
+    BUFF_002 = 1
+    BUFF_003 = 2
+    BUFF_004 = 3
+    BUFF_005 = 4
+    BUFF_006 = 5
+    BUFF_007 = 6
 
+    @classmethod
+    def gamma(cls, tracks: list[tuple],
+              weights: list[float] = [1., 1., 1., .6, 0.3, 0.3],
+              priorities: list[int] = []) -> np.ndarray:
+        """
 
+        @param tracks:
+        @param weights: sp, ssr, sr, r, n, g
+        @param priorities:
+        @return:
+        """
+        z = np.zeros((720, 1280), dtype=np.float32)
+        for _id, _class, _conf, _cx, _cy, _w, _h, _v in tracks:
+            weight = 1.
+            match _class:
+                case _ if cls.MIN_G <= _class <= cls.MAX_G: weight = weights[5]
+                case _ if cls.MIN_N <= _class <= cls.MAX_N: weight = weights[4]
+                case _ if cls.MIN_R <= _class <= cls.MAX_R: weight = weights[3]
+                case _ if cls.MIN_SR <= _class <= cls.MAX_SR: weight = weights[2]
+                case _ if cls.MIN_SSR <= _class <= cls.MAX_SSR: weight = weights[1]
+                case _ if cls.MIN_SP <= _class <= cls.MAX_SP: weight = weights[0]
+                case cls.BUFF_005: weight = -1.  # freeze
+            z = embed_patch_in_canvas(canvas=z, patch=weight * Agent.GAUSSIAN, position=(_cx, _cy))
+        return z
 
 
 

+ 0 - 0
tasks/Hyakkiyakou/agent/focus.py


+ 7 - 6
tasks/Hyakkiyakou/config.py

@@ -20,13 +20,14 @@ class HyakkiyakouConfig(ConfigBase):
     hya_invite_friend: bool = Field(default=False, description='hya_invite_friend_help')
     # 自动调整豆子数量
     hya_auto_bean: bool = Field(default=False, description='hya_auto_bean_help')
-    hya_sp: bool = Field(default=False, description='hya_sp_help')
-    hya_ssr: bool = Field(default=False, description='hya_ssr_help')
-    hya_sr: bool = Field(default=False, description='hya_sr_help')
-    hya_r: bool = Field(default=False, description='hya_r_help')
-    hya_n: bool = Field(default=False, description='hya_n_help')
+    hya_priorities: str = Field(default='', description='hya_priorities_help')
+    hya_sp: float = Field(default=1., description='hya_sp_help')
+    hya_ssr: float = Field(default=1., description='hya_ssr_help')
+    hya_sr: float = Field(default=0.5, description='hya_sr_help')
+    hya_r: float = Field(default=0, description='hya_r_help')
+    hya_n: float = Field(default=0, description='hya_n_help')
     # 呱太和N卡不是一个东西
-    hya_g: bool = Field(default=False, description='hya_g_help')
+    hya_g: float = Field(default=0, description='hya_g_help')
 
 
 class HyakkiyakouModels(ConfigBase):

+ 3 - 2
tasks/Hyakkiyakou/debugger.py

@@ -16,7 +16,7 @@ from tasks.base_task import BaseTask
 from module.logger import logger
 
 
-def test_track(show : bool = False):
+def test_track(show: bool = False):
     tracker = Tracker()
     # --------------------------------
     #
@@ -33,12 +33,13 @@ def test_track(show : bool = False):
         img = cv2.imdecode(fromfile(str(file), dtype=uint8), -1)
         image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
         results = tracker(image=image)
+        print(results)
         save_file = folder_save / f'{file.stem}.png'
         draw_image = draw_tracks(image, results)
         if show:
             cv2.imshow('OAS Tracker', draw_image)
+            cv2.waitKey(1000)
             time.sleep(2)
-            cv2.waitKey(10000)
             print('Press any key to continue')
         else:
             cv2.imwrite(str(save_file), draw_image)

+ 0 - 1
tasks/Hyakkiyakou/script_task.py

@@ -53,7 +53,6 @@ class ScriptTask(GameUi, HyaSlave):
     def debugger(self) -> Debugger:
         return Debugger()
 
-
     def run(self):
         self.ui_get_current_page()
         self.ui_goto(page_hyakkiyakou)