動体検知をOpenCVで実装するしかないな(
# -*- coding: utf-8 -*- import cv2 import numpy as np import time import commands DURATION = 1.0 #取得する画像の解像度 WIDTH = 320 HEIGHT = 240 camera = cv2.VideoCapture(0) camera.set(3, WIDTH) camera.set(4, HEIGHT) camera.set(5, 15)# FPS retval, frame = camera.read() hist_32 = np.zeros((HEIGHT, WIDTH), np.float32) frame_pre = frame while(camera.isOpened()): retval, frame = camera.read() # 直前フレームとの差分を抽出 diff = cv2.absdiff(frame, frame_pre) # グレースケール変換 diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) # 差分画像2値化 第二引数が抽出する動きの領域のしきい値 retval, diff = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY) times_s = time.clock() # 履歴画像の作成 cv2.updateMotionHistory(diff, hist_32, times_s, DURATION) # 履歴画像から動きの方向を計算 hist_8, direction = cv2.calcMotionGradient(hist_32, 0.25, 0.05, apertureSize=5) # 履歴画像と方向から全体の動き方向を計算 angle = cv2.calcGlobalOrientation(direction, hist_8, hist_32, times_s, DURATION) if 170 < angle < 190: print "<-----" cv2.imwrite("tmp.jpg",frame) print commands.getoutput("img2sixel tmp.jpg") if angle > 340: print "----->" cv2.imwrite("tmp.jpg",frame) print commands.getoutput("img2sixel tmp.jpg")