self.root = tk.Tk() self.root.title("视频处理系统") self.root.geometry("1250x1200") global last_frame1 # creating global variable self.last_frame1 = np.zeros((480, 640, 3), dtype=np.uint8) global last_frame2 # creating global variable self.last_frame2 = np.zeros((480, 640, 3), dtype=np.uint8) global cap1 global cap2 self.cap1 = cv2.VideoCapture("./movie/video_1.mp4") self.cap2 = cv2.VideoCapture("./movie/video_1_sol.mp4") self.key = 0 self.button2 = tk.Button(self.root, text="打开视频", command=lambda: [self.show_vid(), self.show_vid2()]) self.button2.grid(row=0, column=1) def show_vid(self): if not self.cap1.isOpened(): print("cant open the camera1") flag1, frame1 = self.cap1.read() if frame1 is None: print("Failed to read frame from camera1. Stopping...") self.cap1.release() return frame1 = cv2.resize(frame1, (640, 480)) if flag1 is None: print("Major error!") elif flag1: global last_frame1 self.last_frame1 = frame1.copy() pic = cv2.cvtColor(self.last_frame1, cv2.COLOR_BGR2RGB) img = Image.fromarray(pic) imgtk = ImageTk.PhotoImage(image=img) self.lmain.imgtk = imgtk self.lmain.configure(image=imgtk) if self.key==0: self.lmain.after(10, self.show_vid)不能再次播放视频怎么解决
时间: 2024-03-31 16:33:21 浏览: 109
在你的代码中,当你调用 show_vid() 函数时,它只会播放一次视频,因为在函数内部,你只设置了一次 after() 函数来更新视频帧。如果想要循环播放视频,可以使用一个 while 循环来不断读取视频帧并更新显示,例如:
```
def show_vid(self):
while True:
if not self.cap1.isOpened():
print("cant open the camera1")
break
flag1, frame1 = self.cap1.read()
if frame1 is None:
print("Failed to read frame from camera1. Stopping...")
self.cap1.release()
break
frame1 = cv2.resize(frame1, (640, 480))
if flag1 is None:
print("Major error!")
elif flag1:
global last_frame1
self.last_frame1 = frame1.copy()
pic = cv2.cvtColor(self.last_frame1, cv2.COLOR_BGR2RGB)
img = Image.fromarray(pic)
imgtk = ImageTk.PhotoImage(image=img)
self.lmain.imgtk = imgtk
self.lmain.configure(image=imgtk)
if self.key==0:
self.lmain.after(10)
```
这样,当你调用 show_vid() 函数时,它会不断循环读取视频帧并更新显示,直到你手动停止程序。
阅读全文