script_task.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import cv2
  6. import numpy as np
  7. from datetime import datetime, timedelta
  8. from numpy import uint8, fromfile
  9. from random import choice
  10. from cached_property import cached_property
  11. # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
  12. from oashya.tracker import Tracker
  13. from oashya.utils import draw_tracks
  14. from module.exception import TaskEnd
  15. from module.logger import logger
  16. from module.exception import RequestHumanTakeover
  17. from tasks.GameUi.game_ui import GameUi
  18. from tasks.GameUi.page import page_hyakkiyakou
  19. from tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
  20. from tasks.Hyakkiyakou.config import InferenceEngine, ModelPrecision
  21. from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
  22. from tasks.Hyakkiyakou.agent.agent import Agent
  23. from tasks.Hyakkiyakou.slave.hya_slave import HyaSlave
  24. from tasks.Hyakkiyakou.debugger import Debugger
  25. def plot_save(image, boxes):
  26. color_palette = np.random.uniform(0, 255, size=(226, 3))
  27. for box in boxes:
  28. _cls = box[0]
  29. _scores = box[1]
  30. _x, _y, _w, _h = box[2]
  31. x1 = int(_x - _w / 2)
  32. y1 = int(_y - _h / 2)
  33. x2 = int(_x + _w / 2)
  34. y2 = int(_y + _h / 2)
  35. cv2.rectangle(image, (x1, y1), (x2, y2), color_palette[_cls], 2)
  36. #
  37. cv2.putText(image, f'{_cls} {_scores:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color_palette[_cls],
  38. 2)
  39. save_file = './tasks/Hyakkiyakou/temp/image.png'
  40. cv2.imwrite(save_file, image)
  41. class ScriptTask(GameUi, HyaSlave):
  42. @property
  43. def _config(self):
  44. return self.config.hyakkiyakou
  45. @cached_property
  46. def tracker(self) -> Tracker:
  47. hyakkiyakou_models = self._config.hyakkiyakou_models
  48. conf = hyakkiyakou_models.conf_threshold
  49. nms = hyakkiyakou_models.iou_threshold
  50. if conf < 0.2 or conf > 1:
  51. raise RequestHumanTakeover('conf_threshold should be in [0.2, 1]')
  52. if nms < 0.2 or nms > 1:
  53. raise RequestHumanTakeover('iou_threshold should be in [0.2, 1]')
  54. inf_en = 'onnxruntime' if hyakkiyakou_models.inference_engine == InferenceEngine.ONNXRUNTIME else 'tensorrt'
  55. precision = 'fp32' if hyakkiyakou_models.model_precision == ModelPrecision.FP32 else 'int8'
  56. # 这个坑后面在补
  57. if inf_en == 'tensorrt' or precision == 'int8':
  58. raise RequestHumanTakeover('Only support onnxruntime')
  59. args = {
  60. 'conf_threshold': conf,
  61. 'iou_threshold': nms,
  62. 'precision': precision,
  63. 'inference_engine': inf_en,
  64. }
  65. return Tracker(args=args)
  66. @cached_property
  67. def agent(self) -> Agent:
  68. hya_config = self._config.hyakkiyakou_config
  69. sp = hya_config.hya_sp
  70. ssr = hya_config.hya_ssr
  71. sr = hya_config.hya_sr
  72. r = hya_config.hya_r
  73. n = hya_config.hya_n
  74. g = hya_config.hya_g
  75. weights: list = [sp, ssr, sr, r, n, g]
  76. strategy: dict = {
  77. 'weights': weights,
  78. 'priorities': [],
  79. 'invite_friend': hya_config.hya_invite_friend,
  80. 'auto_bean': hya_config.hya_auto_bean
  81. }
  82. return Agent(strategy=strategy)
  83. @cached_property
  84. def debugger(self) -> Debugger:
  85. debug_config = self._config.debug_config
  86. hya_interval = debug_config.hya_interval
  87. if hya_interval <= 100 or hya_interval >= 1000:
  88. raise RequestHumanTakeover('screenshot_interval must be between 1000 and 10000')
  89. self.set_fast_screenshot_interval(hya_interval)
  90. return Debugger(info_enable=debug_config.hya_info, continuous_learning=debug_config.continuous_learning)
  91. def run(self):
  92. hya_count: int = 0
  93. self.limit_count: int = self._config.hyakkiyakou_config.hya_limit_count
  94. limit_time = self._config.hyakkiyakou_config.hya_limit_time
  95. self.limit_time: timedelta = timedelta(hours=limit_time.hour, minutes=limit_time.minute,
  96. seconds=limit_time.second)
  97. self.ui_get_current_page()
  98. self.ui_goto(page_hyakkiyakou)
  99. if self._config.hyakkiyakou_config.hya_invite_friend:
  100. self.invite_friend(True)
  101. while 1:
  102. if hya_count >= self.limit_count:
  103. logger.info('Hyakkiyakou count limit out')
  104. break
  105. if datetime.now() - self.start_time >= self.limit_time:
  106. logger.info('Hyakkiyakou time limit out')
  107. break
  108. self.one()
  109. hya_count += 1
  110. while 1:
  111. self.screenshot()
  112. if not self.appear(self.I_HACCESS):
  113. continue
  114. if self.appear(self.I_HCLOSE_RED):
  115. break
  116. self.ui_click_until_disappear(self.I_HCLOSE_RED)
  117. self.set_next_run(task='Hyakkiyakou', success=True, finish=False)
  118. raise TaskEnd
  119. def one(self):
  120. if not self.appear(self.I_HACCESS):
  121. logger.warning('Page Error')
  122. self.ui_click(self.I_HACCESS, self.I_HSTART, interval=2)
  123. self.wait_until_appear(self.I_HTITLE)
  124. # 随机选一个
  125. click_button = choice([self.C_HSELECT_1, self.C_HSELECT_2, self.C_HSELECT_3])
  126. while 1:
  127. self.screenshot()
  128. if not self.appear(self.I_HTITLE):
  129. break
  130. if self.appear_then_click(self.I_HSTART, interval=2):
  131. continue
  132. if not self.appear(self.I_HSELECTED):
  133. self.click(click_button, interval=2)
  134. self.device.stuck_record_add('BATTLE_STATUS_S')
  135. # 正式开始
  136. logger.hr('Start Hyakkiyakou')
  137. init_bean_flag: bool = False
  138. last_action = [0, 0, False, 10]
  139. if self._config.debug_config.hya_show:
  140. self.debugger.show_start()
  141. while 1:
  142. self.fast_screenshot()
  143. if self.appear(self.I_HEND):
  144. break
  145. if not self.appear(self.I_CHECK_RUN):
  146. continue
  147. if not init_bean_flag:
  148. init_bean_flag = True
  149. self.bean_05to10()
  150. time.sleep(0.5)
  151. if not self.appear(self.I_HFREEZE):
  152. # --------------------------------------------------------
  153. slave_state = self.update_state()
  154. tracks = self.tracker(image=self.device.image, response=last_action)
  155. last_action = self.agent.decision(tracks=tracks, state=slave_state)
  156. self.do_action(last_action, state=slave_state)
  157. else:
  158. # TODO freeze state
  159. tracks = []
  160. # debug
  161. if self._config.debug_config.hya_show:
  162. draw_image = draw_tracks(self.device.image, tracks=tracks)
  163. self.debugger.show_sync(image=draw_image)
  164. if self._config.debug_config.continuous_learning:
  165. self.debugger.deal_learning(image=self.device.image, tracks=tracks)
  166. logger.info('Hyakkiyakou End')
  167. if self._config.debug_config.hya_show:
  168. self.debugger.show_stop()
  169. self.ui_click(self.I_HEND, self.I_HACCESS)
  170. del self.debugger
  171. def do_action(self, action: list, state):
  172. x, y, throw, bean = action
  173. if not throw:
  174. return
  175. if state[0] <= 0:
  176. return
  177. self.fast_click(x=x, y=y)
  178. # def test_opencv():
  179. # import timeit
  180. # from module.config.config import Config
  181. # from module.device.device import Device
  182. #
  183. # c = Config('oas1')
  184. # d = Device(c)
  185. #
  186. # t = ScriptTask(c, d)
  187. # t.fast_screenshot()
  188. # def sh():
  189. # t.device.image = cv2.cvtColor(t.device.image, cv2.COLOR_BGR2RGB)
  190. # execution_time = timeit.timeit(sh, number=100)
  191. # print(f"执行总的时间: {execution_time * 1000} ms")
  192. # cv2.imshow('test', t.device.image)
  193. # cv2.waitKey(0)
  194. if __name__ == '__main__':
  195. from module.config.config import Config
  196. from module.device.device import Device
  197. c = Config('oas1')
  198. d = Device(c)
  199. t = ScriptTask(c, d)
  200. t.run()
  201. # from tasks.Hyakkiyakou.agent.tagent import test_observe, test_state
  202. # test_state()
  203. # from debugger import test_track
  204. # test_track(show=False)
  205. pass