動きを検出したら、ビデオき書き出す
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import numpy as np import cv2 ESC_KEY = 27 THRESHOLD = 100000 fgbg = cv2.bgsegm.createBackgroundSubtractorMOG() cap = cv2.VideoCapture(0) w = cap.get(cv2.CAP_PROP_FRAME_WIDTH) h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) fps = cap.get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc(*'MP4V') out = cv2.VideoWriter('out2.mp4',fourcc, 30.0, (int(w),int(h))) while True: ret, frame = cap.read() if frame is None: break fgmask = fgbg.apply(frame) cv2.imshow('frame', frame) cv2.imshow('fgmask', fgmask) motion = sum(sum(fgmask)) if motion > THRESHOLD: print("MOTION DETECTED:",str(motion)) out.write(frame) keyboard = cv2.waitKey(30) if keyboard == ESC_KEY: break cap.release() out.release() cv2.destroyAllWindows() |