2015-04-22 155 views
0

我想刪除當前顯示由OpenCV製作的網絡攝像頭流的Tkinter標籤。我最終發生了這種情況,但不是以我想要的方式發生,因爲它只是停止流,但流輸出的最後一個圖像仍然存在。該代碼是這樣的:Tkinter標籤刪除的Python OpenCV輸出

from Tkinter import * 
import cv2 
from PIL import Image, ImageTk 

def Start(): 
    width, height = 800, 600 
    cap = cv2.VideoCapture(0) 
    cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width) 
    cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height) 
    def show_frame(): 
     _, frame = cap.read() 
     frame = cv2.flip(frame, 1) 
     cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) 
     img = Image.fromarray(cv2image) 
     imgtk = ImageTk.PhotoImage(image=img) 
     lmain.imgtk = imgtk 
     lmain.configure(image=imgtk) 
     lmain.after(10, show_frame) 
    show_frame() 

root = Tk() 
lmain = Label(root) 
lmain.pack(side = RIGHT) 

Button1 = Button(root, text = "Start", command = Start) 
Button1.pack(side = LEFT) 
Button2 = Button(root, text = "Stop", command = Start) 
Button2.pack(side = LEFT) 

root.mainloop() 

您可能注意到,我用來停止它是我用來啓動功能相同的功能,那就是因爲我就如何阻止它很無能。

回答

0

我已經試過你的代碼,我在這裏補充一些代碼:

from Tkinter import * 
import cv2 
from PIL import Image, ImageTk 

isrunning = 0 
def Start(): 
    global isrunning 
    if isrunning == 0: 
     width, height = 800, 600 
     cap = cv2.VideoCapture(0) 
     cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width) 
     cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height) 
     isrunning = 1 
     lmain.pack(side = RIGHT) 

     def show_frame(): 
      _, frame = cap.read() 
      frame = cv2.flip(frame, 1) 
      cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) 
      img = Image.fromarray(cv2image) 
      imgtk = ImageTk.PhotoImage(image=img) 
      lmain.imgtk = imgtk 
      lmain.configure(image=imgtk) 
      if isrunning == 1: 
       lmain.after(10, show_frame) 

    show_frame() 

def Stop(): 
    global isrunning 
    isrunning = 0 
    lmain.pack_forget() 

root = Tk() 
lmain = Label(root) 

Button1 = Button(root, text = "Start", command = Start) 
Button1.pack(side = LEFT) 
Button2 = Button(root, text = "Stop", command = Stop) 
Button2.pack(side = LEFT) 

root.mainloop() 

,可以看到我添加了一個全局變量isrunning每次都讓功能show_frame檢查。如果變量isrunning等於0,則該功能將停止。我還添加了功能Stop作爲包含代碼lmain.pack_forget()的「停止」按鈕的回調函數以刪除標籤。

由於每次單擊「停止」按鈕時都會刪除標籤,因此我將用於添加標籤的代碼移動到Start函數中。希望能幫助到你。

+0

我已經嘗試了代碼,你修飾它,我做了一個小的調整它,因爲它的一部分,你可以刪除標籤,但它並沒有關閉網絡攝像頭我也是這麼做的,所以你可以再次按下「開始」,它仍然可以工作。無論如何,我感謝你的幫助,因爲我用它作爲基礎。 該代碼在我的回答問題帖子中。 –

1

的代碼是:

from Tkinter import * 
import cv2 
from PIL import Image, ImageTk 

isrunning = 0 
def Start(): 
    global isrunning 
    if isrunning == 0: 
     global cap 
     cap = cv2.VideoCapture(0) 
     isrunning = 1 
     lmain.pack(side = RIGHT) 

     def show_frame(): 
      _, frame = cap.read() 
      frame = cv2.flip(frame, 1) 
      cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA) 
      img = Image.fromarray(cv2image) 
      imgtk = ImageTk.PhotoImage(image=img) 
      lmain.imgtk = imgtk 
      lmain.configure(image=imgtk) 
      if isrunning == 1: 
       lmain.after(10, show_frame) 

    show_frame() 

def Stop(): 
    global isrunning 
    isrunning = 0 
    lmain.pack_forget() 

def main(): 
    Stop() 
    Button1.invoke() 
    Stop() 


root = Tk() 
lmain = Label(root) 

Button1 = Button(root, text = "Start", command = Start) 
Button1.pack(side = LEFT) 
Button2 = Button(root, text = "Stop", command = main) 
Button2.pack(side = LEFT) 

root.mainloop()