首页 > 解决方案 > Python:视频(OMXPlayer)上的图像(Tkinter)

问题描述

我想知道是否可以在视频顶部显示图像,让我解释一下:我目前正在进行一个小型万圣节项目,该项目由一个显示视频的数字板组成(https://atmosfx.com/products /unliving-肖像)。因此,我将视频置于暂停状态,当事件发生时,我启动视频并触发跳跃恐慌。我正在使用 OMXPlayer 来管理视频。

当一个人走过棋盘时,角色应该能够用眼睛跟随这个人,我用 Tkinter 和 PIL 做到了这一点(逐帧取决于位置),到目前为止它工作正常但分开,我目前正在尝试将我的两个代码合二为一,但问题仍然存在,我无法更正它,跟随此人的图像不在视频上方而是在下方,我不知道如何解决此问题。我希望这些图像在暂停时位于视频顶部,并在视频开始后消失。

#!/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()

我想明确表示我是 python 新手,我的代码对你来说肯定很糟糕,我提前道歉。谢谢你的帮助

标签: pythontkinterraspberry-pipython-imaging-libraryomxplayer

解决方案


推荐阅读