おっ、これは初めて自分が他の人の質問に答えられそうですね。これは以前僕がやってみたものですが。
tkinterのafterメソッド内で動かすとうまくいくと思いますよ。
python
1import tkinter as tk
2from PIL import Image,ImageTk
3import cv2
4import numpy as np
5
6class GUI:
7 def __init__(self):
8 self.cvv=CV2()
9 self.root=tk.Tk()
10 self.ROOT_X = 1000
11 self.ROOT_Y = 700
12 self.CANVAS_X=640
13 self.CANVAS_Y=480
14 self.root.title(u"tkcv")
15 self.root.geometry(str(self.ROOT_X) + "x" + str(self.ROOT_Y))
16 self.root.resizable(width=0, height=0)
17
18 self.count_num=0
19
20 self.firstFrame()
21 self.afterMSec()
22
23 def afterMSec(self):
24 self.count_num+=1
25 self.label_count.configure(text=str(self.count_num))
26
27 self.cvv.cameraFrame()
28
29 self.loop_img = Image.fromarray(self.cvv.frame_flip)
30
31 self.canvas_img = ImageTk.PhotoImage(self.loop_img)
32 self.canvas.create_image(self.CANVAS_X / 2, self.CANVAS_Y / 2, image=self.canvas_img)
33
34 self.root.after(10, self.afterMSec)
35
36 def firstFrame(self):
37 self.first_frame = tk.Frame(self.root, bd=2, relief="ridge", bg="white",
38 width=self.ROOT_X, height=self.ROOT_Y)
39 self.first_frame.grid(row=0, column=0)
40
41 self.label_count = tk.Label(self.first_frame, text=str(self.count_num),font=("", 40))
42 self.label_count.place(x=50,y=50,width=600)
43
44 self.canvas = tk.Canvas(self.root, width=self.CANVAS_X, height=self.CANVAS_Y)
45 self.canvas.create_rectangle(0, 0, self.CANVAS_X, self.CANVAS_Y, fill="#696969")
46 self.canvas.place(x=300, y=200)
47
48 #opencvからpil
49 self.pil_img=Image.fromarray(self.cvv.img)
50
51 self.canvas_img = ImageTk.PhotoImage(self.pil_img)
52 self.canvas.create_image(self.CANVAS_X / 2, self.CANVAS_Y / 2, image=self.canvas_img)
53
54
55
56class CV2:
57 def __init__(self):
58 self.openFile()
59 self.openCamera()
60 def openFile(self):
61 self.img = cv2.imread("C\○○○\○○○\face.jpg")
62
63 #BGR→RGB
64 self.img=self.img[:,:,::-1]
65
66 def openCamera(self):
67 self.cap = cv2.VideoCapture(0)
68
69 def cameraFrame(self):
70 _,self.frame=self.cap.read()
71 self.frame_flip = cv2.flip(self.frame, 1)
72 self.frame_flip = self.frame_flip[:, :, ::-1]
73
74
75class Main:
76 def __init__(self):
77 self.gui=GUI()
78 self.gui.root.mainloop()
79
80
81if __name__=="__main__":
82 Main()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/11/11 02:10
退会済みユーザー
2018/11/11 02:10
2018/11/11 08:14