rolyantrauts

Untitled

Feb 3rd, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | Source Code | 0 0
  1. Detect.py
  2. **********************************************************************************************************************************
  3. import cv2
  4. import argparse
  5. from get_background import get_background
  6. import time
  7. import numpy as np
  8.  
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('-i', '--input', help='path to the input video',
  11. required=True)
  12. parser.add_argument('-c', '--consecutive-frames', default=4, type=int,
  13. dest='consecutive_frames', help='path to the input video')
  14. args = vars(parser.parse_args())
  15. #info = cv2.getBuildInformation()
  16. #print(info)
  17. print(cv2.useOptimized())
  18. cv2.setNumThreads(1)
  19. cap = cv2.VideoCapture('rtsp://admin:Phone439401@192.168.1.4/12', apiPreference=cv2.CAP_FFMPEG)
  20. # get the video frame height and width
  21. frame_width = int(cap.get(3))
  22. frame_height = int(cap.get(4))
  23. print(frame_height, frame_width)
  24. save_name = f"outputs/{args['input'].split('/')[-1]}"
  25. # define codec and create VideoWriter object
  26. #out = cv2.VideoWriter(
  27. # save_name,
  28. # cv2.VideoWriter_fourcc(*'mp4v'), 10,
  29. # (frame_width, frame_height)
  30. #)
  31.  
  32. # get the background model
  33. #background = get_background(args['input'])
  34. # convert the background model to grayscale format
  35.  
  36. frame_count = 0
  37. consecutive_frame = args['consecutive_frames']
  38. oldtime = time.time()
  39. ret, background = cap.read()
  40. background = cv2.cvtColor(background, cv2.COLOR_BGR2GRAY)
  41. color = (255, 255, 255)
  42. mask = cv2.rectangle(background, (600,0), (640,20), color, -1)
  43. cv2.imshow('bkg mask' , mask)
  44. while (cap.isOpened()):
  45. ret, frame = cap.read()
  46. if ret == True:
  47. frame_count += 1
  48. mask = cv2.rectangle(frame, (600,0), (640,20), color, -1)
  49. orig_frame = mask.copy()
  50. # IMPORTANT STEP: convert the frame to grayscale first
  51. gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
  52. if frame_count % consecutive_frame == 0 or frame_count == 1:
  53. frame_diff_list = []
  54. background_list = []
  55. # find the difference between current frame and base frame
  56.  
  57. frame_diff = cv2.absdiff(gray, background)
  58. # thresholding to convert the frame to binary
  59. ret, thres = cv2.threshold(frame_diff, 50, 255, cv2.THRESH_BINARY)
  60. # dilate the frame a bit to get some more white area...
  61. # ... makes the detection of contours a bit easier
  62. #dilate_frame = cv2.dilate(thres, None, iterations=2)
  63. # append the final result into the `frame_diff_list`
  64.  
  65.  
  66.  
  67. frame_diff_list.append(thres)
  68. background_list.append(orig_frame)
  69. # if we have reached `consecutive_frame` number of frames
  70. if len(frame_diff_list) == consecutive_frame:
  71. # add all the frames in the `frame_diff_list`
  72. sum_frames = sum(frame_diff_list)
  73. median_bkg = np.median(background_list, axis=0).astype(np.uint8)
  74. background = cv2.cvtColor(median_bkg, cv2.COLOR_BGR2GRAY)
  75. x1,y1,w,h = cv2.boundingRect(sum_frames)
  76. x2 = x1+w
  77. y2 = y1+h
  78.  
  79. # Draw bounding rectangle
  80. start = (x1, y1)
  81. end = (x2, y2)
  82. colour = (255, 0, 0)
  83. thickness = 2
  84. rectangle_img = cv2.rectangle(sum_frames, start, end, colour, thickness)
  85. #rectangle_img = cv2.rectangle(sum_frames, (0, 0), (320, 320), colour, thickness)
  86.  
  87. # find the contours around the white segmented areas
  88. #contours, hierarchy = cv2.findContours(sum_frames, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  89. # draw the contours, not strictly necessary
  90. #for i, cnt in enumerate(contours):
  91. #cv2.drawContours(frame, contours, i, (0, 0, 255), 3)
  92. #for contour in contours:
  93. # continue through the loop if contour area is less than 500...
  94. # ... helps in removing noise detection
  95. #if cv2.contourArea(contour) < 500:
  96. # continue
  97. # get the xmin, ymin, width, and height coordinates from the contours
  98. #(x, y, w, h) = cv2.boundingRect(contour)
  99. # draw the bounding boxes
  100. #cv2.rectangle(orig_frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
  101.  
  102.  
  103. cv2.imshow('Detected Objects' , sum_frames)
  104. #print(1/(time.time() - oldtime))
  105. oldtime = time.time()
  106. #out.write(orig_frame)
  107. if cv2.waitKey(100) & 0xFF == ord('q'):
  108. break
  109. else:
  110. break
  111. cap.release()
  112. cv2.destroyAllWindows()
  113. *******************************************************************************************************
  114. get_background.py
  115. *******************************************************************************************************
  116. import numpy as np
  117. import cv2
  118.  
  119. def get_background(file_path):
  120. cv2.setNumThreads(1)
  121. cap = cv2.VideoCapture('rtsp://admin:password@192.168.1.4/12', apiPreference=cv2.CAP_FFMPEG)
  122. # we will randomly select 50 frames for the calculating the median
  123. #frame_indices = cap.get(cv2.CAP_PROP_FRAME_COUNT) * np.random.uniform(size=50)
  124. # we will store the frames in array
  125. frames = []
  126. #for idx in frame_indices:
  127. # set the frame id to read that particular frame
  128. #cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
  129. for x in range(50):
  130. ret, frame = cap.read()
  131. frames.append(frame)
  132. # calculate the median
  133. median_frame = np.median(frames, axis=0).astype(np.uint8)
  134. return median_frame
  135. **********************************************************************************************************
Add Comment
Please, Sign In to add comment