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.

112 line
3.3 KiB

  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. 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. return
  29. class VehicleDetection(mp.Process):
  30. def __init__(self,name='Vehicle Detection Process',custom_id=1):
  31. super(VehicleDetection,self).__init__()
  32. self.name = f'{name} {custom_id}'
  33. global inputQueue
  34. def run(self):
  35. while (True):
  36. (frame,frameId) = inputQueue.get()
  37. #inputQueue.task_done()
  38. print(f"{self.name}Got frame with ID {frameId} qsize = {inputQueue.qsize()}\n")
  39. #do some processing here.
  40. vehicleDetectionQueue.put((frame,frameId))
  41. if(inputQueue.qsize() < 1):
  42. return
  43. class NumberPlateOcr(mp.Process):
  44. def __init__(self,name='Number plate OCR Process',custom_id=1):
  45. super(NumberPlateOcr,self).__init__()
  46. self.name=f'{name} {custom_id}'
  47. global inputQueue
  48. global numberPlateOcrQueue
  49. def run(self):
  50. while True:
  51. (frame,frameId) = vehicleDetectionQueue.get()
  52. #inputQueue.task_done()
  53. print(f"{self.name} Got frame with ID {frameId}\n")
  54. #do some processing here.
  55. outputQueue.put((frame,frameId))
  56. class outputframe(th.Thread):
  57. def __init__(self,name='output thread',custom_id=1):
  58. super().__init__()
  59. self.name = f'{name} {custom_id}'
  60. def run(self):
  61. while True:
  62. (frame,frameId) = outputQueue.get()
  63. print(f'{self.name} got frame {frameId}\n')
  64. if __name__ == '__main__':
  65. import cProfile
  66. app_profiler = cProfile.Profile()
  67. parser = argparse.ArgumentParser(description='BitSilica Traffic Analysis Solution')
  68. parser.add_argument('--image', help=' Full Path to image file.')
  69. parser.add_argument('--video', help='Full Path to video file.')
  70. parser.add_argument('--realtime',help='Camera Connected Input')
  71. args = parser.parse_args()
  72. #enable profiler here.
  73. app_profiler.enable()
  74. readFramesThread = ReadFrame(args.video)
  75. vehicleDetectionProcess = VehicleDetection()
  76. numberPlateOcrProcess = NumberPlateOcr()
  77. readFramesThread.start()
  78. vehicleDetectionProcess.start()
  79. numberPlateOcrProcess.start()
  80. #disable profiler here.
  81. app_profiler.disable()
  82. profile_name = str('temp.prof'.format(os.path.basename(args.video)[0:-4]))
  83. print("------------------------\nEnd of execution, dumping profile stats\n-------------------------")
  84. app_profiler.dump_stats(profile_name)