script_task.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 tasks.Hyakkiyakou.config import Hyakkiyakou as HyakkiyakouConfig
  12. from tasks.Hyakkiyakou.assets import HyakkiyakouAssets
  13. from tasks.Hyakkiyakou.agent.agent import Agent
  14. from tasks.Hyakkiyakou.slave.hya_slave import HyaSlave
  15. from tasks.Hyakkiyakou.debugger import Debugger
  16. def plot_save(image, boxes):
  17. color_palette = np.random.uniform(0, 255, size=(226, 3))
  18. for box in boxes:
  19. _cls = box[0]
  20. _scores = box[1]
  21. _x, _y, _w, _h = box[2]
  22. x1 = int(_x - _w / 2)
  23. y1 = int(_y - _h / 2)
  24. x2 = int(_x + _w / 2)
  25. y2 = int(_y + _h / 2)
  26. cv2.rectangle(image, (x1, y1), (x2, y2), color_palette[_cls], 2)
  27. #
  28. cv2.putText(image, f'{_cls} {_scores:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color_palette[_cls],
  29. 2)
  30. save_file = './tasks/Hyakkiyakou/temp/image.png'
  31. cv2.imwrite(save_file, image)
  32. def test_():
  33. tracker = Tracker()
  34. # --------------------------------
  35. file_img = './tasks/Hyakkiyakou/temp/20240526T103931/all1716691228563.png'
  36. img = cv2.imdecode(fromfile(str(file_img), dtype=uint8), -1)
  37. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  38. resutl = tracker.detect(img)
  39. start_time = datetime.now()
  40. resutl = tracker.detect(img)
  41. end_time = datetime.now()
  42. print(f'Cost time: {(end_time - start_time).total_seconds() * 1000} ms')
  43. for one in resutl:
  44. print(one)
  45. plot_save(img, resutl)
  46. class ScriptTask:
  47. @cached_property
  48. def tracker(self) -> Tracker:
  49. args = {}
  50. return Tracker(args=args)
  51. @cached_property
  52. def agent(self) -> Agent:
  53. return Agent()
  54. @cached_property
  55. def slave(self) -> HyaSlave:
  56. return HyaSlave()
  57. @cached_property
  58. def debugger(self) -> Debugger:
  59. return Debugger()
  60. def run(self):
  61. pass
  62. if __name__ == '__main__':
  63. # from module.config.config import Config
  64. # from module.device.device import Device
  65. # import cv2
  66. # c = Config('oas1')
  67. # d = Device(c)
  68. # t = ScriptTask(c, d)
  69. # t.run()
  70. test_()