Advertisement
Guest User

ScareRandom

a guest
Oct 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. #!/usr/bin/python
  2. import subprocess as sp
  3. import time
  4. import os
  5. import datetime
  6. from pirDetect import *
  7. import sys
  8. import random
  9. """
  10. This script will play any of the videos listed in a random order.
  11. Usage: python ./scareRandom.py [VideoName] [Minutes]
  12.  
  13. [VideoName] is any of video prefixes in the video_prefix list.
  14. [Minutes] is the time value in minutes of how often you want to rotate to a different video.
  15.  
  16. Example usage would be : python ./scareRandom.py Male 5.
  17.  
  18. After each trigger of the on_motion event the script will check and determine if the time elapsed is greater than the
  19. value you provided in argument 2 and if the elapsed time is longer than your time setting it will randomly pick a new
  20. video prefix and will recursively attempt to choose one that is NOT the current video prefix so it doesn't play the same
  21. video more than one time in sequence.
  22.  
  23. To add more or different videos just add to or modify the video_prefix list below.
  24. If adding more videos or changing the defaults you will have to create a start image for each additional video.
  25. The naming structure for the start images and videos are as follows.
  26.  
  27. [Prefix]ScareV.m4v (MaleScareV.m4v) or [Prefix]ScareV.mp4 (MaleScareV.mp4)
  28. [Prefix]Start.png (MaleStart.png)
  29. """
  30.  
  31.  
  32. #  initialize variables
  33.  
  34. video_prefix = ["Male", "Female", "Child"] # This is the list of videos prefixes, you can add additional video
  35. # prefixes here.
  36. video = ["omxplayer", "filename", "-o", "both", "--win", "0 0 1280 720", "--aspect-mode", "fill", "--no-osd",
  37.          "--orientation", "180", "--vol", "-600"]
  38. record = ["raspivid", "-o", "filename", "-n", "-t", "5000", "-rot", "180"]
  39. scare_file = ""
  40. current_prefix = ""
  41. new_prefix = ""
  42. image_name = ""
  43. start_time = time.time()
  44.  
  45.  
  46. def change_video():
  47.     global start_time
  48.     global scare_file
  49.     global current_prefix
  50.     global new_prefix
  51.     elapsed_time = time.time() - start_time
  52.     print(str("\nTime since last rotation: {0}".format(datetime.timedelta(seconds=elapsed_time))))
  53.     if elapsed_time > (int(sys.argv[2]) * 60):
  54.         while new_prefix == current_prefix:  # make sure we don't choose the same video
  55.             new_prefix = video_prefix[random.randrange(len(video_prefix))]
  56.         current_prefix = new_prefix
  57.         scare_file = "/home/pi/Projects/Halloween/ScareMedia/{0}ScareV.m4v".format(current_prefix)
  58.         start_time = time.time()
  59.         show_image(current_prefix)
  60.         print("\nUpdating Video to: {0}\n".format(current_prefix))
  61.  
  62.  
  63. def getfilename():
  64.     return "/home/pi/Projects/Halloween/Recordings/" + datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
  65.  
  66.  
  67. def sub_proc_wait(params):
  68.     sub = sp.Popen(params)
  69.     while sub.poll() is None:
  70.         time.sleep(.1)
  71.  
  72.  
  73. def on_motion(curr_state):
  74.     if curr_state:
  75.         auto_file_name = getfilename()  # Get a time stamped file name
  76.         record[2] = auto_file_name
  77.         sub_record = sp.Popen(record)  # Start recording to capture their fright
  78.         video[1] = scare_file
  79.         sub_proc_wait(video)  # Play the video to scare them
  80.         video[1] = auto_file_name
  81.         sub_proc_wait(video)  # Play back the video we just recorded
  82.         change_video()
  83.  
  84.  
  85. def show_image(_image_name):
  86.     os.system("sudo fbi -T 1 -d /dev/fb0 -noverbose -once /home/pi/Projects/Halloween/ScareMedia/{0}Start.png".format(
  87.         _image_name))
  88.  
  89.  
  90. def start_up():
  91.     global scare_file
  92.     global image_name
  93.     image_name = arg1
  94.     scare_file = "/home/pi/Projects/Halloween/ScareMedia/{0}ScareV.m4v".format(image_name)
  95.     show_image(image_name)
  96.     obj_detect = detector(7)
  97.     obj_detect.subscribe(on_motion)
  98.     obj_detect.start()
  99.     os.system("sudo killall -9 fbi")
  100.  
  101.  
  102. if __name__ == "__main__":
  103.  
  104.     try:
  105.  
  106.         arg1 = sys.argv[1]
  107.         if arg1 not in video_prefix:
  108.             raise ValueError('first argument must be Male,Female or Child')
  109.         if sys.argv[2].isdigit():
  110.             arg2 = int(sys.argv[2])
  111.         else:
  112.             raise ValueError('Second argument must be a number')
  113.     except IndexError:
  114.         print("Usage: python ./scareRandom.py [VideoName] [Minutes]")
  115.         sys.exit(1)
  116.     except ValueError as x:
  117.         print(x.message + "\nUsage: python ./scareRandom.py [VideoName] [Minutes]")
  118.         sys.exit(1)
  119.  
  120. start_up()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement