Created
April 2, 2018 20:39
-
-
Save TareObjects/bade6c6b5ef42b8bf15fd2fe108347b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import io | |
import picamera | |
import sys | |
import time | |
import datetime | |
import numpy as np | |
import cv2 | |
# | |
# 初期化、パラメータ読み込みなど | |
# | |
seconds = 1 | |
showImage = False | |
fileImage = False | |
args = sys.argv | |
for i in args: | |
if i == "image": | |
showImage = True | |
if i == "file": | |
fileImage = True | |
if i.isdigit(): | |
seconds = int(i) | |
print(showImage, seconds) | |
# | |
# カメラの準備 | |
#写真 | |
#cap = cv2.VideoCapture(0) | |
CAMERA_WIDTH = 320 | |
CAMERA_HEIGHT = 240 | |
camera = picamera.PiCamera() | |
camera.resolution = (CAMERA_WIDTH, CAMERA_HEIGHT) | |
# カメラ動作テスト : camera.capture('my_picture.jpg') | |
# | |
# 動体検出器の生成 | |
# | |
fgbg = cv2.BackgroundSubtractorMOG() | |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) | |
count = 0 | |
while(1): | |
#動画を1フレーム読み込む | |
#ret, frame = cap.read() | |
#original | |
#camera.capture(stream, format='jpeg') | |
#data = np.fromstring(stream.getvalue(), dtype=np.uint8) | |
#frame = cv2.imdecode(data, 1) | |
stream = io.BytesIO() | |
camera.capture(stream, format='jpeg') | |
data = np.fromstring(stream.getvalue(), dtype=np.uint8) | |
frame = cv2.imdecode(data, 1) | |
#「Escが押される」または「動画フレームがない場合」終了 | |
k = cv2.waitKey(30) & 0xff | |
if k == 65: | |
break | |
#動体、背景、影に分ける | |
fgmask = fgbg.apply(frame) | |
#_, fgmask = cv2.threshold(fgmask,127,1,cv2.THRESH_BINARY) | |
#fgmask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel) | |
#bgr = cv2.split(frame) | |
#new_bgr = list(map(lambda x:x * fgmask, bgr)) | |
#result = cv2.merge(new_bgr) | |
now = datetime.datetime.now() | |
wCount = np.count_nonzero(fgmask) | |
aCount = fgmask.size | |
filename = now.strftime("%Y-%m-%dT%H:%M:%S") | |
print filename, wCount, aCount, float(wCount) / float(aCount) | |
if showImage: | |
cv2.imshow('result', fgmask) | |
cv2.imshow('frame', frame) | |
if fileImage: | |
cv2.imwrite('sabun/'+filename+'org.jpg', frame) | |
cv2.imwrite('sabun/'+filename+'dif.jpg', fgmask) | |
time.sleep(seconds) | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment