Python : Images (Tkinter) over a video (OMXPlayer)

233 views Asked by At

I would like to know if it is possible to display images on top of a video, let me explain: I am currently on a small Halloween project which consists of a digital board that displays a video (https://atmosfx.com/products/unliving-portraits). So I put my videos on Pause and when an event occurs, I launch the video and the jump scare is triggered. I am using OMXPlayer to manage the videos.

When a person walks past the board, the character should be able to follow the person with their eyes, I did this with Tkinter and PIL (frame by frame depending on position) and so far it works fine but separately, I am currently trying to group my two codes into one but a problem persists and I cannot correct it, The images following the person do not go above the video but below and I do not see how to fix this. I would like these images to be on top of the video while it is in Pause and disappear once the video starts.

#!/usr/bin/python3

# ----- Import python library -----
import time
import RPi.GPIO as GPIO
import sys
import json
import tkinter
from tkinter import Label
from tkinter import mainloop
from tkinter import *
from tkinter import Tk
from PIL import Image
from PIL.ImageTk import PhotoImage
from huskylib import HuskyLensLibrary
from omxplayer.player import OMXPlayer
from pathlib import Path

# ----- Initalize -----
hl = HuskyLensLibrary("SERIAL", "/dev/ttyUSB0", 3000000)
# Change to face recognition algorithms
hl.algorithm("ALGORITHM_FACE_RECOGNITION")

# BCM GPIO references instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Declaration Motionless
face_motionless_apparition_time = 0
motionless_timer = time.time()
last_motionless_block_seen = None
# Declaration Walking
face_walking_apparition_time = 0
music_apparition_time = 0
walking_timer = time.time()
music_timer = time.time()
last_walking_block_seen = None

# Video Jump Scare
files1_jump = "Madam_Jump_H.mp4"
files2_jump = "Gent_Jump_H.mp4"

# Run Tkinter
screen = Tk()
screen.overrideredirect(True) # Real fullscreen, no taskbar
screen.geometry('900x550') # Size of screen for the image ("background")
canvas = Canvas(screen, width = 900, height = 550) # Size of the canvas
canvas.pack()

# Image Following Eyes
Left_1 = PhotoImage(file = 'H_Gauche_1.jpg')
Middle = PhotoImage(file = 'H_Centre.jpg')
Right_1 = PhotoImage(file = 'H_Droite_1.jpg')

# Screen size
slength = '900' #1366
swidth = '550' #768
print("Starting up ...")
tgr = 0

try:
    # ----- VIDEO -----
    VIDEO_PATH = Path(files2_jump) # Display Gent_Jump
    player = OMXPlayer(VIDEO_PATH, args = ['--no-osd', '--loop', '--win', '0 0 {0} {1}'.format(slength, swidth)])
    time.sleep(1)

    while True:
        # ----- Ultrasonic Sensor -----

        # Pause the video
        player.pause()

        GPIO.setup(11, GPIO.OUT)

        # Cleanup output
        GPIO.output(11, 0)
        time.sleep(0.000002)    

        # Send signal
        GPIO.output(11, 1)
        time.sleep(0.000005)
        GPIO.output(11, 0)
        GPIO.setup(11, GPIO.IN)

        # Measure the distance
        while GPIO.input(11) == 0:
            start = time.time()

        while GPIO.input(11) == 1:
            stop = time.time()

        print("Measuring")

        duration = stop - start
        distance = duration * 34000 / 2
        DD = round(distance)
        print(DD, "cm")

        # ----- HuskyLens -----
        blocks = hl.requestAll() # Ask for Blocks and Arrows

        def eyes():
            # Left 1 [ | | | | |x| | | | | | ]
            if block.x >= 120 and block.x <= 149:
                eyesTopRight = canvas.create_image(0, 0, anchor = NW, image = Right_1) # PosX, PosY, Anchor, Image
                screen.update_idletasks()
                screen.update()
                time.sleep(0.5)
            # Middle [ | | | | |x| | | | | | ]
            if block.x >= 150 and block.x <= 160:
                eyesTopRight = canvas.create_image(0, 0, anchor = NW, image = Middle) # PosX, PosY, Anchor, Image
                screen.update_idletasks()
                screen.update()
                time.sleep(0.5)
            # Right 1 [ | | | | | | |x| | | | ]
            if block.x >= 170 and block.x <= 199:
                eyesTopRight = canvas.create_image(0, 0, anchor = NW, image = Left_1) # PosX, PosY, Anchor, Image
                screen.update_idletasks()
                screen.update()
                time.sleep(0.5)
            
        # TODO : WALKING
        if(200 <= DD): # If we are more than 100 cm away
            print("Inside WALKING")
            print("THERE IS NOBODY ! ")
            # We count the number of seconds between the last checkpoint and now
            face_walking_apparition_time = time.time() - walking_timer # (t+3) - t = 3, it's how u calculate timer
            print(face_walking_apparition_time)

        # Then we check if a face has appeared for more than 30 seconds:
        if face_walking_apparition_time > 30: # If the block is detected more than 30 seconds
            print("30 sec have passed, play the video")
            player.play()
            if face_walking_apparition_time > 50:
                print("20 sec have passed, pause the video")
                player.pause()
                walking_timer = time.time()
                face_walking_apparition_time = 0
            
    elif (50 < DD) and (DD < 200):
        print("THERE IS SOMEONE ! Reset all Walking's variable")
        # As we don't see no face, we have to reset our checkpoint to "now"
        walking_timer = time.time()
        music_timer = time.time()
        face__walking_apparition_time = 0
        music_apparition_time = 0
        time.sleep(0.1)

    for block in blocks: # for loop
        if block.type == "BLOCK": # If block is detected
            # TODO : EYES
            eyes()

            # TODO : PROXIMITY
            if DD <= 50: # If less than 50 cm
                player.play() # Play the video
                time.sleep(player.duration())
                tgr = tgr + 1
            else:
                pass
            player.set_position(0.0)

            # TODO : MOTIONLESS
            if 50 < DD and DD < 200: # If between 50 and 200 cm
                # Then we check if the last block was also a block. If yes, we increase our timer
                if last_motionless_block_seen == "BLOCK":
                    # We count the number of seconds between the last checkpoint and now
                    face_motionless_apparition_time = time.time() - motionless_timer # (t+3) - t = 3, it's how u calculate timer
                    print(face_motionless_apparition_time)
                    
                # Then we check if a face has appeared for more than 7 seconds:
                if face_motionless_apparition_time > 7: # If the block is detected more than 7 seconds
                    player.play() # Play the video
                    time.sleep(player.duration())
                    tgr = tgr + 1
                    face_motionless_apparition_time = 0 # Reset timer
                    motionless_timer = time.time()
                else:
                    pass
                player.set_position(0.0)
            else:
                # As we don't see no face, we have to reset our checkpoint to "now"
                motionless_timer = time.time()
                face_motionless_apparition_time = 0

            # Do not forget that we are going to look at the next block, so this block must be stored
            last_motionless_block_seen = block.type
            time.sleep(0.5)

        else:
            print("No Block")
            time.sleep(0.5)

    mainloop()

I want to make it clear that I am new to python and my code is most certainly horrible for you to read and I apologize in advance. Thanks for your help

0

There are 0 answers