focus.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import numpy as np
  2. from rich.table import Table
  3. from rich.text import Text
  4. from cached_property import cached_property
  5. from module.logger import logger
  6. from oashya.labels import id2label, id2name
  7. from oashya.labels import CLASSINDEX as CI
  8. class Focus:
  9. def __init__(self, inputs: list[tuple]):
  10. self._id: int = inputs[0]
  11. self._class: int = inputs[1]
  12. self._conf: int = inputs[2]
  13. self._cx: int = inputs[3]
  14. self._cy: int = inputs[4]
  15. self._w: int = inputs[5]
  16. self._h: int = inputs[6]
  17. self._v: float = inputs[7]
  18. # -----------------------
  19. self._omega: float = 0.
  20. self._omega_buff: float = 0.
  21. def __eq__(self, o):
  22. return self._id == o._id or o._class == self._class
  23. def __str__(self):
  24. return f"""
  25. id: {self._id}
  26. class: {self._class}
  27. label: {id2label(self._class)}
  28. name: {id2name(self._class)}
  29. conf: {self._conf}
  30. xywh: ({self._cx}, {self._cy}, {self._w}, {self._h})
  31. velocity: {self._v}"""
  32. def omega(self, z):
  33. __omega = z[self._cy, self._cx]
  34. self._omega = __omega
  35. return __omega
  36. def set_omega(self, value):
  37. self._omega = value
  38. def show(self):
  39. table = Table(show_lines=True)
  40. table.add_column('Property', header_style="bright_cyan", style="cyan", no_wrap=True)
  41. table.add_column("Value", style="green")
  42. table.add_column('Property', header_style="bright_cyan", style="cyan", no_wrap=True)
  43. table.add_column("Value", style="green")
  44. table.add_row('id', str(self._id), 'conf', str(self._conf))
  45. table.add_row('class', str(self._class), 'xywh', f'({self._cx}, {self._cy}, {self._w}, {self._h})')
  46. table.add_row('label', id2label(self._class), 'velocity', str(self._v))
  47. table.add_row('name', f'{id2name(self._class)}', 'omega', str(self._omega))
  48. logger.print(table, justify='center')
  49. def update(self, focus):
  50. self._id = focus._id
  51. self._class = focus._class
  52. self._conf = focus._conf
  53. self._cx = focus._cx
  54. self._cy = focus._cy
  55. self._w = focus._w
  56. self._h = focus._h
  57. self._v = focus._v
  58. def decision(self, tracks, strategy, state) -> list:
  59. buff_omega, buff_cx, buff_cy, buff_v, buff_class = self.omega_buff(tracks, strategy['invite_friend'])
  60. if self._omega > buff_omega:
  61. buffed = False
  62. target_x = self._cx + self._v * 100 - (self._w // 2)
  63. target_y = self._cy - 40
  64. else:
  65. buffed = True
  66. target_x = buff_cx + buff_v * 100
  67. target_y = buff_cy - 40 # top left corner
  68. _r = self.r(vector=state, omega=self._omega, omega_buff=self._omega_buff)
  69. throw = True if _r > 0 else False
  70. # x, y, throw, number
  71. return [target_x, target_y, throw, 10]
  72. def omega_buff(self, tracks, invite_friend: bool) -> float:
  73. max_omega = 0.
  74. max_cx = 0
  75. max_cy = 0
  76. max_v = 0
  77. max_class = 0
  78. for _id, _class, _conf, _cx, _cy, _w, _h, _v in tracks:
  79. _current_omega = 0.
  80. match _class:
  81. case CI.BUFF_004: _current_omega = 2.2
  82. case CI.BUFF_006: _current_omega = 2.0
  83. case CI.BUFF_007: _current_omega = 2.0 if invite_friend and _cx <= 1000 else 0
  84. case CI.BUFF_002: _current_omega = 2.0 if self._omega >= 1.5 else 0
  85. case CI.BUFF_003: _current_omega = 2.0 if self._omega >= 1.5 else 0
  86. case CI.BUFF_003: _current_omega = 2.0 if self._omega >= 1.5 and _cx <= 640 else 0
  87. case _: pass
  88. if _current_omega > max_omega:
  89. max_omega = _current_omega
  90. max_cx = _cx
  91. max_cy = _cy
  92. max_v = _v
  93. max_class = _class
  94. return max_omega, max_cx, max_cy, max_v, max_class
  95. @classmethod
  96. def r(cls, vector: list, omega: float, omega_buff: float) -> float:
  97. _omega = max(omega, omega_buff)
  98. tau = - 140 / (max(101, vector[0]) - 100)
  99. upsilon = (vector[1] / 250 - vector[2] / 35)
  100. upsilon = 100 * (upsilon**2 if upsilon > 0 else - upsilon**2)
  101. result = _omega + tau + upsilon - 0.6
  102. # print(f"total: {result:.4f} | {_omega:.4f} | {tau:.4f} | {upsilon:.4f}")
  103. return result