简体   繁体   中英

Video stream not displayed on Tkinter screen

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())

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM