script_task.py 9.5 KB

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