質問編集履歴
6
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -236,15 +236,9 @@
|
|
236
236
|
|
237
237
|
# 追記2
|
238
238
|
|
239
|
+
|
240
|
+
|
239
|
-
|
241
|
+
teamikl様からのコメントの指摘をもとに変更
|
240
|
-
|
241
|
-
- リサイズ時に消えてしまう。
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
![イメージ説明](707bcb188834dae0c40687a45a089d2a.jpeg)
|
246
|
-
|
247
|
-
|
248
242
|
|
249
243
|
```Python
|
250
244
|
|
@@ -254,6 +248,10 @@
|
|
254
248
|
|
255
249
|
import cv2
|
256
250
|
|
251
|
+
import queue
|
252
|
+
|
253
|
+
import threading
|
254
|
+
|
257
255
|
import tkinter as tk
|
258
256
|
|
259
257
|
|
@@ -272,10 +270,18 @@
|
|
272
270
|
|
273
271
|
|
274
272
|
|
273
|
+
self.data_queue = queue.Queue()
|
274
|
+
|
275
|
+
|
276
|
+
|
275
277
|
self.win = win
|
276
278
|
|
277
279
|
self.win.title("Camera")
|
278
280
|
|
281
|
+
self.thread = threading.Thread(target=self.get_queue)
|
282
|
+
|
283
|
+
self.thread.start()
|
284
|
+
|
279
285
|
self.disp_center(self.win, 720, 480)
|
280
286
|
|
281
287
|
self.win.protocol("WM_DELETE_WINDOW", self.close)
|
@@ -288,6 +294,8 @@
|
|
288
294
|
|
289
295
|
|
290
296
|
|
297
|
+
|
298
|
+
|
291
299
|
def disp_center(self, win, wx=400, wy=300):
|
292
300
|
|
293
301
|
#win.resizable(0, 0)
|
@@ -304,14 +312,22 @@
|
|
304
312
|
|
305
313
|
|
306
314
|
|
315
|
+
|
316
|
+
|
307
317
|
def close(self):
|
308
318
|
|
319
|
+
self.data_queue.put(["close", None])
|
320
|
+
|
321
|
+
self.thread.join()
|
322
|
+
|
309
323
|
self.win.destroy()
|
310
324
|
|
311
325
|
self.vcap.release()
|
312
326
|
|
313
327
|
|
314
328
|
|
329
|
+
|
330
|
+
|
315
331
|
def create_view(self):
|
316
332
|
|
317
333
|
self.win.rowconfigure(0, weight=1)
|
@@ -336,17 +352,21 @@
|
|
336
352
|
|
337
353
|
|
338
354
|
|
339
|
-
_, fr
|
355
|
+
_, frm = self.vcap.read()
|
340
|
-
|
356
|
+
|
341
|
-
frame = cv2.cvtColor(fr
|
357
|
+
self.frame = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)
|
342
|
-
|
358
|
+
|
343
|
-
self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
|
359
|
+
self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame))
|
360
|
+
|
361
|
+
#self.canvas.update()
|
344
362
|
|
345
363
|
cv_w = self.canvas.winfo_reqwidth()
|
346
364
|
|
347
365
|
cv_h = self.canvas.winfo_reqheight()
|
348
366
|
|
349
|
-
self.img_id = self.canvas.create_image(cv_w // 2, cv_h // 2, image=self.photo)
|
367
|
+
self.img_id = self.canvas.create_image(cv_w // 2, cv_h // 2, image=self.photo, anchor="center")
|
368
|
+
|
369
|
+
|
350
370
|
|
351
371
|
|
352
372
|
|
@@ -402,15 +422,7 @@
|
|
402
422
|
|
403
423
|
def pushed_button(self):
|
404
424
|
|
405
|
-
_, frame = self.vcap.read()
|
406
|
-
|
407
|
-
f
|
425
|
+
self.data_queue.put(["snapshot", None])
|
408
|
-
|
409
|
-
print(frame)
|
410
|
-
|
411
|
-
cv2.imwrite( "Desktop/frame-" + datetime.now().strftime("%Y-%d-%m-%H-%M-%S") + ".jpg",
|
412
|
-
|
413
|
-
cv2.cvtColor( frame, cv2.COLOR_BGR2RGB ) )
|
414
426
|
|
415
427
|
|
416
428
|
|
@@ -418,11 +430,11 @@
|
|
418
430
|
|
419
431
|
def update_by_timer(self):
|
420
432
|
|
421
|
-
_, fr
|
433
|
+
_, frm = self.vcap.read()
|
422
|
-
|
434
|
+
|
423
|
-
frame = cv2.cvtColor(fr
|
435
|
+
self.frame = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)
|
424
|
-
|
436
|
+
|
425
|
-
self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
|
437
|
+
self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame))
|
426
438
|
|
427
439
|
self.canvas.itemconfig(self.img_id, image=self.photo)
|
428
440
|
|
@@ -434,19 +446,17 @@
|
|
434
446
|
|
435
447
|
def update_by_resize(self, e):
|
436
448
|
|
437
|
-
_, frame = self.vcap.read()
|
438
|
-
|
439
|
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
440
|
-
|
441
|
-
self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
|
442
|
-
|
443
|
-
self.canvas.itemconfig(self.img_id, image=self.photo)
|
449
|
+
self.canvas.itemconfig(self.img_id, image=self.photo, anchor="center")
|
444
|
-
|
450
|
+
|
445
|
-
cv_w = self.canvas.winfo_
|
451
|
+
cv_w = self.canvas.winfo_width()
|
446
|
-
|
452
|
+
|
447
|
-
cv_h = self.canvas.winfo_
|
453
|
+
cv_h = self.canvas.winfo_height()
|
454
|
+
|
448
|
-
|
455
|
+
print(datetime.now())
|
456
|
+
|
457
|
+
print(cv_w, cv_h)
|
458
|
+
|
449
|
-
self.canvas.
|
459
|
+
self.canvas.coords(self.img_id, cv_w // 2, cv_h // 2)
|
450
460
|
|
451
461
|
|
452
462
|
|
@@ -460,6 +470,40 @@
|
|
460
470
|
|
461
471
|
|
462
472
|
|
473
|
+
def get_queue(self):
|
474
|
+
|
475
|
+
while True:
|
476
|
+
|
477
|
+
msg, data = self.data_queue.get()
|
478
|
+
|
479
|
+
if msg == "snapshot":
|
480
|
+
|
481
|
+
self.snapshot()
|
482
|
+
|
483
|
+
elif msg == "close":
|
484
|
+
|
485
|
+
break
|
486
|
+
|
487
|
+
else:
|
488
|
+
|
489
|
+
print("Undefined msg")
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
def snapshot(self):
|
494
|
+
|
495
|
+
filename = "C:/Users/user01/Desktop/img/frame-" + datetime.now().strftime("%Y-%d-%m-%H-%M-%S_%f") + ".jpg"
|
496
|
+
|
497
|
+
cv2.imwrite( filename,
|
498
|
+
|
499
|
+
cv2.cvtColor( self.frame, cv2.COLOR_BGR2RGB ) )
|
500
|
+
|
501
|
+
print(filename)
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
|
506
|
+
|
463
507
|
def main():
|
464
508
|
|
465
509
|
win = tk.Tk()
|
5
文法修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -236,7 +236,11 @@
|
|
236
236
|
|
237
237
|
# 追記2
|
238
238
|
|
239
|
+
- ウィンドウが720x480の時、映像が右寄りに描画
|
240
|
+
|
239
|
-
|
241
|
+
- リサイズ時に消えてしまう。
|
242
|
+
|
243
|
+
|
240
244
|
|
241
245
|
![イメージ説明](707bcb188834dae0c40687a45a089d2a.jpeg)
|
242
246
|
|
4
文法修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -236,7 +236,7 @@
|
|
236
236
|
|
237
237
|
# 追記2
|
238
238
|
|
239
|
-
映像が
|
239
|
+
映像が右寄りに描画、リサイズ時に消えてしまう。
|
240
240
|
|
241
241
|
![イメージ説明](707bcb188834dae0c40687a45a089d2a.jpeg)
|
242
242
|
|
3
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -231,3 +231,243 @@
|
|
231
231
|
映像が右下寄りに描画されてしまう。
|
232
232
|
|
233
233
|
![イメージ説明](844dd72ce6a4bdff692cee2bcd80abd5.jpeg)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
# 追記2
|
238
|
+
|
239
|
+
映像が左寄りに描画、リサイズ時に消えてしまう。
|
240
|
+
|
241
|
+
![イメージ説明](707bcb188834dae0c40687a45a089d2a.jpeg)
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
```Python
|
246
|
+
|
247
|
+
from datetime import datetime
|
248
|
+
|
249
|
+
from PIL import Image, ImageTk
|
250
|
+
|
251
|
+
import cv2
|
252
|
+
|
253
|
+
import tkinter as tk
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
class App:
|
258
|
+
|
259
|
+
def __init__(self, win):
|
260
|
+
|
261
|
+
video_source = 0
|
262
|
+
|
263
|
+
self.vcap = cv2.VideoCapture(video_source)
|
264
|
+
|
265
|
+
self.width = self.vcap.get(cv2.CAP_PROP_FRAME_WIDTH)
|
266
|
+
|
267
|
+
self.height = self.vcap.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
self.win = win
|
272
|
+
|
273
|
+
self.win.title("Camera")
|
274
|
+
|
275
|
+
self.disp_center(self.win, 720, 480)
|
276
|
+
|
277
|
+
self.win.protocol("WM_DELETE_WINDOW", self.close)
|
278
|
+
|
279
|
+
self.create_view()
|
280
|
+
|
281
|
+
self.bind_event()
|
282
|
+
|
283
|
+
self.update_by_timer()
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
def disp_center(self, win, wx=400, wy=300):
|
288
|
+
|
289
|
+
#win.resizable(0, 0)
|
290
|
+
|
291
|
+
dx = win.winfo_screenwidth()
|
292
|
+
|
293
|
+
dy = win.winfo_screenheight()
|
294
|
+
|
295
|
+
win_size = str(wx) + "x" + str(wy) + \
|
296
|
+
|
297
|
+
"+" + str(int(dx/2 - wx/2)) + "+" + str(int(dy/2 - wy/2))
|
298
|
+
|
299
|
+
win.geometry(win_size)
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
def close(self):
|
304
|
+
|
305
|
+
self.win.destroy()
|
306
|
+
|
307
|
+
self.vcap.release()
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
def create_view(self):
|
312
|
+
|
313
|
+
self.win.rowconfigure(0, weight=1)
|
314
|
+
|
315
|
+
self.win.columnconfigure(0, weight=1)
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
self.frame = tk.Frame(self.win, relief="sunken", bd=1)
|
320
|
+
|
321
|
+
self.frame.grid(row=0, column=0, sticky="nsew")
|
322
|
+
|
323
|
+
self.frame.rowconfigure(0, weight=1)
|
324
|
+
|
325
|
+
self.frame.columnconfigure(0, weight=1)
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
self.canvas = tk.Canvas(self.frame)
|
330
|
+
|
331
|
+
self.canvas.grid(row=0, column=0, sticky="nsew", padx=10, pady=10)
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
_, frame = self.vcap.read()
|
336
|
+
|
337
|
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
338
|
+
|
339
|
+
self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
|
340
|
+
|
341
|
+
cv_w = self.canvas.winfo_reqwidth()
|
342
|
+
|
343
|
+
cv_h = self.canvas.winfo_reqheight()
|
344
|
+
|
345
|
+
self.img_id = self.canvas.create_image(cv_w // 2, cv_h // 2, image=self.photo)
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
"""
|
350
|
+
|
351
|
+
self.canvas.update()
|
352
|
+
|
353
|
+
cv_w = self.canvas.winfo_width()
|
354
|
+
|
355
|
+
cv_h = self.canvas.winfo_height()
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
rec_id = self.canvas.create_rectangle(0, 0, cv_w, cv_h, fill="black")
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
# 長方形の座標取得
|
366
|
+
|
367
|
+
rec_pos = self.canvas.coords(rec_id)
|
368
|
+
|
369
|
+
# テキストを描画(位置は適当)
|
370
|
+
|
371
|
+
text_id = self.canvas.create_text(0, 0, text="NO SIGNAL", font=("", 20), fill="white")
|
372
|
+
|
373
|
+
# テキストのサイズ取得
|
374
|
+
|
375
|
+
text_size = self.canvas.bbox(text_id)
|
376
|
+
|
377
|
+
# テキストの座標移動
|
378
|
+
|
379
|
+
rc_x = rec_pos[2] / 2
|
380
|
+
|
381
|
+
rc_y = rec_pos[2] / 3
|
382
|
+
|
383
|
+
tc_y = text_size[3] / 2
|
384
|
+
|
385
|
+
self.canvas.move(text_id, rc_x - (rec_pos[0] / 2), rc_y - tc_y)
|
386
|
+
|
387
|
+
"""
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
self.button = tk.Button(self.win, text="Snapshot", command=self.pushed_button)
|
392
|
+
|
393
|
+
self.button.grid(row=1, column=0, sticky="", pady=10)
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
def pushed_button(self):
|
400
|
+
|
401
|
+
_, frame = self.vcap.read()
|
402
|
+
|
403
|
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
404
|
+
|
405
|
+
print(frame)
|
406
|
+
|
407
|
+
cv2.imwrite( "Desktop/frame-" + datetime.now().strftime("%Y-%d-%m-%H-%M-%S") + ".jpg",
|
408
|
+
|
409
|
+
cv2.cvtColor( frame, cv2.COLOR_BGR2RGB ) )
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
def update_by_timer(self):
|
416
|
+
|
417
|
+
_, frame = self.vcap.read()
|
418
|
+
|
419
|
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
420
|
+
|
421
|
+
self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
|
422
|
+
|
423
|
+
self.canvas.itemconfig(self.img_id, image=self.photo)
|
424
|
+
|
425
|
+
self.canvas.after(15, self.update_by_timer)
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
def update_by_resize(self, e):
|
432
|
+
|
433
|
+
_, frame = self.vcap.read()
|
434
|
+
|
435
|
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
436
|
+
|
437
|
+
self.photo = ImageTk.PhotoImage(image = Image.fromarray(frame))
|
438
|
+
|
439
|
+
self.canvas.itemconfig(self.img_id, image=self.photo)
|
440
|
+
|
441
|
+
cv_w = self.canvas.winfo_reqwidth()
|
442
|
+
|
443
|
+
cv_h = self.canvas.winfo_reqheight()
|
444
|
+
|
445
|
+
self.canvas.move(self.img_id, cv_w // 2, cv_h // 2)
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
def bind_event(self):
|
452
|
+
|
453
|
+
self.canvas.bind("<Configure>", self.update_by_resize)
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
def main():
|
460
|
+
|
461
|
+
win = tk.Tk()
|
462
|
+
|
463
|
+
App(win)
|
464
|
+
|
465
|
+
win.mainloop()
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
if __name__ == "__main__":
|
470
|
+
|
471
|
+
main()
|
472
|
+
|
473
|
+
```
|
2
画像追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -223,3 +223,11 @@
|
|
223
223
|
|
224
224
|
|
225
225
|
- [パソコンに接続したカメラの映像をGUIに表示する。](https://qiita.com/kotai2003/items/3d31528d56059c848458)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
# 追記
|
230
|
+
|
231
|
+
映像が右下寄りに描画されてしまう。
|
232
|
+
|
233
|
+
![イメージ説明](844dd72ce6a4bdff692cee2bcd80abd5.jpeg)
|
1
文法修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Python3 Tkinter カメラ映像の
|
1
|
+
Python3 Tkinter カメラ映像の描画位置と、カメラとの接続が途切れた場合の処理について
|
test
CHANGED
@@ -6,15 +6,15 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
-
|
9
|
+
- 描画位置をcanvasの中央にしたいが左上に表示されてしまっている
|
10
10
|
|
11
11
|
- カメラの接続が物理的に切れた場合などの処理をどうすればいいかわからない
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
描画位置については、おそらくcreate_image()の座標設定だと思いますが、指定する座標の求め方がわかりません。
|
16
|
-
|
16
|
+
|
17
|
-
解像度/2をすればよいのでしょうか?
|
17
|
+
カメラの解像度を求めて、解像度/2をすればよいのでしょうか?
|
18
18
|
|
19
19
|
|
20
20
|
|