benchmark.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os
  2. from statistics import mean
  3. import multiprocessing as mp
  4. import numpy as np
  5. import datetime
  6. from frigate.edgetpu import LocalObjectDetector, EdgeTPUProcess, RemoteObjectDetector, load_labels
  7. my_frame = np.expand_dims(np.full((300,300,3), 1, np.uint8), axis=0)
  8. labels = load_labels('/labelmap.txt')
  9. ######
  10. # Minimal same process runner
  11. ######
  12. # object_detector = LocalObjectDetector()
  13. # tensor_input = np.expand_dims(np.full((300,300,3), 0, np.uint8), axis=0)
  14. # start = datetime.datetime.now().timestamp()
  15. # frame_times = []
  16. # for x in range(0, 1000):
  17. # start_frame = datetime.datetime.now().timestamp()
  18. # tensor_input[:] = my_frame
  19. # detections = object_detector.detect_raw(tensor_input)
  20. # parsed_detections = []
  21. # for d in detections:
  22. # if d[1] < 0.4:
  23. # break
  24. # parsed_detections.append((
  25. # labels[int(d[0])],
  26. # float(d[1]),
  27. # (d[2], d[3], d[4], d[5])
  28. # ))
  29. # frame_times.append(datetime.datetime.now().timestamp()-start_frame)
  30. # duration = datetime.datetime.now().timestamp()-start
  31. # print(f"Processed for {duration:.2f} seconds.")
  32. # print(f"Average frame processing time: {mean(frame_times)*1000:.2f}ms")
  33. def start(id, num_detections, detection_queue, event):
  34. object_detector = RemoteObjectDetector(str(id), '/labelmap.txt', detection_queue, event)
  35. start = datetime.datetime.now().timestamp()
  36. frame_times = []
  37. for x in range(0, num_detections):
  38. start_frame = datetime.datetime.now().timestamp()
  39. detections = object_detector.detect(my_frame)
  40. frame_times.append(datetime.datetime.now().timestamp()-start_frame)
  41. duration = datetime.datetime.now().timestamp()-start
  42. object_detector.cleanup()
  43. print(f"{id} - Processed for {duration:.2f} seconds.")
  44. print(f"{id} - FPS: {object_detector.fps.eps():.2f}")
  45. print(f"{id} - Average frame processing time: {mean(frame_times)*1000:.2f}ms")
  46. ######
  47. # Separate process runner
  48. ######
  49. # event = mp.Event()
  50. # detection_queue = mp.Queue()
  51. # edgetpu_process = EdgeTPUProcess(detection_queue, {'1': event}, 'usb:0')
  52. # start(1, 1000, edgetpu_process.detection_queue, event)
  53. # print(f"Average raw inference speed: {edgetpu_process.avg_inference_speed.value*1000:.2f}ms")
  54. ####
  55. # Multiple camera processes
  56. ####
  57. camera_processes = []
  58. events = {}
  59. for x in range(0, 10):
  60. events[str(x)] = mp.Event()
  61. detection_queue = mp.Queue()
  62. edgetpu_process_1 = EdgeTPUProcess(detection_queue, events, 'usb:0')
  63. edgetpu_process_2 = EdgeTPUProcess(detection_queue, events, 'usb:1')
  64. for x in range(0, 10):
  65. camera_process = mp.Process(target=start, args=(x, 300, detection_queue, events[str(x)]))
  66. camera_process.daemon = True
  67. camera_processes.append(camera_process)
  68. start_time = datetime.datetime.now().timestamp()
  69. for p in camera_processes:
  70. p.start()
  71. for p in camera_processes:
  72. p.join()
  73. duration = datetime.datetime.now().timestamp()-start_time
  74. print(f"Total - Processed for {duration:.2f} seconds.")