回答編集履歴
2
本文修正
test
CHANGED
@@ -8,24 +8,6 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
メモ.1 原因は割り切れていませんが、(threadのtimer等を使って)whileの無限ループ以外で画像を更新しないと、黒地に白い粒状の画像になってしまう。
|
14
|
-
|
15
|
-
メモ.2 Youhei様の指摘に合ったように、別のスレッドで画像を更新しないと、windowをドラッグしたり、windowのフォーカスを別のwindowに移動させるだけでソフトがクラッシュする
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
Youhei様のご指摘を加味するとこんなイメージです。
|
20
|
-
|
21
|
-
とりあえず動く形になったので自己解決でベストアンサーで閉めたいと思います。
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
ご協力ありがとうございました。
|
26
|
-
|
27
|
-
|
28
|
-
|
29
11
|
```Python
|
30
12
|
|
31
13
|
import wx
|
1
コードの全面修正
test
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
5/19朝追記
|
2
|
+
|
3
|
+
GUIを落としてからまだ黒窓が残ってしまっています
|
4
|
+
|
5
|
+
|
6
|
+
|
1
7
|
粗削りですが…
|
2
8
|
|
3
9
|
|
@@ -22,7 +28,7 @@
|
|
22
28
|
|
23
29
|
```Python
|
24
30
|
|
25
|
-
import wx
|
31
|
+
import wx
|
26
32
|
|
27
33
|
import os
|
28
34
|
|
@@ -32,16 +38,16 @@
|
|
32
38
|
|
33
39
|
import threading
|
34
40
|
|
35
|
-
import
|
41
|
+
import sys
|
36
42
|
|
37
43
|
|
38
44
|
|
39
45
|
GUI_IMAGE_X = 240
|
40
46
|
|
47
|
+
|
48
|
+
|
41
49
|
GUI_IMAGE_Y = 180
|
42
50
|
|
43
|
-
TIMER_MS = 500
|
44
|
-
|
45
51
|
CAMMODE = False
|
46
52
|
|
47
53
|
|
@@ -202,177 +208,185 @@
|
|
202
208
|
|
203
209
|
|
204
210
|
|
205
|
-
self.camera_thread = threading.Thread(target = self.Camera_mode)
|
206
|
-
|
207
|
-
self.camera_thread.start()
|
208
|
-
|
209
|
-
self.Camera_mode
|
210
|
-
|
211
|
-
#----------------------------------------------------------------------
|
211
|
+
#----------------------------------------------------------------------
|
212
|
-
|
212
|
+
|
213
|
-
def
|
213
|
+
def run(self):
|
214
|
+
|
215
|
+
print("RUN時のスレッド数 : " + str(threading.activeCount()))
|
214
216
|
|
215
217
|
global CAMMODE
|
216
218
|
|
219
|
+
|
220
|
+
|
221
|
+
while CAMMODE == True :
|
222
|
+
|
223
|
+
# Get camera information : "0" is device numer
|
224
|
+
|
225
|
+
cap = cv2.VideoCapture(0)
|
226
|
+
|
227
|
+
ret, frame = cap.read()
|
228
|
+
|
229
|
+
while ret == False:
|
230
|
+
|
231
|
+
# Get camera information : "0" is device numer
|
232
|
+
|
233
|
+
cap = cv2.VideoCapture(0)
|
234
|
+
|
235
|
+
ret, frame = cap.read()
|
236
|
+
|
237
|
+
self.panelText("ERROR: Camera is not working.")
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
while ret == True:
|
242
|
+
|
243
|
+
cv2.waitKey(1)
|
244
|
+
|
245
|
+
ret, frame = cap.read()
|
246
|
+
|
247
|
+
self.panelText("Camera mode")
|
248
|
+
|
249
|
+
# Load an image to entity from file
|
250
|
+
|
251
|
+
self.panelImage(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
# 複数スレッド化がうまくいていない
|
256
|
+
|
257
|
+
if threading.activeCount() == 1:
|
258
|
+
|
217
|
-
|
259
|
+
sys.exit("うまくいっていない")
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
########################################################################
|
264
|
+
|
265
|
+
class DnDFrame(wx.Frame):
|
266
|
+
|
267
|
+
#----------------------------------------------------------------------
|
268
|
+
|
269
|
+
def __init__(self):
|
270
|
+
|
271
|
+
self.window = wx.Frame.__init__(self, parent=None, title="GUI test", style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
self.InitMenu()
|
276
|
+
|
277
|
+
self.panel = DnDPanel(self)
|
278
|
+
|
279
|
+
self.Fit()
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
self.cc = CameraControl(self)
|
284
|
+
|
285
|
+
self.cc.setDaemon(True)
|
286
|
+
|
287
|
+
self.cc.start()
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
self.Show()
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
#----------------------------------------------------------------------
|
296
|
+
|
297
|
+
# Initialize
|
298
|
+
|
299
|
+
def InitMenu(self):
|
300
|
+
|
301
|
+
# Menubar
|
302
|
+
|
303
|
+
menubar = wx.MenuBar()
|
304
|
+
|
305
|
+
fileMenu = wx.Menu()
|
306
|
+
|
307
|
+
f_item2 = fileMenu.Append(wx.ID_ANY, '&Camera(C)', 'Load image from USB camera')
|
308
|
+
|
309
|
+
fileMenu.AppendSeparator()
|
310
|
+
|
311
|
+
f_item3 = fileMenu.Append(wx.ID_ANY, '&Quit(Q)', 'Quit application')
|
312
|
+
|
313
|
+
menubar.Append(fileMenu, '&File(F)')
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
self.SetMenuBar(menubar)
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
# Event call definition
|
322
|
+
|
323
|
+
self.Bind(wx.EVT_MENU, self.OnCamera, f_item2)
|
324
|
+
|
325
|
+
self.Bind(wx.EVT_MENU, self.OnQuit, f_item3)
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
# Frame definition
|
330
|
+
|
331
|
+
self.Centre()
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
# Status bar
|
336
|
+
|
337
|
+
self.CreateStatusBar()
|
338
|
+
|
339
|
+
self.setstatusbarTXT("")
|
340
|
+
|
341
|
+
|
342
|
+
|
343
|
+
self.Show(True)
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
#----------------------------------------------------------------------
|
348
|
+
|
349
|
+
# Event hander: set status text
|
350
|
+
|
351
|
+
def setstatusbarTXT(self, msg):
|
352
|
+
|
353
|
+
self.SetStatusText(msg)
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
#----------------------------------------------------------------------
|
358
|
+
|
359
|
+
# Event: Quit
|
360
|
+
|
361
|
+
def OnQuit(self,event):
|
362
|
+
|
363
|
+
#self.Destroy()
|
364
|
+
|
365
|
+
sys.exit()
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
#----------------------------------------------------------------------
|
370
|
+
|
371
|
+
# Event: Camera
|
372
|
+
|
373
|
+
def OnCamera(self, event):
|
374
|
+
|
375
|
+
global CAMMODE
|
218
376
|
|
219
377
|
CAMMODE = True
|
220
378
|
|
221
|
-
|
222
|
-
|
223
|
-
while CAMMODE == True :
|
224
|
-
|
225
|
-
# Get camera information : "0" is device numer
|
226
|
-
|
227
|
-
cap = cv2.VideoCapture(0)
|
228
|
-
|
229
|
-
ret, frame = cap.read()
|
230
|
-
|
231
|
-
while ret == False:
|
232
|
-
|
233
|
-
#print("SLEEP")
|
234
|
-
|
235
|
-
time.sleep(TIMER_MS/1000)
|
236
|
-
|
237
|
-
# Get camera information : "0" is device numer
|
238
|
-
|
239
|
-
cap = cv2.VideoCapture(0)
|
240
|
-
|
241
|
-
ret, frame = cap.read()
|
242
|
-
|
243
|
-
self.panelText("ERROR: Camera is not working.")
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
while ret == True:
|
248
|
-
|
249
|
-
#cv2.imshow("frame",frame )
|
250
|
-
|
251
|
-
cv2.waitKey(1)
|
252
|
-
|
253
|
-
ret, frame = cap.read()
|
254
|
-
|
255
|
-
|
379
|
+
self.panel.updateText("Camera mode")
|
256
|
-
|
257
|
-
|
380
|
+
|
258
|
-
|
259
|
-
|
381
|
+
self.setstatusbarTXT("Camera mode")
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
########################################################################
|
264
|
-
|
265
|
-
class DnDFrame(wx.Frame):
|
266
|
-
|
267
|
-
#----------------------------------------------------------------------
|
268
|
-
|
269
|
-
def __init__(self):
|
270
|
-
|
271
|
-
self.window = wx.Frame.__init__(self, parent=None, title="GUI test", style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
self.InitMenu()
|
276
|
-
|
277
|
-
self.panel = DnDPanel(self)
|
278
|
-
|
279
|
-
self.Fit()
|
280
382
|
|
281
383
|
|
282
384
|
|
283
385
|
self.cc = CameraControl(self)
|
284
386
|
|
285
|
-
|
286
|
-
|
287
|
-
self.Show()
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
#----------------------------------------------------------------------
|
292
|
-
|
293
|
-
# Initialize
|
294
|
-
|
295
|
-
def InitMenu(self):
|
296
|
-
|
297
|
-
# Menubar
|
298
|
-
|
299
|
-
menubar = wx.MenuBar()
|
300
|
-
|
301
|
-
fileMenu = wx.Menu()
|
302
|
-
|
303
|
-
f_item2 = fileMenu.Append(wx.ID_ANY, '&Camera(C)', 'Load image from USB camera')
|
304
|
-
|
305
|
-
fileMenu.AppendSeparator()
|
306
|
-
|
307
|
-
f_item3 = fileMenu.Append(wx.ID_ANY, '&Quit(Q)', 'Quit application')
|
308
|
-
|
309
|
-
menubar.Append(fileMenu, '&File(F)')
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
self.SetMenuBar(menubar)
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
# Event call definition
|
318
|
-
|
319
|
-
self.Bind(wx.EVT_MENU, self.OnCamera, f_item2)
|
320
|
-
|
321
|
-
self.Bind(wx.EVT_MENU, self.OnQuit, f_item3)
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
# Frame definition
|
326
|
-
|
327
|
-
self.Centre()
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
# Status bar
|
332
|
-
|
333
|
-
self.CreateStatusBar()
|
334
|
-
|
335
|
-
self.setstatusbarTXT("")
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
self.
|
387
|
+
self.cc.setDaemon(True)
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
388
|
+
|
344
|
-
|
345
|
-
# Event hander: set status text
|
346
|
-
|
347
|
-
def setstatusbarTXT(self, msg):
|
348
|
-
|
349
|
-
self.SetStatusText(msg)
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
#----------------------------------------------------------------------
|
354
|
-
|
355
|
-
# Event: Quit
|
356
|
-
|
357
|
-
def OnQuit(self,event):
|
358
|
-
|
359
|
-
self.
|
389
|
+
self.cc.start()
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
#----------------------------------------------------------------------
|
364
|
-
|
365
|
-
# Event: Camera
|
366
|
-
|
367
|
-
def OnCamera(self, event):
|
368
|
-
|
369
|
-
self.panel.updateText("Camera mode")
|
370
|
-
|
371
|
-
self.setstatusbarTXT("Camera mode")
|
372
|
-
|
373
|
-
CAMMODE = True
|
374
|
-
|
375
|
-
self.cc.Camera_mode()
|
376
390
|
|
377
391
|
|
378
392
|
|
@@ -380,34 +394,6 @@
|
|
380
394
|
|
381
395
|
if __name__ == "__main__":
|
382
396
|
|
383
|
-
#
|
384
|
-
|
385
|
-
# cap = cv2.VideoCapture(0)
|
386
|
-
|
387
|
-
# ret, frame = cap.read()
|
388
|
-
|
389
|
-
# while ret == False:
|
390
|
-
|
391
|
-
# print("SLEEP")
|
392
|
-
|
393
|
-
# time.sleep(TIMER_MS/1000)
|
394
|
-
|
395
|
-
# cap = cv2.VideoCapture(0)
|
396
|
-
|
397
|
-
# ret, frame = cap.read()
|
398
|
-
|
399
|
-
#
|
400
|
-
|
401
|
-
# while ret == True:
|
402
|
-
|
403
|
-
# cv2.imshow("frame",frame )
|
404
|
-
|
405
|
-
# cv2.waitKey(1)
|
406
|
-
|
407
|
-
# ret, frame = cap.read()
|
408
|
-
|
409
|
-
|
410
|
-
|
411
397
|
app = wx.App(False)
|
412
398
|
|
413
399
|
frame = DnDFrame()
|
@@ -416,4 +402,6 @@
|
|
416
402
|
|
417
403
|
|
418
404
|
|
405
|
+
|
406
|
+
|
419
407
|
```
|