Python:视频上的图像TkinterOMXPlayer

问题描述

我想知道是否可以在视频上显示图像,让我解释一下: 我目前正在进行一个小型万圣节项目,该项目由一个显示视频 (https://atmosfx.com/products/unliving-portraits) 的数字板组成。所以我把我的视频放在暂停状态,当一个事件发生时,我启动视频并触发跳跃恐慌。我正在使用 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,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,image = Middle) # PosX,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,image = Left_1) # PosX,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 新手,我的代码肯定让你读起来很糟糕,我提前道歉。 感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...