def diffs(self, other): diff = cv2.absdiff(self.processed, other.processed) _, diff = cv2.threshold(diff, int(config.get("MIN_CHANGE_THRESHOLD")), 255, cv2.THRESH_BINARY) # dilate to join broken contours diff = cv2.dilate(diff, None, iterations=2) (_, contours, _) = cv2.findContours(diff, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) return [cv2.boundingRect(c) for c in contours if cv2.contourArea(c) >= int(config.get("MIN_CHANGE_AREA"))]
def process(image): # Discard colour information; irrelevant to motion detection processed = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Remove noise blur_kernel = (int(config.get("BLUR_KERNEL_WIDTH")), int(config.get("BLUR_KERNEL_HEIGHT"))) processed = cv2.GaussianBlur(processed, blur_kernel, 0) return processed
def finalise(self, name): print 'finalising recording' input_paths = sorted(glob(path.join(self.output_dir, '*'))) output_path = path.join(tempfile.gettempdir(), name + '.gif') args = ['convert', '-delay', str(int(100.0 / int(config.get('OUTPUT_FPS')))), '-loop', '0'] args += input_paths args += [output_path] exit_status = subprocess.call(args) if exit_status == 0: print 'wrote %s' % output_path shutil.rmtree(self.output_dir) return output_path else: print 'error writing %s' % output_path
def exceeds_max_duration(self, frame): return self.first_write_time and \ frame.time - self.first_write_time > int(config.get('MAX_RECORDING_SECONDS'))
def exceeds_max_fps(self, frame): return self.last_write_time and \ frame.time - self.last_write_time < 1.0 / int(config.get('OUTPUT_FPS'))
def downscale(image): orig_h, orig_w = image.shape[:2] width = int(config.get("FRAME_WIDTH")) ratio = width / float(orig_w) height = int(orig_h * ratio) return cv2.resize(image, (width, height), interpolation=cv2.INTER_AREA)