You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.8 KiB

  1. import argparse
  2. import multiprocessing as mp
  3. import os
  4. import random
  5. import threading as th
  6. import time
  7. from queue import Queue
  8. import cv2
  9. inputQueue = mp.Queue()
  10. vehicleDetectionQueue = mp.Queue()
  11. outputQueue = mp.Queue()
  12. class ReadFrame(th.Thread):
  13. def __init__(self,source,name='Input thread',custom_id=1) -> None:
  14. super().__init__()
  15. self.frameId = 1
  16. self.stopped = False
  17. self.grabbed = True
  18. self.name = f'{name} {custom_id}'
  19. self.videoCaptureObject = cv2.VideoCapture(source)
  20. print(f'Reading from source = {source}')
  21. global inputQueue
  22. def run(self):
  23. while (self.grabbed):
  24. (self.grabbed, self.frame) = self.videoCaptureObject.read()
  25. inputQueue.put((self.frame,self.frameId))
  26. print(f"{self.name}frame added with id {self.frameId}\n")
  27. self.frameId+=1
  28. print('--Done reading frames--\n')
  29. return
  30. class VehicleDetection(mp.Process):
  31. def __init__(self,name='Vehicle Detection Process',custom_id=1):
  32. super(VehicleDetection,self).__init__()
  33. self.name = f'{name} {custom_id}'
  34. global inputQueue
  35. def run(self):
  36. while (True):
  37. if(inputQueue.qsize() == 0):
  38. return
  39. (frame,frameId) = inputQueue.get()
  40. #inputQueue.task_done()
  41. print(f"{self.name}Got frame with ID {frameId} qsize = {inputQueue.qsize()}\n")
  42. #do some processing here.
  43. time.sleep(.5)
  44. vehicleDetectionQueue.put_nowait((frame,frameId))
  45. class NumberPlateOcr(mp.Process):
  46. def __init__(self,name='Number plate OCR Process',custom_id=1):
  47. super(NumberPlateOcr,self).__init__()
  48. self.name=f'{name} {custom_id}'
  49. global inputQueue
  50. global vehicleDetectionQueue
  51. global outputQueue
  52. def run(self):
  53. while True:
  54. (frame,frameId) = vehicleDetectionQueue.get()
  55. #inputQueue.task_done()
  56. print(f"{self.name} Got frame with ID {frameId}\n")
  57. #do some processing here.
  58. time.sleep(.25)
  59. outputQueue.put_nowait((frame,frameId))
  60. if((inputQueue.empty()) and (vehicleDetectionQueue.empty())):
  61. return
  62. class outputframe(th.Thread):
  63. def __init__(self,name='output thread',custom_id=1):
  64. super().__init__()
  65. self.name = f'{name} {custom_id}'
  66. def run(self):
  67. while True:
  68. (frame,frameId) = outputQueue.get()
  69. print(f'{self.name} got frame {frameId}\n')
  70. if __name__ == '__main__':
  71. import cProfile
  72. app_profiler = cProfile.Profile()
  73. parser = argparse.ArgumentParser(description='BitSilica Traffic Analysis Solution')
  74. parser.add_argument('--image', help=' Full Path to image file.')
  75. parser.add_argument('--video', help='Full Path to video file.')
  76. parser.add_argument('--realtime',help='Camera Connected Input')
  77. args = parser.parse_args()
  78. #enable profiler here.
  79. app_profiler.enable()
  80. readFramesThread = ReadFrame(args.video)
  81. vehicleDetectionProcess = VehicleDetection()
  82. numberPlateOcrProcess = NumberPlateOcr()
  83. readFramesThread.start()
  84. time.sleep(.25)
  85. vehicleDetectionProcess.start()
  86. numberPlateOcrProcess.start()
  87. readFramesThread.join()
  88. print(f'readframesthread {readFramesThread.is_alive()}\n')
  89. vehicleDetectionProcess.join()
  90. print(f'vehicleDetectionProcess {vehicleDetectionProcess.is_alive()}\n')
  91. numberPlateOcrProcess.join()
  92. print(f'numberPlateOcrProcess {numberPlateOcrProcess.is_alive()}\n')
  93. #disable profiler here.
  94. app_profiler.disable()
  95. profile_name = str('temp.prof'.format(os.path.basename(args.video)[0:-4]))
  96. print("------------------------\nEnd of execution, dumping profile stats\n-------------------------")
  97. app_profiler.dump_stats(profile_name)