forked from XPUB/si_7-IRIS
motion detector csensibility corrected. some comments added
parent
b99136a813
commit
e0ff1654dd
@ -1,87 +1,76 @@
|
||||
# import the necessary packages
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
# trigger espeak colophon by covering the camera with a finger
|
||||
|
||||
# import dependencies
|
||||
# sudo pip install PiCamera[array]
|
||||
# sudo pip install aplay
|
||||
|
||||
import imutils
|
||||
from imutils.video import VideoStream
|
||||
import argparse
|
||||
import datetime
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
import time, sys
|
||||
from time import sleep
|
||||
import cv2
|
||||
import datetime
|
||||
from datetime import datetime
|
||||
from subprocess import call
|
||||
|
||||
# construct the argument parser and parse the arguments
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("-a", "--min-area", type=int, default=5000, help="minimum area size")
|
||||
args = vars(ap.parse_args())
|
||||
|
||||
# using picamera as default
|
||||
# check open cv version
|
||||
print(cv2.__version__)
|
||||
# choose video source from camera pi
|
||||
vs = VideoStream(usePiCamera=True).start()
|
||||
# let camera warm up
|
||||
sleep(2.0)
|
||||
|
||||
# initialize the first frame in the video stream
|
||||
firstFrame = None
|
||||
occupied = False
|
||||
# loop over the frames of the video
|
||||
while True:
|
||||
# grab the current frame and initialize the occupied/unoccupied
|
||||
# text
|
||||
frame = vs.read()
|
||||
frame = frame if args.get("video", None) is None else frame[1]
|
||||
text = "Unoccupied"
|
||||
# if the frame could not be grabbed, then we have reached the end
|
||||
# of the video
|
||||
if frame is None:
|
||||
break
|
||||
|
||||
# resize the frame, convert it to grayscale, and blur it
|
||||
frame = imutils.resize(frame, width=500)
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
gray = cv2.GaussianBlur(gray, (210, 210), 0)
|
||||
|
||||
# if the first frame is None, initialize it
|
||||
if firstFrame is None:
|
||||
firstFrame = gray
|
||||
continue
|
||||
image = vs.read()
|
||||
count = 0
|
||||
success = True
|
||||
buffer = []
|
||||
|
||||
# compute the absolute difference between the current frame and
|
||||
# first frame
|
||||
frameDelta = cv2.absdiff(firstFrame, gray)
|
||||
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
|
||||
playing = False
|
||||
|
||||
# dilate the thresholded image to fill in holes, then find contours
|
||||
# on thresholded image
|
||||
thresh = cv2.dilate(thresh, None, iterations=2)
|
||||
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
|
||||
cv2.CHAIN_APPROX_SIMPLE)
|
||||
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
|
||||
while success:
|
||||
|
||||
# loop over the contours
|
||||
for c in cnts:
|
||||
# if the contour is too small, ignore it
|
||||
if cv2.contourArea(c) < args["min_area"]:
|
||||
continue
|
||||
# save live frame as JPEG file
|
||||
cv2.imwrite("check_frame.jpg", image)
|
||||
image = vs.read()
|
||||
|
||||
# compute the bounding box for the contour, draw it on the frame,
|
||||
# and update the text
|
||||
(x, y, w, h) = cv2.boundingRect(c)
|
||||
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
||||
text = "Occupied"
|
||||
if not occupied:
|
||||
occupied = True
|
||||
print ('occupied')
|
||||
sys.exit()
|
||||
# open the frame
|
||||
img = Image.open("check_frame.jpg")
|
||||
# resize the frame
|
||||
img2 = img.resize((1, 1))
|
||||
# get the color of pixel
|
||||
color = img2.getpixel((0, 0))
|
||||
# print the color of pixel
|
||||
print('#{:02x}{:02x}{:02x}'.format(*color))
|
||||
# create a sum of the 3 parts that constitute the value
|
||||
sum = color[0] + color[1] + color[2]
|
||||
# this allows the color not to be complete black, but also dark shades, by raising the sum value, we can increase the sensability
|
||||
if sum < 10 :
|
||||
buffer.append(True)
|
||||
else :
|
||||
# the values are set to be datetime.now to prevent the triggering of the colophon while all the values are False
|
||||
buffer.append(datetime.now())
|
||||
|
||||
if occupied and text == "Unoccupied":
|
||||
occupied = False
|
||||
print ("Unoccupied")
|
||||
# create an array with all the values that we got, when the array is bigger than 300 values, start erasing being the first one first to go
|
||||
if len(buffer) > 300:
|
||||
buffer.pop(0)
|
||||
print(buffer)
|
||||
|
||||
# show the frame and record if the user presses a key
|
||||
# cv2.imshow("Security Feed", frame)
|
||||
cv2.imwrite('normal.jpg', frame)
|
||||
#cv2.imshow("Thresh", thresh)
|
||||
cv2.imwrite('thereshold.jpg', thresh)
|
||||
#cv2.imshow("Frame Delta", frameDelta)
|
||||
cv2.imwrite('Gaussianblur.jpg', frameDelta)
|
||||
#key = cv2.waitKey(1) & 0xFF
|
||||
# For calling the colophon.wav some conditions must be met.
|
||||
# By the order of:
|
||||
# all the values must be the same
|
||||
# we need to have at least 200 values, this prevents it from starting when there is just 1 value
|
||||
if ( (len(set(buffer))==1) & ( len(buffer) > 200 ) & ( playing == False ) ):
|
||||
print ("All elements in list are same")
|
||||
call(["aplay", "/home/pi/colophon/colophon.wav"])
|
||||
# to record the espeak sentence into a .wav file ->
|
||||
# -> espeak "sentence goes here" -ven+whisper -s 150 --stdout > colophon.wav
|
||||
# espeak 'Iris Version 0.5 Contributors: Gill Baldwin, Simon Browne, Tancredi Di Giovanni, Paloma García, Rita Graça, Artemis Gryllaki, Pedro Sá Couto, Biyi Wen, Bohye Woo, Silvio Lorusso, Aymeric Mansoux, André Castro, Steve Rushton, Michael Murtaugh, Leslie Robbins. Produced and published by the Experimental Publishing (XPUB) program of the Piet Zwart Institute, Rotterdam, December 2018. A collaboration between the Research Department of Het Nieuwe Instituut and XPUB.
|
||||
playing = True
|
||||
|
||||
# cleanup the camera and close any open windows
|
||||
vs.stop() if args.get("video", None) is None else vs.release()
|
||||
#cv2.destroyAllWindows()
|
||||
else:
|
||||
print ("All elements in list are not same")
|
||||
|
@ -1,9 +1,10 @@
|
||||
#!/bin/sh
|
||||
|
||||
# location of the directory
|
||||
DIR=/var/www/html/lifeHackAgent/
|
||||
while [ 1 ]
|
||||
do
|
||||
python "$DIR"motion_detector_2.py -a 10
|
||||
python "$DIR"motion_detector_2.py -a 6000
|
||||
python "$DIR"guru-pirate.py
|
||||
sleep 10
|
||||
sleep 5
|
||||
done
|
||||
|
Loading…
Reference in New Issue