Video stream not displayed on Tkinter screen

时间:2019-04-16 22:21:21

标签: python opencv tkinter

screen_tkinter

I've created a class Tela which is basically is my Screen and I'm trying to display the webcam video on a Tkinter GUI. My webcam LED is on but the label "painel" where I want do show my video is grey. Can someone tell me what's wrong with my code. I appreciate.

class Tela:
def __init__(self, janela):
    self.janela = janela
    self.janela.title("Reconhecimento Facial")
    self.janela.config(background="#FFFFFF")

    # Open camera
    self.cam = cv2.VideoCapture(0)
    self.detector = dlib.get_frontal_face_detector()

    self.quadro = tkinter.Frame(janela, width=600, height=500)
    self.quadro.grid(row=0, column=0, padx=10, pady=2, rowspan=3)

    self.painel = tkinter.Label(self.quadro)
    self.quadro.grid(row=0, column=0, rowspan=3)

    # Methods for screen update
    self.delay = 15
    self.update()
    self.janela.mainloop()

def update(self):
    # Get frame 
    ret, frame = self.cam.read()    
    faces, confianca, idx = self.detector.run(frame)
    for i, face in enumerate(faces):
        e, t, d, b = (int(face.left()), int(face.top()), int(face.right()), int(face.bottom()))
        cv2.rectangle(frame, (e, t), (d, b), (0, 255, 255), 2)
    cv2image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGBA)
    image = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=image)
    self.painel.imgtk = imgtk
    self.painel.configure(image=imgtk)
    self.janela.after(self.delay, self.update)

   # Creates the window
   Tela(tkinter.Tk())

1 个答案:

答案 0 :(得分:0)

Mistake is in second line

 self.painel = tkinter.Label(self.quadro)
 self.quadro.grid(row=0, column=0, rowspan=3)

You created Label but you don't put it in window - it has to be self.painel.grid instead of self.quadro.grid

 self.painel = tkinter.Label(self.quadro)
 self.painel.grid(row=0, column=0, rowspan=3)