動体の検知

2019/07/04

Python2.7.6, OpenCV3.0

# -*- coding: utf-8 -*-
import cv2
import numpy as np

cap = cv2.VideoCapture(r'C:\Temp\input.avi')
size = (170,300)
avg = None
fourcc = "DIB "
fps = 24
output = cv2.VideoWriter(r'C:\Temp\output.avi',
                         cv2.VideoWriter_fourcc(*fourcc), fps, size)

while True:

    ret, frame = cap.read()
    if not ret:
        break

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    if avg is None:
        avg = gray.copy().astype("float")
        continue

    cv2.accumulateWeighted(gray, avg, 0.5)
    frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg))

    thresh = cv2.threshold(frameDelta, 3, 255, cv2.THRESH_BINARY)[1]

    cv2.imshow('frame', thresh)
    output.write(thresh)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
output.release()
cv2.destroyAllWindows()


OpenCVを利用して動画(カメラ)から動体検知をする方法について
https://developers.cyberagent.co.jp/blog/archives/12666/

Python+OpenCVとWebカメラを使って動体検知する話
https://ensekitt.hatenablog.com/entry/2018/06/11/200000