1

I've attempted to modify this script here, but instead of for 4 video outputs, to only 2. I'm pretty new to programming a pi and am trying to wrap this up for an gallery project. Here is my current script. The video plays currently, but get stuck at the 102 second mark. What I am attempting to do is have a video of an eye closed when in idle state, but when motion is sensored, to have the eye appear to open and look around. Any help is appreciated.

from omxplayer.player import OMXPlayer 
from pathlib import Path
import time #for determining the time of playback
import RPi.GPIO as GPIO #for taking signal from GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO_PIR = 7 #The GPIO is plugged into BCM pin 07 (GPIO pin 26)
GPIO.setup(GPIO_PIR,GPIO.IN) # Set pin as input
 
Current_Motion  = 0 #the current state of motion detection
Previous_Motion = 0 #the previous state

birdname = "goldfinch"
VIDEO_PATH = Path("/media/pi/ASTEYES/sensor_py_birds.mp4")

player = OMXPlayer(VIDEO_PATH)


''' The following variables need to be changed from one
    program to the next. They are the times when each event happens
    in the videos.'''
eyeclosed = 2 #eyeclosed
eyeopen = 102 #eyeopen

current_mode = 0 #determines which mode the player is in

motion_detect_bool = False #boolean for detecting motion

def eyeclosed_mode():
    player.set_position(eyeclosed)
    print("player seeked to " + str(player.position()) + "for eyeclosed")

def eyeopen_mode():
    player.set_position(eyeopen)
    print("player seeked to " + str(player.position()) + "for eyeopen")


def check_motion_sensor(Current_Motion, Previous_Motion):
    Current_Motion = GPIO.input(GPIO_PIR) # Read PIR state
    time.sleep(1)
    if Current_Motion == 1 and Previous_Motion == 0:
        Previous_Motion = 1
        return True
    elif Current_Motion == 0 and Previous_Motion == 1:
        Previous_Motion = 0
        return False

try:
    player.play()
    
    while GPIO.input(GPIO_PIR) == 1: #loop while the motion sensor is setting up and still reading high
        print ("Waiting for PIR to settle ...")
        time.sleep(2)
 
    print ("  Ready")
    
    current_time = int(time.time())

    while True:
        
        motion_sensor_reading = GPIO.input(GPIO_PIR)
        
        if current_mode == 0:
            player.play()
            next_mode = 1
        
        if current_mode == 1:
            motion_detect_bool = check_motion_sensor(Current_Motion, Previous_Motion)
            if motion_detect_bool == True:
                print("still motioned!")
                next_mode = 1
            elif motion_detect_bool == False:
                if player.position() > eyeopen-2: #loops the eyeopen mode before eyeclosed happens
                    eyeopen()                
        
        if current_mode == 1:
            motion_detect_bool = check_motion_sensor(Current_Motion, Previous_Motion)
            if motion_detect_bool == True:
                print("still motioned!")
                if int(player.position()) > eyeopen+5:
                    eyeopen_mode()
            elif motion_detect_bool == False:
                print("no more motion!")
                next_mode = 0

        if next_mode is not None:
            current_mode = next_mode #changes mode
        
        time.sleep(5) #sleep for 5 seconds
        print("current mode is " + str(current_mode))
        print("motion detect reading is " + str(motion_sonsor_reading))
        print("current time is " + str(current_time))

except KeyboardInterrupt:
    print ("  Quit")
    # Reset GPIO settings
    GPIO.cleanup()

1 Answer 1

1

Note: This question isn't Raspberry Pi specific. Please post these kinds of questions in Stack Overflow

This should work:

Remember to replace the ENDOV variable with the time that the video ends

from omxplayer.player import OMXPlayer 
from pathlib import Path
import time #for determining the time of playback
import RPi.GPIO as GPIO #for taking signal from GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO_PIR = 7 #The GPIO is plugged into BCM pin 07 (GPIO pin 26)
GPIO.setup(GPIO_PIR,GPIO.IN) # Set pin as input
 
Current_Motion  = 0 #the current state of motion detection
Previous_Motion = 0 #the previous state

VIDEO_PATH = Path("/media/pi/ASTEYES/sensor_py_birds.mp4")

player = OMXPlayer(VIDEO_PATH)


''' The following variables need to be changed from one
    program to the next. They are the times when each event happens
    in the videos.'''
eyeclosed = 2 #eyeclosed
eyeopen = 102 #eyeopen
ENDOV = 116 #IMPORTANT: Replace this with the actual end of the video

current_mode = 0 #determines which mode the player is in

motion_detect_bool = False #boolean for detecting motion

def eyeclosed_mode():
    player.set_position(eyeclosed)
    print("player seeked to " + str(player.position()) + "for eyeclosed")

def eyeopen_mode():
    player.set_position(eyeopen)
    print("player seeked to " + str(player.position()) + "for eyeopen")


def check_motion_sensor(current_motion, previous_motion):
    current_motion = GPIO.input(GPIO_PIR) # Read PIR state
    time.sleep(1)
    if current_motion == 1 and previous_motion == 0:
        previous_motion = 1
        return True
    elif current_motion == 0 and previous_motion == 1:
        previous_motion = 0
        return False
    print("The motion sensor reading is: " + str(current_motion))

try:
    player.play()
    
    while GPIO.input(GPIO_PIR) == 1: #loop while the motion sensor is setting up and still reading high
        print ("Waiting for PIR to settle ...")
        time.sleep(2)
 
    print ("  Ready")
    
    current_time = int(time.time())

    while True:
        
        motion_detect_bool = check_motion_sensor(Current_Motion, Previous_Motion)

        if motion_detect_bool:
            eyeopen_mode()
        
        if not motion_detect_bool:
            eyeclosed_mode()
                
        if player.position() > eyeopen-7: #loops the eyeclosed mode before eyeopen happens
            eyeclosed_mode()                

        if player.position() > ENDOV-7: #loops the eyeopen mode before video ends
            eyeopen_mode()         

        time.sleep(5) #sleep for 5 seconds
        print("current time is " + str(current_time))

except KeyboardInterrupt:
    print ("  Quit")
    # Reset GPIO settings
    GPIO.cleanup()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.