Skip to main content
 首页 » 编程设计

python之基于人脸识别更新标签

2024年11月01日8jillzhang

我正在用 python tkinter 构建一个程序,它将在框架中放置一个带有人名的标签。如何获取要更新的名称?

我在 True 时尝试过,但没有成功。

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

我希望当我看着相机时,它应该读作 Jack,而当我不看着它时,它应该说没有。目前,如果我在它开始时开始查看它,它会说 Jack。如果我不这样做,它会说不,但不会更新。我该如何解决这个问题?

请您参考如下方法:

只需使用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 

希望对您有所帮助。