简体   繁体   中英

Update label based on facial recognition

I am building a program in python tkinter that will put a label with the name of the person in frame on it. How do I get the name to update?

I tried while True but it didn't work.

import cv2
from time import sleep
import face_recognition as fr
from tkinter import *
def main():
tk = Tk()
cap = cv2.VideoCapture(0)
sleep(1)
while True:
    ret, frame = cap.read()
#  frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    cv2.imwrite("temp.jpg", frame)
    image = fr.load_image_file("temp.jpg")
    #img = __draw_label(frame, "Jack", face_locations[0][:2], (255,0,0))
    #cv2.imshow("Hello", img)
    v = StringVar()
    w = Label(tk, textvariable=v)
    w.pack()
    if len(fr.face_locations(image)) > 0:
        face_encoding = fr.face_encodings(image)[0]
        faceid = fr.compare_faces(faces, face_encoding)
        if True in faceid:
            v.set(names[faceid.index(True)])
        else:
            v.set("Unknown")
    else:
        v.set("None")
    mainloop()

I expect that when I'm looking at the camera it should read Jack and when I'm not it should say none. Currently if I begin looking at it when it starts it says Jack. And if I do't it says non but it doesn't update. How can I fix this?

Simply use tk.update() :

while True:
    ret, frame = cap.read()
    …
    v = StringVar()
    w = Label(tk, textvariable=v)
    w.pack()
    tk.update() #show potential changes on your window

Hope that helped.

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