質問編集履歴

2

Markdown記法の修正をしました

2020/03/09 03:35

投稿

mr_nurseboy
mr_nurseboy

スコア14

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  bootKeyとrecKeyはtkinterのボタンで変わるようにしています。
16
16
 
17
+
18
+
17
19
  ```python
18
20
 
19
21
  def init():
@@ -82,152 +84,154 @@
82
84
 
83
85
  普通に考えるとImageTkとCV2.writeが干渉しているように思いますがいかがでしょうか?
84
86
 
87
+
88
+
89
+
90
+
91
+ ###解決したコード
92
+
93
+ ```python
94
+
95
+ import tkinter as tk
96
+
97
+ import threading
98
+
99
+ import cv2
100
+
101
+ from PIL import Image,ImageTk
102
+
103
+
104
+
105
+ bootKey=True
106
+
107
+ recKey=False
108
+
109
+
110
+
111
+ def CV_init():
112
+
113
+ global cap
114
+
115
+ global w
116
+
117
+ global h
118
+
119
+ global out
120
+
121
+ cap = cv2.VideoCapture(1)
122
+
123
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
124
+
125
+ w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
126
+
127
+ h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
128
+
129
+ print(w)
130
+
131
+ print(h)
132
+
133
+ rate_wh = 240 / w # width=240
134
+
135
+ w = int(w * rate_wh)
136
+
137
+ h = int(h * rate_wh)
138
+
139
+ print(w)
140
+
141
+ print(h)
142
+
143
+ fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
144
+
145
+ out = cv2.VideoWriter('output.mp4', fourcc, fps, (w, h))
146
+
147
+ print("cv_init_fin")
148
+
149
+
150
+
151
+ def CV_loop():
152
+
153
+ global frame
154
+
155
+ global im
156
+
157
+ if bootKey:
158
+
159
+ ret, frame = cap.read()
160
+
161
+ frame = cv2.resize(frame, dsize=(w, h))
162
+
163
+ if recKey:
164
+
165
+ out.write(frame)
166
+
167
+ im = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
168
+
169
+ canvas.create_image(0, 0, image=im)
170
+
171
+ root.after(1,CV_loop)
172
+
173
+ else:
174
+
175
+ print("exit")
176
+
177
+ CV_exit()
178
+
179
+
180
+
181
+ def CV_exit():
182
+
183
+ cap.release()
184
+
185
+ out.release()
186
+
187
+ root.quit()
188
+
189
+
190
+
191
+ def recording():
192
+
193
+ global recKey
194
+
195
+ if recKey:
196
+
197
+ recKey=False
198
+
199
+ else:
200
+
201
+ recKey=True
202
+
203
+
204
+
205
+ def finish():
206
+
207
+ global bootKey
208
+
209
+ global recKey
210
+
211
+ bootKey=False
212
+
213
+ recKey=False
214
+
215
+
216
+
217
+ root=tk.Tk()
218
+
219
+ canvas=tk.Canvas(root)
220
+
221
+ canvas.pack()
222
+
223
+ btn_rec=tk.Button(root,text="rec",command=recording)
224
+
225
+ btn_rec.pack()
226
+
227
+ btn_fin=tk.Button(root,text="fin",command=finish)
228
+
229
+ btn_fin.pack()
230
+
231
+ CV_init()
232
+
233
+ threading.Thread(target=CV_loop).start()
234
+
235
+ root.mainloop()
236
+
85
237
  ```
86
-
87
- ###解決したコード
88
-
89
- ```python
90
-
91
- import tkinter as tk
92
-
93
- import threading
94
-
95
- import cv2
96
-
97
- from PIL import Image,ImageTk
98
-
99
-
100
-
101
- bootKey=True
102
-
103
- recKey=False
104
-
105
-
106
-
107
- def CV_init():
108
-
109
- global cap
110
-
111
- global w
112
-
113
- global h
114
-
115
- global out
116
-
117
- cap = cv2.VideoCapture(1)
118
-
119
- fps = int(cap.get(cv2.CAP_PROP_FPS))
120
-
121
- w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
122
-
123
- h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
124
-
125
- print(w)
126
-
127
- print(h)
128
-
129
- rate_wh = 240 / w # width=240
130
-
131
- w = int(w * rate_wh)
132
-
133
- h = int(h * rate_wh)
134
-
135
- print(w)
136
-
137
- print(h)
138
-
139
- fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
140
-
141
- out = cv2.VideoWriter('output.mp4', fourcc, fps, (w, h))
142
-
143
- print("cv_init_fin")
144
-
145
-
146
-
147
- def CV_loop():
148
-
149
- global frame
150
-
151
- global im
152
-
153
- if bootKey:
154
-
155
- ret, frame = cap.read()
156
-
157
- frame = cv2.resize(frame, dsize=(w, h))
158
-
159
- if recKey:
160
-
161
- out.write(frame)
162
-
163
- im = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
164
-
165
- canvas.create_image(0, 0, image=im)
166
-
167
- root.after(1,CV_loop)
168
-
169
- else:
170
-
171
- print("exit")
172
-
173
- CV_exit()
174
-
175
-
176
-
177
- def CV_exit():
178
-
179
- cap.release()
180
-
181
- out.release()
182
-
183
- root.quit()
184
-
185
-
186
-
187
- def recording():
188
-
189
- global recKey
190
-
191
- if recKey:
192
-
193
- recKey=False
194
-
195
- else:
196
-
197
- recKey=True
198
-
199
-
200
-
201
- def finish():
202
-
203
- global bootKey
204
-
205
- global recKey
206
-
207
- bootKey=False
208
-
209
- recKey=False
210
-
211
-
212
-
213
- root=tk.Tk()
214
-
215
- canvas=tk.Canvas(root)
216
-
217
- canvas.pack()
218
-
219
- btn_rec=tk.Button(root,text="rec",command=recording)
220
-
221
- btn_rec.pack()
222
-
223
- btn_fin=tk.Button(root,text="fin",command=finish)
224
-
225
- btn_fin.pack()
226
-
227
- CV_init()
228
-
229
- threading.Thread(target=CV_loop).start()
230
-
231
- root.mainloop()
232
-
233
- ```

1

最終的なコードを追加しました

2020/03/09 03:35

投稿

mr_nurseboy
mr_nurseboy

スコア14

test CHANGED
File without changes
test CHANGED
@@ -14,9 +14,97 @@
14
14
 
15
15
  bootKeyとrecKeyはtkinterのボタンで変わるようにしています。
16
16
 
17
+ ```python
18
+
19
+ def init():
20
+
21
+ global cap
22
+
23
+ global w
24
+
25
+ global h
26
+
27
+ global out
28
+
29
+ cap = cv2.VideoCapture(1)
30
+
31
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
32
+
33
+ w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
34
+
35
+ h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
36
+
37
+ fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
38
+
39
+ out = cv2.VideoWriter('output.mp4', fourcc, fps, (w, h))
40
+
41
+
42
+
43
+ def record():
44
+
45
+ global frame
46
+
47
+ if bootKey:
48
+
49
+ ret, frame = cap.read()
50
+
51
+ frame = cv2.resize(frame, dsize=(w, h))
52
+
53
+ print(w)
54
+
55
+ print(h)
56
+
57
+ if recKey:
58
+
59
+ out.write(frame)
60
+
61
+ im = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
62
+
63
+ print(3)
64
+
65
+ canvas.create_image(w, h, image=im)
66
+
67
+ root.after(1,record)
68
+
69
+ else:
70
+
71
+ print("exit")
72
+
17
73
  ```
18
74
 
75
+
76
+
77
+ ### 試したこと
78
+
79
+
80
+
81
+ im=ImageTk~~とcanvas.create_Imageをコメントアウトすると録画できることは確認しました。
82
+
83
+ 普通に考えるとImageTkとCV2.writeが干渉しているように思いますがいかがでしょうか?
84
+
85
+ ```
86
+
87
+ ###解決したコード
88
+
89
+ ```python
90
+
91
+ import tkinter as tk
92
+
93
+ import threading
94
+
95
+ import cv2
96
+
97
+ from PIL import Image,ImageTk
98
+
99
+
100
+
101
+ bootKey=True
102
+
103
+ recKey=False
104
+
105
+
106
+
19
- def init():
107
+ def CV_init():
20
108
 
21
109
  global cap
22
110
 
@@ -34,54 +122,112 @@
34
122
 
35
123
  h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
36
124
 
125
+ print(w)
126
+
127
+ print(h)
128
+
129
+ rate_wh = 240 / w # width=240
130
+
131
+ w = int(w * rate_wh)
132
+
133
+ h = int(h * rate_wh)
134
+
135
+ print(w)
136
+
137
+ print(h)
138
+
37
139
  fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
38
140
 
39
141
  out = cv2.VideoWriter('output.mp4', fourcc, fps, (w, h))
40
142
 
41
-
143
+ print("cv_init_fin")
42
-
144
+
145
+
146
+
43
- def record():
147
+ def CV_loop():
44
148
 
45
149
  global frame
46
150
 
151
+ global im
152
+
47
153
  if bootKey:
48
154
 
49
155
  ret, frame = cap.read()
50
156
 
51
157
  frame = cv2.resize(frame, dsize=(w, h))
52
158
 
53
- print(w)
54
-
55
- print(h)
56
-
57
159
  if recKey:
58
160
 
59
161
  out.write(frame)
60
162
 
61
163
  im = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)))
62
164
 
63
- print(3)
64
-
65
- canvas.create_image(w, h, image=im)
165
+ canvas.create_image(0, 0, image=im)
66
-
166
+
67
- root.after(1,record)
167
+ root.after(1,CV_loop)
68
168
 
69
169
  else:
70
170
 
71
171
  print("exit")
72
172
 
73
-
74
-
75
- ```python
173
+ CV_exit()
174
+
175
+
176
+
177
+ def CV_exit():
178
+
179
+ cap.release()
180
+
181
+ out.release()
182
+
183
+ root.quit()
184
+
185
+
186
+
187
+ def recording():
188
+
189
+ global recKey
190
+
191
+ if recKey:
192
+
193
+ recKey=False
194
+
195
+ else:
196
+
197
+ recKey=True
198
+
199
+
200
+
201
+ def finish():
202
+
203
+ global bootKey
204
+
205
+ global recKey
206
+
207
+ bootKey=False
208
+
209
+ recKey=False
210
+
211
+
212
+
213
+ root=tk.Tk()
214
+
215
+ canvas=tk.Canvas(root)
216
+
217
+ canvas.pack()
218
+
219
+ btn_rec=tk.Button(root,text="rec",command=recording)
220
+
221
+ btn_rec.pack()
222
+
223
+ btn_fin=tk.Button(root,text="fin",command=finish)
224
+
225
+ btn_fin.pack()
226
+
227
+ CV_init()
228
+
229
+ threading.Thread(target=CV_loop).start()
230
+
231
+ root.mainloop()
76
232
 
77
233
  ```
78
-
79
-
80
-
81
- ### 試したこと
82
-
83
-
84
-
85
- im=ImageTk~~とcanvas.create_Imageをコメントアウトすると録画できることは確認しました。
86
-
87
- 普通に考えるとImageTkとCV2.writeが干渉しているように思いますがいかがでしょうか?