python初心者です
cv2.imshowを使わないリピート動画再生で
(https://scribles.net/showing-video-image-on-tkinter-window-with-opencv/)
外部ボタンによる停止でエラーが発生します。
どのような回避方法が有るのでしょうかアドバイス下さい。
(最初のループ動画をスタートボタンで停止し新しいループフォト表示と考えています
python
1# coding: utf-8 2import tkinter as tk 3from tkinter import ttk 4from tkinter import * 5from PIL import Image, ImageTk 6import cv2 7 8class Application(tk.Frame): 9 def __init__(self, master): 10 super().__init__(master) 11 self.pack() 12 self.master.title("Digital Photo") 13 self.menu_frame = tk.Frame(self, width=200) 14 self.menu_frame.grid(row=0, column=0, sticky=tk.NSEW) 15 self.main_frame = tk.Frame(self, width=800, bg="red") 16 self.main_frame.grid(row=0, column=1, sticky=tk.NSEW) 17 18 self.MenuFrame() 19 self.MainCanvas() 20 21 def MainCanvas(self): 22 23 self.win_w = 800 24 self.win_h = 480 25 videoFile="./video/2018年おうし座流星群 大火球出現.mp4" 26 # 取り敢えずyoutubeから https://youtu.be/JTUb5Bxkvck 流用 27 self.cap = cv2.VideoCapture(videoFile) 28 self.img_w = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH) # 動画幅 29 self.img_h = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # 動画高 30 self.canvas1 = tk.Canvas(self.main_frame, width=self.win_w-10, height=self.win_h-10, bg="green") 31 self.canvas1.grid(row=0, column=0, sticky=tk.NSEW) 32 33 ### top_button 34 icon3 = Image.open('./inifile/top.png') 35 icon3 = icon3.resize(size=(55, 57)) 36 self.icon3 = ImageTk.PhotoImage(icon3) 37 self.top_btn = tk.Button(self.main_frame, image=self.icon3, relief=tk.FLAT) 38 # self.top_btn.grid(row=0, column=0, padx=10, pady=10, sticky=tk.SW) 39 40 41 # Update image on canvas 42 self.interval = 10 # Interval in ms to get the latest frame 43 self.update_image() 44 45 def update_image(self): 46 47 # Get the latest frame and convert image format 48 image = cv2.cvtColor(self.cap.read()[1], cv2.COLOR_BGR2RGB) # to RGB 49 image = Image.fromarray(image) # to PIL format 50 image = image.resize((int(self.img_w * (self.win_h / self.img_h)), int(self.win_h))) 51 self.image = ImageTk.PhotoImage(image) # to ImageTk format 52 53 # roop ok 54 ret, frame = self.cap.read() 55 # print(ret) 56 if ret: 57 # Update image 58 self.canvas1.create_image(0, 0, anchor=tk.NW, image=self.image) 59 self.canvas1.create_image(self.win_w / 2 - (self.img_w * (self.win_h / self.img_h) / 2), 0, image=self.image, 60 anchor=tk.NW) 61 # 指定した時間経過後 self.update_image を実行 62 self.master.after(self.interval, self.update_image) 63 64 else: 65 self.cap.set(cv2.CAP_PROP_POS_FRAMES, 0) 66 self.update_image() 67 68 69 def MenuFrame(self): 70 71 ### load_button 72 icon1 = Image.open('./inifile/start.png') 73 icon1 = icon1.resize(size=(60, 57)) 74 self.icon1 = ImageTk.PhotoImage(icon1) 75 # load_btn = tk.Button(menu_frame, 76 # image=icon1, relief=tk.FLAT, command=load_btn_click) 77 self.load_btn = tk.Button(self.menu_frame, image=self.icon1, relief=tk.FLAT) 78 79 ### exit_button 80 icon2 = Image.open('./inifile/exit.png') 81 icon2 = icon2.resize(size=(44, 57)) 82 self.icon2 = ImageTk.PhotoImage(icon2) 83 # exit_btn = tk.Button(menu_frame, 84 # image=icon2, relief=tk.FLAT, command=exit_btn_click) 85 self.exit_btn = tk.Button(self.menu_frame, image=self.icon2, relief=tk.FLAT) 86 87 ### control Layout grid 88 self.load_btn.grid(row=5, column=0, padx=(20, 0), pady=30, sticky=tk.W) 89 self.exit_btn.grid(row=6, column=0, padx=0, pady=0) 90 91 92def main(): 93 root = tk.Tk() 94 app = Application(master=root) 95 96 def show_animation(maxsize=200): 97 def animation(value): 98 value = min(value, maxsize) 99 root.grid_columnconfigure(0, minsize=value) 100 if value < maxsize: 101 root.after(20, animation, value+20) 102 app.menu_frame.grid(row=0, column=0, sticky=tk.NSEW) 103 app.top_btn.grid_forget() 104 root.after_idle(animation, 10) 105 # add code 106 topVideo_show() 107 108 109 def hide_animation(value=200): 110 def animation(value): 111 value = max(0, value-20) 112 root.grid_columnconfigure(0, minsize=value) 113 if value > 0: 114 root.after(20, animation, value) 115 else: 116 app.grid_propagate(False) 117 app.menu_frame.grid_forget() 118 app.top_btn.grid(row=0, column=0, padx=10, pady=10, sticky=tk.SW) 119 # add code 120 canvas_clear() 121 122 root.after_idle(animation, value) 123 124 def toggle_menu(e): 125 is_shown = root.grid_columnconfigure(0)["minsize"] 126 if not is_shown: 127 show_animation() 128 else: 129 hide_animation() 130 131 def canvas_clear(): 132## //// エラー発生 ///// 133 app.cap.release() 134 app.canvas1.delete("all") 135## エラー内容↓ 136## self.image = cv2.cvtColor(self.video.read()[1], cv2.COLOR_BGR2RGB) # to RGB 137## cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' 138 139 def topVideo_show(): 140 app.MainCanvas() 141 142 143 root.bind_all("<F4>", toggle_menu) 144 app.exit_btn.config(command=root.quit) 145 app.load_btn.config(command=hide_animation) 146 app.top_btn.config(command=show_animation) 147 148 app.mainloop() 149 150if __name__ == "__main__": 151 main()
--環境--
python3.8
tkinter8.6
pycharm2021.1
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/11 05:52
2021/06/11 09:57
2021/06/12 01:06
2021/06/12 09:16