script_task.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. args = {
  28. 'dual_detect': True,
  29. 'conf_threshold': 0.5,
  30. 'iou_threshold': 0.6,
  31. 'precision': 'int8',
  32. }
  33. tracker = Tracker(args=args)
  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. if __name__ == '__main__':
  47. test_()