0

I am creating a visual front end for a computer project. I have previously written some test code and can successfully play videos, loop a video and stop one and start the other to get a transition without a break.

What I am trying to do now is start the program and play a video in a loop whilst waiting for external activity (motion detection) using a PIR sensor.

Without the videos playing the code works (I think) but when I incorporate the video for some reason the video that is supposed to play when motion is detected plays as soon as the program starts (seemingly before its called). The second time it is called (when motion is detected) it does not play and just shows a still image).

Sometimes the wait for motion (vid1) plays again and sometimes it does not.

Here is my code - sorry I am no python expert

import os
import sys
from time import sleep
from omxplayer import OMXPlayer
from gpiozero import MotionSensor
from signal import pause
from picamera import PiCamera

pir = MotionSensor(23)

camera = PiCamera()
vid1 = OMXPlayer('/home/pi/Videos/Vid_Wait_Loop1.mp4',args=['--win', '100 100 640 480','--loop'])
vid2 = OMXPlayer('/home/pi/Videos/Vid_Name_Hammerstein.mp4',args=['--win', '100 100 640 480'])

def main():
    initiate()
    while True:
        print("playing vid1")
        vid1.play()
        if pir.motion_detected:
            motion_detected()
        else:
            no_motion()
    except KeyboardInterrupt:
        print("terminated by user")
        vid1.quit()
        vid2.quit()
        camera.close()
            try:
                sys.exit(0)
            except SystemExit:
                os.exit(0)
    finally:
           vid1.quit()
           vid2.quit()
         camera.close()
     print("bye bye")

def initiate():
    print("CENTRAL AI Startup - Running initial setup")
    sleep(1)
    print("Starting Central AI visual front end")
    sleep(1)
    print("Motion detection activated")
    sleep(1)
    print("Security system activated")

def no_motion():
    print("All quiet")
    vid1.play()

def motion_detected():
    print("Intruder Detected")
    sleep(2)
    print("Pausing loop")
    vid1.pause()
    print("Playing Vid_Name_Hammerstein")
    vid2.play()
    sleep(10)
    vid2.pause()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.