プログラムのポイント
ポイントは次の2点です。
- TkInterでの表示形式に画像を変換する
- TkInterのafter関数を利用して再帰的にループ処理を実現する
このプログラムだと、FPSの数値もほぼ変わりません(数値的にも若干落ちているような気もするという程度)。GUIを操作してもフレーム読み込みの処理が止まりませんので、OpenCVだけのよりはこちらのほうがいいかもしれません。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root=tk.Tk() | |
root.title("RGB Recorder") | |
root.geometry("800x600") | |
root.resizable(width=False, height=False) | |
canvas=tk.Canvas(root, width=640, height=480, bg="white") | |
canvas.pack() | |
def update():#update | |
global img | |
global camera | |
ret, frame =camera.read() | |
#TkInterで表示できる形式に変換する | |
img=ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))) | |
canvas.create_image(w/2,h/2,image=img) | |
root.after(1,update) #本関数を再帰的に呼び出す | |
#Cameraの設定などは省略してます | |
update() | |
root.mainloop() |
0 件のコメント:
コメントを投稿