script_task.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import cv2
  5. import numpy as np
  6. from datetime import datetime
  7. from numpy import uint8, fromfile
  8. from cached_property import cached_property
  9. # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
  10. from oashya.tracker import Tracker
  11. from module.exception import TaskEnd
  12. from tasks.GameUi.game_ui import GameUi
  13. from tasks.GameUi.page import page_hyakkiyakou
  14. from tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
  15. from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
  16. from tasks.Hyakkiyakou.agent.agent import Agent
  17. from tasks.Hyakkiyakou.slave.hya_slave import HyaSlave
  18. from tasks.Hyakkiyakou.debugger import Debugger
  19. def plot_save(image, boxes):
  20. color_palette = np.random.uniform(0, 255, size=(226, 3))
  21. for box in boxes:
  22. _cls = box[0]
  23. _scores = box[1]
  24. _x, _y, _w, _h = box[2]
  25. x1 = int(_x - _w / 2)
  26. y1 = int(_y - _h / 2)
  27. x2 = int(_x + _w / 2)
  28. y2 = int(_y + _h / 2)
  29. cv2.rectangle(image, (x1, y1), (x2, y2), color_palette[_cls], 2)
  30. #
  31. cv2.putText(image, f'{_cls} {_scores:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color_palette[_cls],
  32. 2)
  33. save_file = './tasks/Hyakkiyakou/temp/image.png'
  34. cv2.imwrite(save_file, image)
  35. def test_():
  36. tracker = Tracker()
  37. # --------------------------------
  38. file_img = './tasks/Hyakkiyakou/temp/20240526T103931/all1716691228563.png'
  39. img = cv2.imdecode(fromfile(str(file_img), dtype=uint8), -1)
  40. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  41. resutl = tracker.detect(img)
  42. start_time = datetime.now()
  43. resutl = tracker.detect(img)
  44. end_time = datetime.now()
  45. print(f'Cost time: {(end_time - start_time).total_seconds() * 1000} ms')
  46. for one in resutl:
  47. print(one)
  48. plot_save(img, resutl)
  49. class ScriptTask(GameUi, HyaSlave):
  50. @cached_property
  51. def tracker(self) -> Tracker:
  52. args = {}
  53. return Tracker(args=args)
  54. @cached_property
  55. def agent(self) -> Agent:
  56. return Agent()
  57. @cached_property
  58. def debugger(self) -> Debugger:
  59. return Debugger()
  60. def run(self):
  61. self.ui_get_current_page()
  62. self.ui_goto(page_hyakkiyakou)
  63. self.one()
  64. self.ui_click_until_disappear(self.I_UI_BACK_RED)
  65. self.set_next_run(task='Hyakkiyakou', success=True, finish=False)
  66. raise TaskEnd
  67. def one(self):
  68. pass
  69. if __name__ == '__main__':
  70. # from module.config.config import Config
  71. # from module.device.device import Device
  72. from debugger import test_track
  73. # c = Config('oas1')
  74. # d = Device(c)
  75. # t = ScriptTask(c, d)
  76. # t.run()
  77. test_track(show=True)