agent.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import cv2
  2. import time
  3. import copy
  4. import numpy as np
  5. from datetime import datetime
  6. from pathlib import Path
  7. from numpy import uint8, fromfile
  8. from cached_property import cached_property
  9. from oashya.tracker import Tracker
  10. from oashya.labels import CLASSINDEX as CI
  11. from oashya.labels import id2label, id2name
  12. from module.logger import logger
  13. from tasks.Hyakkiyakou.agent.focus import Focus
  14. from tasks.Hyakkiyakou.debugger import Debugger
  15. def generate_gaussian_patch(size=(300, 300), mean=0, std_dev=60):
  16. x = np.linspace(-150, 150, size[1])
  17. y = np.linspace(-150, 150, size[0])
  18. x, y = np.meshgrid(x, y)
  19. z = np.exp(-(((x - mean) ** 2 + (y - mean) ** 2) / (2 * std_dev ** 2)))
  20. return z
  21. def embed_patch_in_canvas(canvas, patch, position=(0, 0), patch_size=(300, 300)):
  22. canvas_height, canvas_width = 720, 1280
  23. patch_height, patch_width = patch_size
  24. pos_x, pos_y = position
  25. pos_x = max(0, min(pos_x, canvas_width))
  26. pos_y = max(0, min(pos_y, canvas_width))
  27. x1 = pos_x - patch_width // 2
  28. y1 = pos_y - patch_height // 2
  29. x2 = pos_x + patch_width // 2
  30. y2 = pos_y + patch_height // 2
  31. x1 = max(0, x1)
  32. y1 = max(0, y1)
  33. x2 = min(canvas_width, x2)
  34. y2 = min(canvas_height, y2)
  35. embed_w = x2 - x1
  36. embed_h = y2 - y1
  37. if embed_w <= 0 or embed_h <= 0:
  38. logger.warning(f'Cannot embed patch in canvas: ({position})')
  39. return canvas
  40. if embed_w < patch_width or embed_h < patch_height:
  41. _patch = patch[:embed_h, :embed_w]
  42. else:
  43. _patch = patch
  44. # Embed the patch in the canvas
  45. canvas[y1:y2, x1:x2] += _patch
  46. return canvas
  47. class Agent:
  48. GAUSSIAN = generate_gaussian_patch()
  49. OBSERVE_THRESHOLD = 0.6
  50. # --------------------------------------------------------------------------------
  51. def __init__(self, strategy: dict = None):
  52. self.z = np.zeros((720, 1280), dtype=np.float32)
  53. self.focus: Focus = None
  54. #
  55. strategy: dict = strategy if strategy is not None else {
  56. # 'weights': [1., 1., 1., 0.6, 0.3, 0.3],
  57. 'priorities': [],
  58. 'invite_friend': False,
  59. 'auto_bean': False
  60. }
  61. self.strategy = strategy
  62. self.weights: list[float] = strategy.get('weights', [1., 1., 0.7, 0.3, 0., 0.])
  63. self.priorities: list[int] = strategy.get('priorities', [])
  64. self.invite_friend: bool = strategy.get('invite_friend', False)
  65. self.auto_bean: bool = strategy.get('auto_bean', False)
  66. #
  67. self.last_throw_time = datetime.now()
  68. self.dbg_throw: int = 0
  69. self.dbg_throw_n: int = 0
  70. @classmethod
  71. def gamma(cls, tracks: list[tuple],
  72. weights: list[float],
  73. priorities: list[int] = []) -> np.ndarray:
  74. """
  75. @param tracks:
  76. @param weights: sp, ssr, sr, r, n, g
  77. @param priorities:
  78. @return:
  79. """
  80. z = np.zeros((720, 1280), dtype=np.float32)
  81. for _id, _class, _conf, _cx, _cy, _w, _h, _v in tracks:
  82. weight = 1.
  83. mu = 0.8 + (_cx * 0.2) / 1280
  84. match _class:
  85. case _ if CI.MIN_G <= _class <= CI.MAX_G: weight = weights[5]
  86. case _ if CI.MIN_N <= _class <= CI.MAX_N: weight = weights[4]
  87. case _ if _class != CI.R_008 and _class != CI.R_007 and (CI.MIN_R <= _class <= CI.MAX_R):
  88. weight = weights[3] # 不要童男童女
  89. case _ if CI.MIN_SR <= _class <= CI.MAX_SR: weight = weights[2]
  90. case _ if CI.MIN_SSR <= _class <= CI.MAX_SSR: weight = 1.5 * weights[1]
  91. case _ if CI.MIN_SP <= _class <= CI.MAX_SP: weight = 1.5 * weights[0]
  92. # case CI.BUFF_005: # freeze
  93. # weight = -1.
  94. # _cy += 100
  95. case _: continue
  96. for priority in priorities: # 我的代码在你之上
  97. if priority == _class:
  98. weight = 1.7
  99. break
  100. if weight == 0.:
  101. continue
  102. z = embed_patch_in_canvas(canvas=z, patch=(mu * weight) * Agent.GAUSSIAN, position=(_cx, _cy))
  103. return z
  104. @classmethod
  105. def argmax_gamma(cls, z: np.ndarray, tracks: list[tuple]) -> Focus:
  106. max_y, max_x = np.unravel_index(np.argmax(z), z.shape)
  107. max_variance = 2000
  108. max_index = -1
  109. for index, track in enumerate(tracks):
  110. _id, _class, _conf, _cx, _cy, _w, _h, _v = track
  111. variance = (max_x - _cx)**2 + (max_y - _cy)**2
  112. if variance < max_variance:
  113. max_index = index
  114. max_variance = variance
  115. return Focus(inputs=tracks[max_index])
  116. def decision(self, tracks: list[tuple], state: list, freeze: bool = False) -> list:
  117. not_decision = [-1, -1, False, -1]
  118. if not tracks:
  119. return not_decision
  120. # Cache the time interval for scattering beans
  121. new_time = datetime.now()
  122. delta_time = 1000 * (new_time - self.last_throw_time).total_seconds() # ms
  123. self.check_observe(tracks=tracks)
  124. if self.focus is None:
  125. return not_decision
  126. result = self.focus.decision(tracks=tracks, strategy=self.strategy, state=[delta_time] + state, freeze=freeze)
  127. if result[2]:
  128. self.last_throw_time = new_time
  129. self.dbg_throw += 1
  130. else:
  131. self.dbg_throw_n += 1
  132. return result
  133. def check_observe(self, tracks: list[tuple]):
  134. z = Agent.gamma(tracks=tracks, weights=self.weights, priorities=self.priorities)
  135. focus = Agent.argmax_gamma(z=z, tracks=tracks)
  136. omega = focus.omega(z)
  137. if omega < self.OBSERVE_THRESHOLD:
  138. if self.focus is not None:
  139. logger.info(f'Focus disappear')
  140. self.focus = None
  141. return
  142. if self.focus is None or self.focus != focus:
  143. logger.info(f'Focus changed, now: {id2name(focus._class)}')
  144. self.focus = focus
  145. self.focus.set_omega(omega)
  146. elif self.focus == focus:
  147. self.focus.update(focus)
  148. self.focus.set_omega(omega)
  149. # if Debugger.info_enable:
  150. # self.focus.show()