前提・実現したいこと
初心者ですので不躾な質問になるかもしれないですが、よろしくお願いします。
pythonにて、OPENCVを利用し、色が閾値を超えるドットの個数がある一定数を超えると、GUIで警告表示をしたい。
発生している問題・エラーメッセージ
while文の中にTkinterを組み込むと、動画検知が一旦停止してしまいます。
止まる原理はroot.mainloop()が原因と思われますが、Tkinterの方法としてこれしか調べることが出来ませんでした。
1.検知しながら、独立して警告用ウィンドウを出し続けるにはどうしたらいいのでしょうか?
2.そもそもOPENCVとTkinterを併用するのは間違っているのでしょうか?
よろしくお願いします。
該当のソースコード
python3
1# OpenCV のインポート 2import cv2 3import numpy as np 4import csv 5import tkinter as tk 6 7# VideoCaptureのインスタンスを作成する。 8# 引数でカメラを選べれる。 9cap = cv2.VideoCapture(0) 10 11while True: 12 root = tk.Tk() 13 root.title(u"コンパクター") 14 root.geometry("400x400") 15 16 frame1 = tk.Frame(root, pady=5) 17 frame1.pack() 18 label1 = tk.Label(frame1, font=("", 14), width=40) 19 label1.pack(side="top") 20 21 #root.mainloop() 22 # VideoCaptureから1フレーム読み込む 23 ret, frame = cap.read() 24 25 # スクリーンショットを撮りたい関係で1/4サイズに縮小 26 #frame = cv2.resize(frame, (int(frame.shape[1]/1), int(frame.shape[0]/1))) 27 # 加工なし画像を表示する 28 #cv2.imshow('Raw Frame', frame) 29 30 # 何か処理(ここでは文字列「hogehoge」を表示する) 31 edframe = frame 32 cv2.putText(edframe, 'test', (0, 50), 33 cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 3, cv2.LINE_AA) 34 cv2.line(edframe, (300, 60), (300, 400), (0, 0, 255)) 35 #cv2.circle(edframe, (100, 100), 15, (255, 255, 255), thickness=-1) 36 cv2.ellipse(edframe, (300, 230), (10, 10), 0, 0, 360, (0, 0, 255)) 37 cv2.ellipse(edframe, (300, 330), (10, 10), 0, 0, 360, (0, 0, 255)) 38 cv2.ellipse(edframe, (300, 120), (10, 10), 0, 0, 360, (0, 0, 255)) 39 px = edframe[230,301] #y,xなので注意 40 px2 = edframe[330,301] 41 px3 = edframe[120,301] 42 #print("{},{},{}".format(px3,px,px2)) 43 44 #print(type(px)) 45 if (px[0] > 100 and px[1] > 100 and px[2] > 100) : 46 #print("検知開始") 47 wide = 0 48 for i in range(50,450): 49 pxpaper = edframe[i,301] 50 if (pxpaper[0] > 130 and pxpaper[1] > 130 and pxpaper[2] > 130): 51 wide += 1 52 cv2.ellipse(edframe, (297, i), (1, 1), 0, 0, 360, (255, 0, 255)) 53 else : 54 pass 55 print(wide)#, end=" ") 56 else : 57 pass 58 # 加工済の画像を表示する 59 cv2.imshow('Edited Frame', edframe) 60 61 # キー入力を1ms待って、k が27(ESC)だったらBreakする 62 k = cv2.waitKey(1) 63 if k == 27: 64 break 65 root.mainloop() 66 67# キャプチャをリリースして、ウィンドウをすべて閉じる 68cap.release() 69cv2.destroyAllWindows()
試したこと
while文の前に入れてみたり、root.mainloop()の位置をwhile文の外に出したりしてみましたが、止まり方が変わるだけで解決しませんでした。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。