繁体   English   中英

基于人脸识别更新标签

[英]Update label based on facial recognition

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

我试过 while 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

希望有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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