我正在尝试使用python 3在树莓派上的tkinter中启动/停止视频文件。
每次红外传感器为LOW(断)时,我都需要从头开始播放视频,并在传感器再次为HIGH时停止。理想情况下,视频应放在tkinter画布中,以便我可以同时在屏幕上显示其他元素(例如,加载栏)。
我设法使除视频之外的所有内容都运行起来,视频在检测到传感器后立即运行,但是它冻结了所有其他过程(例如加载栏),并且当传感器为HIGH时并没有停止。
这是代码的简化(未经检查)版本,可让您大致了解结构(实际代码要长得多):
import tkinter as TK
import RPi.GPIO as GPIO
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.IN)
class App:
def __init__(self, root):
self.root = root
self.root.config(background = 'black', cursor = 'none')
self.background = TK.Canvas(root, width = 1024, height = 600, bg = 'black')
self.background.pack()
self.ext = 0
self.trial = 0
self.infrared()
def infrared(self):
if (GPIO.input(14) == False):
self.makebar()
if (self.ext == 0):
self.runvideo()
else:
os.system("killall omxplayer.bin")
self.ext = 0
self.root.after(16, self.infrared)
def runvideo(self):
os.system("omxplayer /home/pi/Desktop/testvideo.m4v")
def makebar():
self.stimulus_rect = TK.Canvas(root, width = 1024, height = 50, bg= 'white')
if self.ext < 1000
self.ext = self.ext + 10
self.stimulus_rect.create_rectangle(0, 0, self.ext, 50, fill="red")
self.stimulus_rect.place(x=0, y=0, anchor="new")
else:
self.trial = self.trial + 1
self.ext = 0
root = TK.Tk()
App(root)
root.mainloop()
根据我在网上找到的信息: 1)tkinter可能与opencv结合使用来实现此目的,但看起来不像在树莓派上安装opencv那样简单; 2)通常,涉及“ os”的选项似乎注定要失败。
我找不到一种干净的方法。我梦dream以求的场景是将视频帧一张一张地加载到画布上,并以60hz(屏幕频率)加载。然后,我将以完全相同的频率检查传感器,并在传感器未损坏的情况下防止装入下一帧。用伪代码看起来像这样
def infrared(self):
if (GPIO.input(14) == False):
self.makebar()
if (self.ext == 0):
self.runvideo()
else:
self.video.stop
self.ext = 0
self.frame = 0
self.root.after(16, self.infrared)
def runvideo(self):
self.frame = self.frame + 1
video.run("testvideo.m4v", self.frame)
关于如何在树莓派上的tkinter中实现此目标的任何想法?
谢谢 蚂蚁
答案 0 :(得分:0)
经过一周的研究,反复试验和错误,这就是我目前所需要的(以伪代码形式):
#### PSEUDOCODE ####
from subprocess import Popen # this library is used to open the video file
class App:
def __init__(self, root):
self.moviestart = False
self.movieduration = 130000
self.movie = "/home/pi/Desktop/test.mp4"
def infrared(self):
if (GPIO.input(IR) == False):
if not self.moviestart:
self.makevideo() # Call the function to start the video
self.moviestart = True # Flag that the video has started
self.moviestop = False # Flag that the video is currently playing
self.root.after(self.videoduration,
self.stopvideo) # manually call the stop video function
# to stop the video after 13 seconds (video's length).
# I could not find a more elegant way of doing this, unfortunately.
else:
self.clear_screen()
self.root.after(self.refreshIR, self.infrared)
def makevideo(self):
# popen will open the movie in a window of the size I specified,
# so that other element from tkinter can be placed on top of the movie window
omxc = Popen(['omxplayer', self.movie, '--win', "0 30 800 450"])
def stopvideo(self):
self.moviestart = False # flag that movie has been stopped
if (self.moviestop == False): # check if the movie is currently playing
try: # this is a cheap workaround other problems I had, do not try this at home
os.system('killall omxplayer.bin') # this literally kills any omxplayer instance
# currently open
self.moviestop = True # flag that the movie is not playing at the moment
except:
pass
我希望这对其他有类似问题的人有用。如果找到更好的解决方案,我将更新答案。目前,这已经足够好了。