1

I want to create a photo booth with a simple sequence. I used mp4 files as animations.
Everything has to start in GUI with a button "start" that will run a loop for users. Exit to GUI will be by pressing the esc key.

In the "user" loop there will be a start animation as mp4 video -in omx player, working over and over until the user touches the touch screen. I already did this with listener move_mouse to kill the process.

I then have another animation with a countdown after which the camera takes a picture and then displays the photo on the screen in a window with two buttons repeat or print.

The problem with the listener is it freezes the application after capturing the photo

How can I solve this problem?

try:
    from Tkinter import * # Python2
except ImportError:
    import tkinter as Tk  # Python3import Tkinter as tk

import sys
from time import sleep
from PIL import  Image, ImageTk
from datetime import datetime
from sh import gphoto2 as gp
import signal, os, subprocess, glob
from pynput import mouse
from pynput import keyboard

shot_date = datetime.now() .strftime("%Y-%m-%d")
shot_time = datetime.now() .strftime("%Y-%m-%d %H:%M:%S")
picID = "PIShots"
folder_name = shot_date + picID
save_location = "/home/pi/Desktop/gphoto/images/" + folder_name


# listener
def on_move(x,y):
    print("mouse move")
    playerOff()
    #pynput.mouse.Listener.stop
    playerOn()
    capturePhoto()

#  press key listener   
#def on_release(key):
#    print('{0} released'.format(
#        key))
#    if key == keyboard.Key.esc:
        # Stop listener
#        return False

#StartAnmation and listener
def Start():
    omxc= subprocess.Popen(['omxplayer','-b','--loop','--no-osd',"/home/pi/Desktop/START.mp4"])
    with mouse.Listener(
            on_move=on_move) as listener:
            listener.join()     

# get the latest file
def get_latest_file(path, *paths):
    """Returns the name of the latest (most recent) file
    of the joined path(s)"""
    fullpath = os.path.join(path, *paths)
    files = glob.glob(fullpath)  # You may use iglob in Python3
    if not files:                # I prefer using the negation
        return None                      # because it behaves like a shortcut
    latest_file = max(files, key=os.path.getctime)
    _, filename = os.path.split(latest_file)
    return filename

#start_animation in OMX player
def playerOn():
    omxc= subprocess.Popen(['omxplayer','-b',"/home/pi/Desktop/animacja.mp4"])

#Player Off
def playerOff():
    os.system('killall omxplayer.bin')

#CreateSaveFolder
def createSaveFolder():
    try:
        os.makedirs(save_location)
    except:
        print("Failed to create the new directory")
    os.chdir(save_location)

def quit(root):
    root.destroy()

def capturePhoto():
    status = 0
    createSaveFolder()
    sleep(6)
    os.system('fswebcam -r 1920x1080 -s brightness=70% -s gain=50% -S 10 --set lights=off --no-banner %H%M%S.jpg')
    print save_location
    location=get_latest_file(save_location,'*.jpg')
   #print location
    sciezkaZdj= save_location + "/" + location
    print sciezkaZdj
    im = Image.open(sciezkaZdj)
    width, height =im.size

    LEFT_HALF = 200, 0, width-400 ,height
    im = im.crop(LEFT_HALF)
    im = im.transpose(Image.ROTATE_270)

    ramka = Image.open("/home/pi/Desktop/ramka1.jpg")
    text_img = Image.new('RGBA', (1200,2000), (0, 0, 0, 0))

    text_img.paste(ramka, (0,0))
    text_img.paste(im, (50,30))
    text_img.save("ramka.png", format="png")
    path = save_location + "/ramka.png"
    top2 = Toplevel(window)
    top2.geometry("1600x720")
    top2.overrideredirect(1)
    top2.title("fotobudka")
    top2.configure(background='black')

    img = ImageTk.PhotoImage(Image.open(path))
    panel = Label(top2, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    playerOff()

        #close gui
    window.after(5000, lambda:  top2.destroy())    

#************MAIN

if __name__ == "__main__":          
try:

            #Gui main*********************************

            window = Tk()
            top1 = Toplevel(window)
            top1.geometry("1600x720")
         #  top1.wm_attributes('-topmost',1) #zawsze na wierzchu
            top1.configure(background='black')
            button_start = Button(window, text='start',command=Start)
            button_start.pack()
            window.mainloop()    


        #Gui main end******************************

except KeyboardInterrupt:
        print "koniec"
ChrisM
  • 505
  • 6
  • 18
Maciej P
  • 23
  • 5
  • Read [Run your own code alongside Tkinter's event loop](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) – stovfl Sep 13 '19 at 15:30

0 Answers0