質問編集履歴

6

変更

2021/05/19 04:31

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -92,6 +92,8 @@
92
92
 
93
93
  self.label_header = "バーコード:"
94
94
 
95
+ self.barcodeData = ""
96
+
95
97
 
96
98
 
97
99
  self.queue_to_subthread = queue.Queue()
@@ -314,7 +316,7 @@
314
316
 
315
317
 
316
318
 
317
- if ret: #and decode_cnt % decode_span == 0:
319
+ if ret and decode_cnt % decode_span == 0:
318
320
 
319
321
  self.queue_to_subthread.put(["decode", self.frame])
320
322
 
@@ -328,24 +330,34 @@
328
330
 
329
331
  print(d)
330
332
 
331
- barcodeData = ""
333
+ x,y,w,h = (0,0,0,0)
334
+
332
-
335
+ if d:
336
+
333
- for barcode in d:
337
+ for barcode in d:
334
-
338
+
335
- x,y,w,h = barcode.rect
339
+ x,y,w,h = barcode.rect
340
+
336
-
341
+ self.barcodeData = barcode.data.decode("utf-8")
342
+
343
+
344
+
345
+
346
+
347
+ else:
348
+
349
+ self.barcodeData = ""
350
+
351
+ self.label["text"] = self.label_header + self.barcodeData
352
+
337
- cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,0,255),2)
353
+ cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,0,255),2)
338
-
339
- barcodeData = barcode.data.decode("utf-8")
354
+
340
-
341
- frame = cv2.putText(self.frame,barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
355
+ frame = cv2.putText(self.frame,self.barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
342
-
343
-
344
-
345
- self.label["text"] = self.label_header + barcodeData
346
356
 
347
357
 
348
358
 
359
+
360
+
349
361
  except queue.Empty:
350
362
 
351
363
  pass
@@ -356,7 +368,7 @@
356
368
 
357
369
  self.canvas.itemconfig(self.img_id, image=self.photo)
358
370
 
359
- #decode_cnt += 1
371
+ decode_cnt += 1
360
372
 
361
373
  self.canvas.after(15, self.update_by_timer)
362
374
 
@@ -404,6 +416,10 @@
404
416
 
405
417
  queue_to_mainthread.put(["result", d])
406
418
 
419
+ else:
420
+
421
+ queue_to_mainthread.put(["result", None])
422
+
407
423
  elif msg == "snapshot":
408
424
 
409
425
  self.snapshot()
@@ -448,8 +464,6 @@
448
464
 
449
465
  main()
450
466
 
451
-
452
-
453
467
  ```
454
468
 
455
469
 

5

変更

2021/05/19 04:31

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -314,7 +314,7 @@
314
314
 
315
315
 
316
316
 
317
- if ret and decode_cnt % decode_span == 0:
317
+ if ret: #and decode_cnt % decode_span == 0:
318
318
 
319
319
  self.queue_to_subthread.put(["decode", self.frame])
320
320
 
@@ -322,11 +322,27 @@
322
322
 
323
323
  try:
324
324
 
325
- msg, data = self.queue_to_mainthread.get_nowait()
325
+ msg, d = self.queue_to_mainthread.get_nowait()
326
+
326
-
327
+ #print("data: \n{}".format(d))
328
+
329
+ print(d)
330
+
331
+ barcodeData = ""
332
+
333
+ for barcode in d:
334
+
335
+ x,y,w,h = barcode.rect
336
+
337
+ cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,0,255),2)
338
+
339
+ barcodeData = barcode.data.decode("utf-8")
340
+
341
+ frame = cv2.putText(self.frame,barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
342
+
343
+
344
+
327
- self.label["text"] = self.label_header + data[0]
345
+ self.label["text"] = self.label_header + barcodeData
328
-
329
- self.frame = data[1]
330
346
 
331
347
 
332
348
 
@@ -334,11 +350,13 @@
334
350
 
335
351
  pass
336
352
 
353
+
354
+
337
- self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame)
355
+ self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame))
338
356
 
339
357
  self.canvas.itemconfig(self.img_id, image=self.photo)
340
358
 
341
- decode_cnt += 1
359
+ #decode_cnt += 1
342
360
 
343
361
  self.canvas.after(15, self.update_by_timer)
344
362
 
@@ -380,27 +398,11 @@
380
398
 
381
399
  if msg == "decode":
382
400
 
383
- barcodeData = ""
384
-
385
- frame = data
386
-
387
- d = decode(frame)
401
+ d = decode(data)
388
402
 
389
403
  if d:
390
404
 
391
- #print("d: \n{}".format(d))
392
-
393
- for barcode in d:
394
-
395
- x,y,w,h = barcode.rect
396
-
397
- cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
398
-
399
- barcodeData = barcode.data.decode('utf-8')
400
-
401
- frame = cv2.putText(frame,barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
402
-
403
- queue_to_mainthread.put(["result", [barcodeData, frame]])
405
+ queue_to_mainthread.put(["result", d])
404
406
 
405
407
  elif msg == "snapshot":
406
408
 
@@ -446,6 +448,8 @@
446
448
 
447
449
  main()
448
450
 
451
+
452
+
449
453
  ```
450
454
 
451
455
 

4

変更

2021/05/19 03:38

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -326,16 +326,18 @@
326
326
 
327
327
  self.label["text"] = self.label_header + data[0]
328
328
 
329
-
330
-
331
- self.photo = ImageTk.PhotoImage(image = Image.fromarray(data[1]))
329
+ self.frame = data[1]
332
-
333
- self.canvas.itemconfig(self.img_id, image=self.photo)
330
+
331
+
334
332
 
335
333
  except queue.Empty:
336
334
 
337
335
  pass
338
336
 
337
+ self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame)
338
+
339
+ self.canvas.itemconfig(self.img_id, image=self.photo)
340
+
339
341
  decode_cnt += 1
340
342
 
341
343
  self.canvas.after(15, self.update_by_timer)
@@ -394,6 +396,8 @@
394
396
 
395
397
  cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
396
398
 
399
+ barcodeData = barcode.data.decode('utf-8')
400
+
397
401
  frame = cv2.putText(frame,barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
398
402
 
399
403
  queue_to_mainthread.put(["result", [barcodeData, frame]])
@@ -416,7 +420,7 @@
416
420
 
417
421
  def snapshot(self):
418
422
 
419
- filename = "/home/pi/デスクトップ/img/frame-" + datetime.now().strftime("%Y-%d-%m-%H-%M-%S_%f") + ".jpg"
423
+ filename = "/home/pi/デスクトップ/img/frame-" + datetime.now().strftime("%Y-%m-%d-%H-%M-%S_%f") + ".jpg"
420
424
 
421
425
  cv2.imwrite(filename,
422
426
 

3

変更

2021/05/19 02:09

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -320,15 +320,21 @@
320
320
 
321
321
 
322
322
 
323
+ try:
324
+
323
- msg, data = self.queue_to_mainthread.get()
325
+ msg, data = self.queue_to_mainthread.get_nowait()
324
-
326
+
325
- self.label["text"] = self.label_header + data[0]
327
+ self.label["text"] = self.label_header + data[0]
326
-
327
-
328
-
328
+
329
+
330
+
329
- self.photo = ImageTk.PhotoImage(image = Image.fromarray(data[1]))
331
+ self.photo = ImageTk.PhotoImage(image = Image.fromarray(data[1]))
330
-
332
+
331
- self.canvas.itemconfig(self.img_id, image=self.photo)
333
+ self.canvas.itemconfig(self.img_id, image=self.photo)
334
+
335
+ except queue.Empty:
336
+
337
+ pass
332
338
 
333
339
  decode_cnt += 1
334
340
 

2

変更

2021/05/19 01:48

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -52,6 +52,12 @@
52
52
 
53
53
 
54
54
 
55
+ decode_cnt = 0
56
+
57
+ decode_span = 20
58
+
59
+
60
+
55
61
  class App:
56
62
 
57
63
  def __init__(self, win):
@@ -84,11 +90,13 @@
84
90
 
85
91
 
86
92
 
87
- self.label_header = "読み取ったデ:"
93
+ self.label_header = "コード:"
88
-
89
-
90
-
94
+
95
+
96
+
91
- self.data_queue = queue.Queue()
97
+ self.queue_to_subthread = queue.Queue()
98
+
99
+ self.queue_to_mainthread = queue.Queue()
92
100
 
93
101
 
94
102
 
@@ -96,7 +104,7 @@
96
104
 
97
105
  self.win.title("Camera")
98
106
 
99
- self.thread = threading.Thread(target=self.get_queue)
107
+ self.thread = threading.Thread(target=self.get_queue, args=(self.queue_to_mainthread,))
100
108
 
101
109
  self.thread.start()
102
110
 
@@ -116,7 +124,7 @@
116
124
 
117
125
  #self.update_by_timer()
118
126
 
119
- self.canvas.after(200, self.update_by_timer)
127
+ self.canvas.after(15, self.update_by_timer)
120
128
 
121
129
 
122
130
 
@@ -142,7 +150,7 @@
142
150
 
143
151
  def close(self, e=None):
144
152
 
145
- self.data_queue.put(["close", None])
153
+ self.queue_to_subthread.put(["close", None])
146
154
 
147
155
  self.thread.join()
148
156
 
@@ -180,25 +188,67 @@
180
188
 
181
189
 
182
190
 
183
- #ret, frm = self.vcap.read()
191
+ ret, frm = self.vcap.read()
184
192
 
185
193
  #print("ret: {}".format(ret))
186
194
 
187
195
  #print("frm: {}".format(frm))
188
196
 
189
- #self.frame = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)
197
+ self.frame = cv2.cvtColor(frm, cv2.COLOR_BGR2RGB)
190
-
198
+
191
- #self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame))
199
+ self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame))
192
-
200
+
193
- #self.canvas.update()
201
+ self.canvas.update()
194
-
202
+
195
- #cv_w = self.canvas.winfo_reqwidth()
203
+ cv_w = self.canvas.winfo_reqwidth()
196
-
204
+
197
- #cv_h = self.canvas.winfo_reqheight()
205
+ cv_h = self.canvas.winfo_reqheight()
198
-
206
+
199
- #self.img_id = self.canvas.create_image(cv_w // 2, cv_h // 2, image=self.photo, anchor="center")
207
+ self.img_id = self.canvas.create_image(cv_w // 2, cv_h // 2, image=self.photo, anchor="center")
208
+
209
+
210
+
211
+
212
+
200
-
213
+ """
214
+
201
-
215
+ self.canvas.update()
216
+
217
+ cv_w = self.canvas.winfo_width()
218
+
219
+ cv_h = self.canvas.winfo_height()
220
+
221
+
222
+
223
+ rec_id = self.canvas.create_rectangle(0, 0, cv_w, cv_h, fill="black")
224
+
225
+
226
+
227
+
228
+
229
+ # 長方形の座標取得
230
+
231
+ rec_pos = self.canvas.coords(rec_id)
232
+
233
+ # テキストを描画(位置は適当)
234
+
235
+ text_id = self.canvas.create_text(0, 0, text="NO SIGNAL", font=("", 20), fill="white")
236
+
237
+ # テキストのサイズ取得
238
+
239
+ text_size = self.canvas.bbox(text_id)
240
+
241
+ # テキストの座標移動
242
+
243
+ rc_x = rec_pos[2] / 2
244
+
245
+ rc_y = rec_pos[2] / 3
246
+
247
+ tc_y = text_size[3] / 2
248
+
249
+ self.canvas.move(text_id, rc_x - (rec_pos[0] / 2), rc_y - tc_y)
250
+
251
+ """
202
252
 
203
253
 
204
254
 
@@ -220,7 +270,7 @@
220
270
 
221
271
  def pushed_button(self):
222
272
 
223
- self.data_queue.put(["snapshot", None])
273
+ self.queue_to_subthread.put(["snapshot", None])
224
274
 
225
275
 
226
276
 
@@ -228,7 +278,7 @@
228
278
 
229
279
  def update_by_timer(self):
230
280
 
231
- print(datetime.now())
281
+ global decode_cnt, decode_span
232
282
 
233
283
  ret, self.frame = self.vcap.read()
234
284
 
@@ -264,86 +314,98 @@
264
314
 
265
315
 
266
316
 
317
+ if ret and decode_cnt % decode_span == 0:
318
+
319
+ self.queue_to_subthread.put(["decode", self.frame])
320
+
321
+
322
+
323
+ msg, data = self.queue_to_mainthread.get()
324
+
325
+ self.label["text"] = self.label_header + data[0]
326
+
327
+
328
+
329
+ self.photo = ImageTk.PhotoImage(image = Image.fromarray(data[1]))
330
+
331
+ self.canvas.itemconfig(self.img_id, image=self.photo)
332
+
333
+ decode_cnt += 1
334
+
335
+ self.canvas.after(15, self.update_by_timer)
336
+
337
+
338
+
339
+
340
+
341
+ def update_by_resize(self, e):
342
+
343
+ self.canvas.itemconfig(self.img_id, image=self.photo, anchor="center")
344
+
345
+ cv_w = self.canvas.winfo_width()
346
+
347
+ cv_h = self.canvas.winfo_height()
348
+
349
+ self.canvas.coords(self.img_id, cv_w // 2, cv_h // 2)
350
+
351
+ #pass
352
+
353
+
354
+
355
+
356
+
357
+ def bind_event(self):
358
+
359
+ self.win.bind("<Escape>", self.close)
360
+
361
+ self.canvas.bind("<Configure>", self.update_by_resize)
362
+
363
+
364
+
365
+
366
+
367
+ def get_queue(self, queue_to_mainthread):
368
+
267
- if ret:
369
+ while True:
370
+
268
-
371
+ msg, data = self.queue_to_subthread.get()
372
+
373
+ if msg == "decode":
374
+
375
+ barcodeData = ""
376
+
377
+ frame = data
378
+
269
- d = decode(self.frame)
379
+ d = decode(frame)
270
-
380
+
271
- if d:
381
+ if d:
272
-
382
+
273
- #print("d: \n{}".format(d))
383
+ #print("d: \n{}".format(d))
274
-
384
+
275
- for barcode in d:
385
+ for barcode in d:
276
-
386
+
277
- x,y,w,h = barcode.rect
387
+ x,y,w,h = barcode.rect
278
-
388
+
279
- cv2.rectangle(self.frame,(x,y),(x+w,y+h),(0,0,255),2)
389
+ cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
280
-
281
- barcodeData = barcode.data.decode('utf-8')
390
+
282
-
283
- self.label["text"] = self.label_header + barcodeData
284
-
285
- frame = cv2.putText(self.frame,barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
391
+ frame = cv2.putText(frame,barcodeData,(x,y-10),self.font,.5,(0,0,255),2,cv2.LINE_AA)
392
+
393
+ queue_to_mainthread.put(["result", [barcodeData, frame]])
394
+
395
+ elif msg == "snapshot":
396
+
397
+ self.snapshot()
398
+
399
+ elif msg == "close":
400
+
401
+ break
286
402
 
287
403
  else:
288
404
 
289
- self.label["text"] = self.label_header
290
-
291
- #self.photo = ImageTk.PhotoImage(image = Image.fromarray(self.frame))
292
-
293
- #self.canvas.itemconfig(self.img_id, image=self.photo)
294
-
295
- self.canvas.after(100, self.update_by_timer)
296
-
297
-
298
-
299
-
300
-
301
- def update_by_resize(self, e):
302
-
303
- #self.canvas.itemconfig(self.img_id, image=self.photo, anchor="center")
304
-
305
- #cv_w = self.canvas.winfo_width()
306
-
307
- #cv_h = self.canvas.winfo_height()
308
-
309
- #self.canvas.coords(self.img_id, cv_w // 2, cv_h // 2)
310
-
311
- pass
312
-
313
-
314
-
315
-
316
-
317
- def bind_event(self):
318
-
319
- self.win.bind("<Escape>", self.close)
405
+ print("----- Undefined msg -----")
320
-
321
- self.canvas.bind("<Configure>", self.update_by_resize)
322
-
323
-
324
-
325
-
326
-
327
- def get_queue(self):
328
-
329
- while True:
330
-
331
- msg, data = self.data_queue.get()
332
-
333
- if msg == "snapshot":
334
-
335
- self.snapshot()
336
-
337
- elif msg == "close":
338
406
 
339
407
  break
340
408
 
341
- else:
342
-
343
- print("----- Undefined msg -----")
344
-
345
- break
346
-
347
409
 
348
410
 
349
411
  def snapshot(self):

1

追記

2021/05/19 00:48

投稿

person
person

スコア224

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  after()の中にprint()にてタイムスタンプ表示を行うと、指定[ms]に対して少し誤差はあるものの、映像表示ほどの遅れはありません。
10
10
 
11
+ 指定時間[ms]は10[ms]、100[ms]、1000[ms]で試験しましたが、どれも同じくらい遅いです。
12
+
11
13
 
12
14
 
13
15
  canvasの描画内容をコメントアウトしてバーコードの読み取りのみに変更しても、読み取るのに要する時間(バーコードをカメラの前にもっていってから、tkinterのラベルに表示するまでの時間)が2~3秒と長いような気がします。(単にピントが合っておらず、時間がかかっているときもありますが)