benchmark.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 = ObjectDetector()
  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. ######
  34. # Separate process runner
  35. ######
  36. def start(id, num_detections, detection_queue):
  37. object_detector = RemoteObjectDetector(str(id), '/labelmap.txt', detection_queue)
  38. start = datetime.datetime.now().timestamp()
  39. frame_times = []
  40. for x in range(0, num_detections):
  41. start_frame = datetime.datetime.now().timestamp()
  42. detections = object_detector.detect(my_frame)
  43. frame_times.append(datetime.datetime.now().timestamp()-start_frame)
  44. duration = datetime.datetime.now().timestamp()-start
  45. print(f"{id} - Processed for {duration:.2f} seconds.")
  46. print(f"{id} - Average frame processing time: {mean(frame_times)*1000:.2f}ms")
  47. edgetpu_process = EdgeTPUProcess()
  48. # start(1, 1000, edgetpu_process.detect_lock, edgetpu_process.detect_ready, edgetpu_process.frame_ready)
  49. ####
  50. # Multiple camera processes
  51. ####
  52. camera_processes = []
  53. for x in range(0, 10):
  54. camera_process = mp.Process(target=start, args=(x, 100, edgetpu_process.detection_queue))
  55. camera_process.daemon = True
  56. camera_processes.append(camera_process)
  57. start = datetime.datetime.now().timestamp()
  58. for p in camera_processes:
  59. p.start()
  60. for p in camera_processes:
  61. p.join()
  62. duration = datetime.datetime.now().timestamp()-start
  63. print(f"Total - Processed for {duration:.2f} seconds.")