Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import cv2
- import os
- import struct
- import unittest
- import signal
- import datetime
- import time
- import subprocess
- import thread
- def play_intro():
- while True:
- time.sleep(.5)
- playProcess = subprocess.Popen(['omxplayer', '-o', 'hdmi', '/home/pi/videos/video1.mp4'],
- stdin=subprocess.PIPE, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE, close_fds=True)
- def main():
- blank_image = cv2.imread('blank.jpg')
- cv2.namedWindow("bw", cv2.WND_PROP_FULLSCREEN)
- cv2.setWindowProperty("bw", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
- cv2.imshow("bw", blank_image)
- cv2.waitKey(0) # wait for keypress infinitely. until any key is pressed, no UI processing occured. WTF
- #If the pixel color difference between the image is
- #greather than the threshold then motion is detected
- THRESHOLD = 80000
- #Set the number of pixels to compare
- NO_PIXELS = 2000
- diff = 0
- first = True
- thread.start_new_thread(play_intro(), ())
- while True:
- os.system("fswebcam -d /dev/video0 -q -r 320x240 -S20 current.jpg")
- current_image = cv2.imread("current.jpg")
- if (first):
- prev_image = current_image
- first = False
- continue
- cnt = 0
- diff = 0
- width = current_image.shape[1]
- height = current_image.shape[0]
- for x in range(0,width):
- for y in range(0, height):
- cnt = cnt + 1
- if cnt == (width*height)/NO_PIXELS:
- pixel1 = current_image[y][x]
- pixel2 = prev_image[y][x]
- diff = diff + abs((int)(pixel1[2]) - (int)(pixel2[2]))
- diff = diff + abs((int)(pixel1[0]) - (int)(pixel2[0]))
- diff = diff + abs((int)(pixel1[1]) - (int)(pixel2[1]))
- cnt = 0
- if diff > THRESHOLD:
- cv.destroyAllWindows()
- print "Motion detected: "+str(diff)+".... resetting"
- timeout_command("omxplayer /home/pi/videos/video2.mp4", 15)
- first = True
- continue
- #print "Difference: "+str(diff)
- prev_image = current_image
- # taken from: http://amix.dk/blog/post/19408
- # modified a bit
- def timeout_command(command, timeout):
- """call shell-command and either return its output or kill it
- if it doesn't normally exit within timeout seconds and return None"""
- import subprocess, datetime, os, time, signal
- cmd = command.split(" ")
- start = datetime.datetime.now()
- process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- while process.poll() is None:
- time.sleep(0.1)
- now = datetime.datetime.now()
- if (now - start).seconds > timeout:
- os.kill(process.pid, signal.SIGKILL)
- os.waitpid(-1, os.WNOHANG)
- break
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement