質問編集履歴

1

テストしたマルチスレッドのコードを追記しました。

2021/08/09 23:34

投稿

ysyk77
ysyk77

スコア4

test CHANGED
File without changes
test CHANGED
@@ -145,3 +145,215 @@
145
145
  ### 試したこと
146
146
 
147
147
  マルチスレッドをWEB上の情報を参考に試しましたが、うまく行きませんでした。
148
+
149
+ マルチスレッド化したときのコードです。
150
+
151
+ cameraボタンを押しcaptureを開始すると、黒画面が出力し、ハングアップしてしまいます。
152
+
153
+ ```python
154
+
155
+
156
+
157
+ #!/usr/bin/python3
158
+
159
+ # -*- coding: utf8 -*-
160
+
161
+ import tkinter
162
+
163
+ import cv2
164
+
165
+ import threading
166
+
167
+ import time
168
+
169
+
170
+
171
+ stopflag = 0
172
+
173
+
174
+
175
+ # Thread Test → OK
176
+
177
+ class ThreadJob(threading.Thread):
178
+
179
+ def __init__(self, v=""):
180
+
181
+ threading.Thread.__init__(self)
182
+
183
+
184
+
185
+ def run(self):
186
+
187
+ global stopflag
188
+
189
+
190
+
191
+ while not(stopflag):
192
+
193
+ print('hello,stop=',stopflag)
194
+
195
+ time.sleep(1)
196
+
197
+ if stopflag != 0:
198
+
199
+ break
200
+
201
+
202
+
203
+ # camera capture
204
+
205
+ class ThreadJob2(threading.Thread):
206
+
207
+ def __init__(self, v=""):
208
+
209
+ threading.Thread.__init__(self)
210
+
211
+
212
+
213
+ def run(self):
214
+
215
+ global stopflag
216
+
217
+ cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
218
+
219
+ if cap.isOpened() == False:
220
+
221
+ return
222
+
223
+
224
+
225
+ while not(stopflag):
226
+
227
+ ret, frame = cap.read()
228
+
229
+ name = "video"
230
+
231
+ cv2.namedWindow(name, cv2.WINDOW_NORMAL)
232
+
233
+ cv2.imshow(name,frame)
234
+
235
+ if stopflag != 0:
236
+
237
+ break
238
+
239
+
240
+
241
+ cap.release()
242
+
243
+ cv2.destroyAllWindows()
244
+
245
+
246
+
247
+
248
+
249
+ class Application(tkinter.Frame):
250
+
251
+
252
+
253
+ def __init__(self, master=None):
254
+
255
+ super().__init__(master)
256
+
257
+ self.pack()
258
+
259
+ self.create_widgets()
260
+
261
+
262
+
263
+ def create_widgets(self):
264
+
265
+ # capture
266
+
267
+ self.button = tkinter.Button(self)
268
+
269
+ self.button["text"] = u"Camera"
270
+
271
+ self.button["command"] = self.button_func
272
+
273
+ self.button["width"] = 20
274
+
275
+ self.button.pack()
276
+
277
+
278
+
279
+ # stop
280
+
281
+ self.btnStop = tkinter.Button(self)
282
+
283
+ self.btnStop["text"] = u"Stop"
284
+
285
+ self.btnStop["command"] = self.button_Stop
286
+
287
+ self.btnStop["width"] = 20
288
+
289
+ self.btnStop.pack()
290
+
291
+
292
+
293
+ # Quit
294
+
295
+ self.quit = tkinter.Button(self)
296
+
297
+ self.quit["text"] = u"Quit"
298
+
299
+ self.quit["width"] = 20
300
+
301
+ self.quit["command"] = self.master.destroy
302
+
303
+ self.quit.pack()
304
+
305
+
306
+
307
+ # Callbacks
308
+
309
+ def button_func(self):
310
+
311
+ global stopflag
312
+
313
+ stopflag = 0
314
+
315
+ #t = ThreadJob() # スレッド確認OK
316
+
317
+ t = ThreadJob2() # capture
318
+
319
+ t.start() # スレッドを実行
320
+
321
+
322
+
323
+ def button_Stop(self):
324
+
325
+ global stopflag
326
+
327
+ print('stop')
328
+
329
+ stopflag = 1
330
+
331
+
332
+
333
+
334
+
335
+ # メイン関数
336
+
337
+ def main():
338
+
339
+ # Windowの生成
340
+
341
+ root = tkinter.Tk()
342
+
343
+ root.geometry("200x100")
344
+
345
+ root.title(u"basic1")
346
+
347
+ app = Application(master=root)
348
+
349
+ app.mainloop()
350
+
351
+
352
+
353
+ if __name__ == '__main__':
354
+
355
+ main()
356
+
357
+
358
+
359
+ ```