pythonpython-3.xtkinterraspberry-piomxplayer

Omxplayer-wrapper problems when run from terminal


I have the following program that I've been working on (I didn't create all of it, so some of it is pretty weird) on a Raspberry Pi. It's basically supposed to open up a tkinter window with buttons, and if you click the buttons in the right order, a new process will start. If the buttons are clicked in the wrong order, it will play a video. All of this works in Thonny IDE. (Really, the specifics of the program don't matter much other than the fact that it is using omxplayer-wrapper to play videos)

import os, subprocess
import RPi.GPIO as GPIO
import sys
import os
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
import time
from omxplayer.player import OMXPlayer
from pathlib import Path

GPIO.setmode(GPIO.BCM)


# Variables
wrongOrder=False
desired_order=[1,2,3,8,9,10] #Desired order of clicking on images
click_num=0  #Number of clicks
videoPath = "MOV5.mp4"
loopPath = "loop2.mp4"
incorrectPath = "Incorrect Password.mp4"
secondVideoPath = "Access Denied.mp4"
secondLoopPath = "loop1.mp4"
GPIO.setup(20, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(19, GPIO.OUT, initial=GPIO.HIGH)
GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(2, GPIO.IN) #The input for when the power rod is removed (stop 1st looping video)
GPIO.setup(3, GPIO.IN) #The input for when to stop the 2nd looping video




# Actions taken when clicked on each image
def on_click1(event=None):
    global IM
    print("image1 clicked")
    IM=1 #the image number
    order(IM)


def on_click2(event=None):
    global IM
    print("image2 clicked")
    IM=2
    order(IM)


def on_click3(event=None):
    global IM
    print("image3 clicked")
    IM=3
    order(IM)


def on_click4(event=None):
    global IM
    print("image4 clicked")
    IM=4
    order(IM)

def on_click5(event=None):
    global IM
    print("image5 clicked")
    IM=5
    order(IM)

def on_click6(event=None):
    global IM
    print("image6 clicked")
    IM=6
    order(IM)

def on_click7(event=None):
    global IM
    print("image7 clicked")
    IM=7
    order(IM)

def on_click8(event=None):
    global IM
    print("image8 clicked")
    IM=8
    order(IM)

def on_click9(event=None):
    global IM
    print("image9 clicked")
    IM=9
    order(IM)

def on_click10(event=None):
    global IM
    print("image10 clicked")
    IM=10
    order(IM)

root = tk.Tk()

#Function when an image is clicked
def order(IM):
    global click_num, wrongOrder
    if (IM != desired_order[click_num]): #If this is not the right button
        wrongOrder = True 
    click_num += 1
    aCanvas.itemconfigure(asterisks, text = aCanvas.itemcget(asterisks, "text")+"*") #Adds an asterisk to the text
    if (click_num == 6): #If it is the last number
        if (wrongOrder): 
            print("Wrong Order!")
            click_num = 0
            wrongOrder = False
            aCanvas.itemconfigure(asterisks, text = "") #Set text back to no asterisks
            incorrectVideo()
        else:
            print("Correct Order!")
            afterPassword()


def afterPassword():
    playVideo()
    time.sleep(11)  #Wait before door opens
    PIN()       #play_video, pin low
    time.sleep(8)
    root.destroy()
    playFirstLoop() #This will have to wait for a GPIO pin to change
    playSecondVideo()
    PIN2()#Trigger different relay
    relayFlash()
    piOutput() #Trigger other pi 
    playSecondLoop()




def PIN():
    GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW)           # set GPIO21 as an output
    #GPIO.output(21, 0)         # set GPIO21 to 0/GPIO.LOW/True
def PIN2():
    GPIO.output(20,0)
def playVideo():
    player = OMXPlayer(videoPath, args=["--win", "0 0 1280 1024"])

def playFirstLoop(): 
    player = OMXPlayer(loopPath, args=["--loop","--no-osd", "--win", "0 0 1280 1024"])
    while(True):
        if (GPIO.input(2)==False): 
            player.quit()
            break

def playSecondVideo():
    player = OMXPlayer(secondVideoPath, args=["--win", "0 0 1280 1024"])
    player.quit()


def playSecondLoop(): 
    player = OMXPlayer(secondLoopPath, args=["--loop","--no-osd", "--win", "0 0 1280 1024"])
    while(True):
        if (GPIO.input(3)==False): 
            player.quit()
            break

def incorrectVideo():
    player = OMXPlayer(incorrectPath, args=["--win", "0 0 1280 1024"], dbus_name='org.mpris.MediaPlayer2.omxplayer0')
    time.sleep(2)
    player.quit()

def relayFlash():
    GPIO.output(19,0)
    time.sleep(0.5)
    for i in range(0,14):
        GPIO.output(19,1)
        time.sleep(0.5)
        GPIO.output(19,0)
        time.sleep(0.5)
    GPIO.output(19,1)

def piOutput():
    GPIO.output(18,1)


size_x=249
size_y = 350
#Screen resolution: 1245 x 1044

# load images
image1 = Image.open("/home/pi/Documents/1.jpg")
image1 = image1.resize((size_x, size_y), Image.ANTIALIAS)
photo1 = ImageTk.PhotoImage(image1)

image2 = Image.open("/home/pi/Documents/2.jpg")
image2 = image2.resize((size_x, size_y), Image.ANTIALIAS)
photo2 = ImageTk.PhotoImage(image2)

