motion.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import cv2
  2. import imutils
  3. import numpy as np
  4. from frigate.config import MotionConfig
  5. class MotionDetector():
  6. def __init__(self, frame_shape, mask, config: MotionConfig):
  7. self.config = config
  8. self.frame_shape = frame_shape
  9. self.resize_factor = frame_shape[0]/config.frame_height
  10. self.motion_frame_size = (config.frame_height, config.frame_height*frame_shape[1]//frame_shape[0])
  11. self.avg_frame = np.zeros(self.motion_frame_size, np.float)
  12. self.avg_delta = np.zeros(self.motion_frame_size, np.float)
  13. self.motion_frame_count = 0
  14. self.frame_counter = 0
  15. resized_mask = cv2.resize(mask, dsize=(self.motion_frame_size[1], self.motion_frame_size[0]), interpolation=cv2.INTER_LINEAR)
  16. self.mask = np.where(resized_mask==[0])
  17. def detect(self, frame):
  18. motion_boxes = []
  19. gray = frame[0:self.frame_shape[0], 0:self.frame_shape[1]]
  20. # resize frame
  21. resized_frame = cv2.resize(gray, dsize=(self.motion_frame_size[1], self.motion_frame_size[0]), interpolation=cv2.INTER_LINEAR)
  22. # TODO: can I improve the contrast of the grayscale image here?
  23. # convert to grayscale
  24. # resized_frame = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2GRAY)
  25. # mask frame
  26. resized_frame[self.mask] = [255]
  27. # it takes ~30 frames to establish a baseline
  28. # dont bother looking for motion
  29. if self.frame_counter < 30:
  30. self.frame_counter += 1
  31. else:
  32. # compare to average
  33. frameDelta = cv2.absdiff(resized_frame, cv2.convertScaleAbs(self.avg_frame))
  34. # compute the average delta over the past few frames
  35. # higher values mean the current frame impacts the delta a lot, and a single raindrop may
  36. # register as motion, too low and a fast moving person wont be detected as motion
  37. cv2.accumulateWeighted(frameDelta, self.avg_delta, self.config.delta_alpha)
  38. # compute the threshold image for the current frame
  39. # TODO: threshold
  40. current_thresh = cv2.threshold(frameDelta, self.config.threshold, 255, cv2.THRESH_BINARY)[1]
  41. # black out everything in the avg_delta where there isnt motion in the current frame
  42. avg_delta_image = cv2.convertScaleAbs(self.avg_delta)
  43. avg_delta_image = cv2.bitwise_and(avg_delta_image, current_thresh)
  44. # then look for deltas above the threshold, but only in areas where there is a delta
  45. # in the current frame. this prevents deltas from previous frames from being included
  46. thresh = cv2.threshold(avg_delta_image, self.config.threshold, 255, cv2.THRESH_BINARY)[1]
  47. # dilate the thresholded image to fill in holes, then find contours
  48. # on thresholded image
  49. thresh = cv2.dilate(thresh, None, iterations=2)
  50. cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  51. cnts = imutils.grab_contours(cnts)
  52. # loop over the contours
  53. for c in cnts:
  54. # if the contour is big enough, count it as motion
  55. contour_area = cv2.contourArea(c)
  56. if contour_area > self.config.contour_area:
  57. x, y, w, h = cv2.boundingRect(c)
  58. motion_boxes.append((int(x*self.resize_factor), int(y*self.resize_factor), int((x+w)*self.resize_factor), int((y+h)*self.resize_factor)))
  59. if len(motion_boxes) > 0:
  60. self.motion_frame_count += 1
  61. if self.motion_frame_count >= 10:
  62. # only average in the current frame if the difference persists for a bit
  63. cv2.accumulateWeighted(resized_frame, self.avg_frame, self.config.frame_alpha)
  64. else:
  65. # when no motion, just keep averaging the frames together
  66. cv2.accumulateWeighted(resized_frame, self.avg_frame, self.config.frame_alpha)
  67. self.motion_frame_count = 0
  68. return motion_boxes