async def _analyze(self, camera: Camera, buffer: FrameBuffer) -> MotionDetectionResult: packet = buffer.get_latest_keyframe() if packet == None: return MotionDetectionResult(False, 0, camera) frames = packet.decode() vfs = [x for x in frames if isinstance(x, av.VideoFrame)] if len(vfs) == 0: return MotionDetectionResult(False, 0, camera) frame = vfs[0] img = cv2.cvtColor(frame.to_rgb().to_ndarray(), cv2.COLOR_BGR2RGB) hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) bounding_boxes, weights = hog.detectMultiScale(img, winStride=(8, 8)) if len(bounding_boxes) == 0: return MotionDetectionResult(False, 0, camera) #boxes = [] #for (x, y, w, h) in bounding_boxes: # boxes.append((x,y,w,h)) #cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) #cv2.imwrite("./recordings/res" + str(self.count) + ".png", img) #self.count += 1 return MotionDetectionResult( True, 50, camera, [[int(x), int(y), int(w), int(h)] for (x, y, w, h) in bounding_boxes])
async def _analyze(self, camera: Camera, buffer: FrameBuffer) -> MotionDetectionResult: packet = buffer.get_latest_keyframe() if packet == None: return MotionDetectionResult(False, 0, camera) frames = packet.decode() vfs = [x for x in frames if isinstance(x, av.VideoFrame)] if len(vfs) == 0: return MotionDetectionResult(False, 0, camera) frame = vfs[0] img = cv2.cvtColor(frame.to_rgb().to_ndarray(), cv2.COLOR_BGR2RGB) img = imutils.resize(img, width=600) # Resize to have a width of 500px. Improves speed without sacrificing accuracy if self.initial_frame is None: self.initial_frame = img return MotionDetectionResult(False, 0, camera) dilated_frame = self.backSub.apply(img) # Find the contours of the dilated version of the thresholded image contours = cv2.findContours(dilated_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = imutils.grab_contours(contours) for contour in contours: boxes = [] # If contour is smaller than the minimum specified area it does not represent # significant motion and should thus be ignored if cv2.contourArea(contour) > 500: return MotionDetectionResult(True, 500, camera, boxes) return MotionDetectionResult(False, 0, camera)