image3 = Image.open("/home/pi/Documents/3.jpg")
image3 = image3.resize((size_x, size_y), Image.ANTIALIAS)
photo3 = ImageTk.PhotoImage(image3)

image4 = Image.open("/home/pi/Documents/4.jpg")
image4 = image4.resize((size_x, size_y), Image.ANTIALIAS)
photo4 = ImageTk.PhotoImage(image4)

image5 = Image.open("/home/pi/Documents/5.jpg")
image5 = image5.resize((size_x, size_y), Image.ANTIALIAS)
photo5= ImageTk.PhotoImage(image5)

image6 = Image.open("/home/pi/Documents/6.jpg")
image6 = image6.resize((size_x, size_y), Image.ANTIALIAS)
photo6 = ImageTk.PhotoImage(image6)

image7 = Image.open("/home/pi/Documents/7.jpg")
image7 = image7.resize((size_x, size_y), Image.ANTIALIAS)
photo7 = ImageTk.PhotoImage(image7)

image8 = Image.open("/home/pi/Documents/8.jpg")
image8 = image8.resize((size_x, size_y), Image.ANTIALIAS)
photo8 = ImageTk.PhotoImage(image8)

image9 = Image.open("/home/pi/Documents/9.jpg")
image9 = image9.resize((size_x, size_y), Image.ANTIALIAS)
photo9 = ImageTk.PhotoImage(image9)

image10 = Image.open("/home/pi/Documents/0.jpg")
image10 = image10.resize((size_x, size_y), Image.ANTIALIAS)
photo10 = ImageTk.PhotoImage(image10)


buttonFrame = Frame(root)
# button with image binded to the same function
b1 = tk.Button(buttonFrame, image=photo1, command=on_click1)
b2 = tk.Button(buttonFrame, image=photo2, command=on_click2)
b3 = tk.Button(buttonFrame, image=photo3, command=on_click3)
b4 = tk.Button(buttonFrame, image=photo4, command=on_click4)
b5 = tk.Button(buttonFrame, image=photo5, command=on_click5)
b6 = tk.Button(buttonFrame, image=photo6, command=on_click6)
b7 = tk.Button(buttonFrame, image=photo7, command=on_click7)
b8 = tk.Button(buttonFrame, image=photo8, command=on_click8)
b9 = tk.Button(buttonFrame, image=photo9, command=on_click9)
b10 = tk.Button(buttonFrame, image=photo10, command=on_click10)

aFrame = Frame(root,bg="white") #Make a new frame containing the canvas
aCanvas = Canvas(aFrame, width = 747, height = 324) #Make canvas for asterisks
aCanvas.create_rectangle(0,0,747,324, fill="white", width = 15) #Make rectangle for asterisks to display on
squarePaddingX = 0
squarePaddingXStart = 25
squarePaddingY = 87
squareLength = 117
for i in range(0,6):
    aCanvas.create_rectangle((squarePaddingX * (i) + squarePaddingXStart)+(squareLength * i),squarePaddingY,(squarePaddingX*(i))+(squareLength * (i+1) + squarePaddingXStart),squareLength+squarePaddingY, width = 5) #Crazy code that creates squares. Set variables above, don't touch this
asterisks = aCanvas.create_text(25,50,text="",font=("Verdana", "175"), anchor=tk.NW) #Make text for asterisks
aCanvas.pack()



b1.grid(row=0,column=0)
b2.grid(row=0,column=1)
b3.grid(row=0,column=2)
b4.grid(row=0,column=3)
b5.grid(row=0,column=4)
b6.grid(row=1,column=0)
b7.grid(row=1,column=1)
b8.grid(row=1,column=2)
b9.grid(row=1,column=3)
b10.grid(row=1,column=4)
buttonFrame.grid(row=0) #Set buttonframe above asterisk frame
aFrame.grid(row=1)

root.mainloop()

GPIO.cleanup()                 #resets all GPIO ports used by this program

My problem is that I want this to run when the Pi boots up, which involves starting the program from the terminal. If I use "sudo python3 [file path]", it tells me: "Traceback (most recent call last): File "/home/pi/Documents/alienPasscodeV2.py", line 10, in from omxplayer.player import OMXPlayer ImportError: No module named 'omxplayer'".

If I use just "python3 [file path]", it starts off working, until it needs to play a video. Then it tells me:

"Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/init.py", line 1562, in call return self.func(*args) File "/home/pi/Documents/alienPasscodeV2.py", line 86, in on_click7 order(IM) File "/home/pi/Documents/alienPasscodeV2.py", line 121, in order incorrectVideo() File "/home/pi/Documents/alienPasscodeV2.py", line 171, in incorrectVideo player = OMXPlayer(incorrectPath, args=["--win", "0 0 1280 1024"], dbus_name='org.mpris.MediaPlayer2.omxplayer0') File "/home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py", line 162, in init self.load(source, pause=pause) File "/home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py", line 245, in load self._load_source(source) File "/home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py", line 171, in _load_source self._connection = self._setup_dbus_connection(self._Connection, self._bus_address_finder) File "/home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py", line 231, in _setup_dbus_connection raise SystemError('DBus cannot connect to the OMXPlayer process') SystemError: DBus cannot connect to the OMXPlayer process"

I have no idea why there's even a difference between using sudo and not using sudo. I've been trying to figure this out for hours and I have no idea. I'm so sorry for this program.


Solution

  • Turns out that when I was setting the paths, I needed to set the full path, not just the name of the file. So the paths changed to /home/pi/Documents/filename.