Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

skeleton.py 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import cv2
  3. import argparse
  4. import time
  5. import random
  6. import multiprocessing as mp
  7. import threading as th
  8. from queue import Queue
  9. inputQueue = mp.Queue()
  10. vehicleDetectionQueue = mp.Queue()
  11. numberPlateOcrQueue = mp.Queue()
  12. displayQueue = mp.Queue()
  13. outputQueue = mp.Queue()
  14. class ReadFrame(th.Thread):
  15. def __init__(self,source) -> None:
  16. super().__init__()
  17. self.frameId = 1
  18. self.stopped = False
  19. self.grabbed = True
  20. self.videoCaptureObject = cv2.VideoCapture(source)
  21. print(f'Reading from source = {source}')
  22. global inputQueue
  23. def run(self):
  24. while self.grabbed:
  25. (self.grabbed, self.frame) = self.videoCaptureObject.read()
  26. inputQueue.put((self.frame,self.frameId))
  27. print(f"IP frame added with id {self.frameId}\n")
  28. self.frameId+=1
  29. class VehicleDetection(mp.Process):
  30. global inputQueue
  31. def run(self):
  32. while True:
  33. (frame,frameId) = inputQueue.get()
  34. #inputQueue.task_done()
  35. print(f"\n VD Got frame with ID {frameId}")
  36. #do some processing here.
  37. vehicleDetectionQueue.put((frame,frameId))
  38. class NumberPlateOcr(mp.Process):
  39. global inputQueue
  40. global numberPlateOcrQueue
  41. def run(self):
  42. while True:
  43. (frame,frameId) = vehicleDetectionQueue.get()
  44. #inputQueue.task_done()
  45. print(f"\n NP Got frame with ID {frameId}")
  46. #do some processing here.
  47. numberPlateOcrQueue.put((frame,frameId))
  48. class DisplayFrame(mp.Process):
  49. global numberPlateOcrQueue
  50. global displayQueue
  51. def run(self):
  52. while True:
  53. (frame,frameId) = numberPlateOcrQueue.get()
  54. print(f'DF got frame with ID {frameId}')
  55. #display image here with frame ID
  56. outputQueue.put((frame,frameId))
  57. class SaveOutput(mp.Process):
  58. global displayQueue
  59. #Takes all the (frame,frameId) and then sorts them and merges them in a video using some tool.
  60. if __name__ == '__main__':
  61. import cProfile
  62. app_profiler = cProfile.Profile()
  63. parser = argparse.ArgumentParser(description='BitSilica Traffic Analysis Solution')
  64. parser.add_argument('--image', help=' Full Path to image file.')
  65. parser.add_argument('--video', help='Full Path to video file.')
  66. parser.add_argument('--realtime',help='Camera Connected Input')
  67. args = parser.parse_args()
  68. #enable profiler here.
  69. app_profiler.enable()
  70. readFramesThread = ReadFrame(args.video)
  71. vehicleDetectionProcess = VehicleDetection()
  72. numberPlateOcrProcess = NumberPlateOcr()
  73. readFramesThread.start()
  74. vehicleDetectionProcess.start()
  75. numberPlateOcrProcess.start()
  76. #disable profiler here.
  77. app_profiler.disable()
  78. profile_name = str('temp.prof'.format(os.path.basename(args.video)[0:-4]))
  79. print("------------------------\nEnd of execution, dumping profile stats\n-------------------------")
  80. app_profiler.dump_stats(profile_name)