Advertisement
anta40

mdetect.2016.py

Feb 3rd, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import cv2
  4. import os
  5. import struct
  6. import unittest
  7. import signal
  8. import datetime
  9. import time
  10. import subprocess
  11. import thread
  12.  
  13. def play_intro():
  14.     while True:
  15.         time.sleep(.5)
  16.         playProcess = subprocess.Popen(['omxplayer', '-o', 'hdmi', '/home/pi/videos/video1.mp4'],
  17.             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  18.             stderr=subprocess.PIPE, close_fds=True)
  19.  
  20.  
  21. def main():
  22.    
  23.     blank_image = cv2.imread('blank.jpg')
  24.     cv2.namedWindow("bw", cv2.WND_PROP_FULLSCREEN)          
  25.     cv2.setWindowProperty("bw", cv2.WND_PROP_FULLSCREEN, cv2.cv.CV_WINDOW_FULLSCREEN)
  26.     cv2.imshow("bw", blank_image)
  27.     cv2.waitKey(0) # wait for keypress infinitely. until any key is pressed, no UI processing occured. WTF
  28.  
  29.     #If the pixel color difference between the image is
  30.     #greather than the threshold then motion is detected
  31.     THRESHOLD = 80000
  32.  
  33.     #Set the number of pixels to compare
  34.     NO_PIXELS = 2000
  35.    
  36.     diff = 0
  37.     first = True
  38.    
  39.     thread.start_new_thread(play_intro(), ())
  40.    
  41.     while True:
  42.         os.system("fswebcam -d /dev/video0 -q -r 320x240 -S20 current.jpg")
  43.         current_image = cv2.imread("current.jpg")
  44.        
  45.         if (first):
  46.             prev_image = current_image
  47.             first = False
  48.             continue
  49.            
  50.         cnt = 0
  51.         diff = 0
  52.         width = current_image.shape[1]
  53.         height = current_image.shape[0]
  54.        
  55.         for x in range(0,width):
  56.             for y in range(0, height):
  57.                 cnt = cnt + 1
  58.                 if cnt == (width*height)/NO_PIXELS:
  59.                     pixel1 = current_image[y][x]
  60.                     pixel2 = prev_image[y][x]
  61.                     diff = diff + abs((int)(pixel1[2]) - (int)(pixel2[2]))
  62.                     diff = diff + abs((int)(pixel1[0]) - (int)(pixel2[0]))
  63.                     diff = diff + abs((int)(pixel1[1]) - (int)(pixel2[1]))
  64.                     cnt = 0
  65.                    
  66.         if diff > THRESHOLD:
  67.             cv.destroyAllWindows()
  68.             print "Motion detected: "+str(diff)+".... resetting"
  69.             timeout_command("omxplayer /home/pi/videos/video2.mp4", 15)
  70.             first = True
  71.             continue
  72.         #print "Difference: "+str(diff)
  73.         prev_image = current_image
  74.  
  75.  
  76. # taken from: http://amix.dk/blog/post/19408
  77. # modified a bit
  78. def timeout_command(command, timeout):
  79.     """call shell-command and either return its output or kill it
  80.     if it doesn't normally exit within timeout seconds and return None"""
  81.     import subprocess, datetime, os, time, signal
  82.  
  83.     cmd = command.split(" ")
  84.     start = datetime.datetime.now()
  85.     process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  86.  
  87.     while process.poll() is None:
  88.         time.sleep(0.1)
  89.         now = datetime.datetime.now()
  90.         if (now - start).seconds > timeout:
  91.             os.kill(process.pid, signal.SIGKILL)
  92.             os.waitpid(-1, os.WNOHANG)
  93.             break
  94.  
  95. if __name__ == "__main__":
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement