benchmark.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. ######
  34. # Separate process runner
  35. ######
  36. def start(id, num_detections, detection_queue, event):
  37. object_detector = RemoteObjectDetector(str(id), '/labelmap.txt', detection_queue, event)
  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. event = mp.Event()
  48. edgetpu_process = EdgeTPUProcess({'1': event})
  49. start(1, 1000, edgetpu_process.detection_queue, event)
  50. print(f"Average raw inference speed: {edgetpu_process.avg_inference_speed.value*1000:.2f}ms")
  51. ####
  52. # Multiple camera processes
  53. ####
  54. # camera_processes = []
  55. # pipes = {}
  56. # for x in range(0, 10):
  57. # pipes[x] = mp.Pipe(duplex=False)
  58. # edgetpu_process = EdgeTPUProcess({str(key): value[1] for (key, value) in pipes.items()})
  59. # for x in range(0, 10):
  60. # camera_process = mp.Process(target=start, args=(x, 100, edgetpu_process.detection_queue, pipes[x][0]))
  61. # camera_process.daemon = True
  62. # camera_processes.append(camera_process)
  63. # start = datetime.datetime.now().timestamp()
  64. # for p in camera_processes:
  65. # p.start()
  66. # for p in camera_processes:
  67. # p.join()
  68. # duration = datetime.datetime.now().timestamp()-start
  69. # print(f"Total - Processed for {duration:.2f} seconds.")