diff --git a/.gitignore b/.gitignore index 1c20eca..14ecd7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.mp4 *.csv +*.png diff --git a/__pycache__/lumen_profiler.cpython-313.pyc b/__pycache__/lumen_profiler.cpython-313.pyc new file mode 100644 index 0000000..b550c01 --- /dev/null +++ b/__pycache__/lumen_profiler.cpython-313.pyc Binary files differ diff --git a/gui_app.py b/gui_app.py index d3fd882..2bc4cd2 100644 --- a/gui_app.py +++ b/gui_app.py @@ -1,9 +1,63 @@ -from tkinter import * +import tkinter as tk +import tkinter.filedialog -master = Tk() -w = Scale(master, from_=0, to=42) -w.pack() -w = Scale(master, from_=0, to=200, orient=HORIZONTAL) -w.pack() +from PIL import Image, ImageOps, ImageTk -mainloop() +import lumen_profiler + + +class GuiApp: + def __init__(self): + self.tkroot = tk.Tk() + self.lp = lumen_profiler.LumenProfiler() + + def make_window(self): + self.tkroot.title("Lumen Profiler") + self.tkroot.geometry("800x600") + self.top_frame = tk.Frame(self.tkroot) + self.top_frame.pack(fill=tk.X) + self.file_button = tk.Button( + self.top_frame, text="映像ファイルを開く", command=self.open_movie + ) + self.file_button.pack(side=tk.LEFT) + self.file_text = tk.Label(self.top_frame, text="no movie") + self.file_text.pack(side=tk.LEFT, fill=tk.X) + self.canvas = tk.Canvas(self.tkroot) + self.canvas.pack(fill=tk.BOTH, expand=True) + self.time_slider = tk.Scale( + self.tkroot, + from_=0, + to=0, + orient=tk.HORIZONTAL, + command=self.on_time_slider_change, + ) + self.time_slider.pack(fill=tk.X) + + self.tkroot.mainloop() + + def open_movie(self): + filename = tk.filedialog.askopenfilename(filetypes=[("mp4", "*.mp4")]) + if filename == "": # cancel + return + self.lp.load_movie(filename) + self.file_text.config(text=filename) + self.time_slider.set(0) + self.time_slider.config(to=self.lp.frame_count - 1) + self.show_image(self.lp.frames[0]) + + def on_time_slider_change(self, event): + rid = self.time_slider.get() + self.show_image(self.lp.frames[rid]) + + def show_image(self, img): + pil = Image.fromarray(self.lp.bgr2rgb(img)) + pil = ImageOps.pad(pil, (self.canvas.winfo_width(), self.canvas.winfo_height())) + self.disp = ImageTk.PhotoImage(pil) + # self.canvas.delete("all") + self.canvas.create_image(0, 0, image=self.disp, anchor=tk.NW) + + +# main +if __name__ == "__main__": + app = GuiApp() + app.make_window() diff --git a/lumen_profiler.py b/lumen_profiler.py index 52d6e35..2e6bfb5 100644 --- a/lumen_profiler.py +++ b/lumen_profiler.py @@ -5,7 +5,7 @@ import numpy as np -class lumen_profiler: +class LumenProfiler: def __init__(self): self.area_ratio = 0.08 @@ -142,6 +142,11 @@ ] writer.writerow(row_data) + def bgr2rgb(self, img, size=None): + if size is not None: + img = cv2.resize(img, size) + return cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + # def func(): # cv2.putText( @@ -162,7 +167,7 @@ movie_file = args[1] # 解析 - lp = lumen_profiler() + lp = LumenProfiler() lp.load_movie(movie_file) lp.profiling(2) lp.csv_output("output/analysis.csv") diff --git a/test.py b/test.py new file mode 100644 index 0000000..149b9e4 --- /dev/null +++ b/test.py @@ -0,0 +1,21 @@ +import tkinter as tk + +import cv2 +from PIL import Image, ImageOps, ImageTk + + +def show_image(): + global disp + img = cv2.imread("img.png") + pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) + pil = ImageOps.pad(pil, (canvas.winfo_width(), canvas.winfo_height())) + disp = ImageTk.PhotoImage(pil) + canvas.create_image(0, 0, image=disp, anchor=tk.NW) # dispの生存期間に注意 + + +tkroot = tk.Tk() +button = tk.Button(tkroot, text="画像を表示", command=show_image) +button.pack() +canvas = tk.Canvas(tkroot) +canvas.pack(fill=tk.BOTH, expand=True) +tkroot.mainloop()