script_task.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
  9. from oashya.tracker import Tracker
  10. def plot_save(image, boxes):
  11. color_palette = np.random.uniform(0, 255, size=(226, 3))
  12. for box in boxes:
  13. _cls = box[0]
  14. _scores = box[1]
  15. _x, _y, _w, _h = box[2]
  16. x1 = int(_x - _w / 2)
  17. y1 = int(_y - _h / 2)
  18. x2 = int(_x + _w / 2)
  19. y2 = int(_y + _h / 2)
  20. cv2.rectangle(image, (x1, y1), (x2, y2), color_palette[_cls], 2)
  21. #
  22. cv2.putText(image, f'{_cls} {_scores:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color_palette[_cls],
  23. 2)
  24. save_file = './tasks/Hyakkiyakou/temp/image.png'
  25. cv2.imwrite(save_file, image)
  26. def test_():
  27. tracker = Tracker()
  28. # --------------------------------
  29. file_img = './tasks/Hyakkiyakou/temp/20240526T103931/all1716691228563.png'
  30. img = cv2.imdecode(fromfile(str(file_img), dtype=uint8), -1)
  31. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  32. resutl = tracker.detect(img)
  33. start_time = datetime.now()
  34. resutl = tracker.detect(img)
  35. end_time = datetime.now()
  36. print(f'Cost time: {(end_time - start_time).total_seconds() * 1000} ms')
  37. for one in resutl:
  38. print(one)
  39. plot_save(img, resutl)
  40. if __name__ == '__main__':
  41. test_()