面对识别但无法获得用户的信息此类用户名称

发布于 2025-02-09 07:18:23 字数 4351 浏览 1 评论 0原文

我遇到了一个错误的信息,该系统已识别谁 的用户名称

系统可以识别面部,但无法获取用户信息,例如我尝试更改以下代码

all_matches = face_recognition.compare_faces(known_face_encodings, current_face_encoding)
if True in all_matches:
    first_match_index=all_matches.index[True]
    name_of_person = known_face_names[first_match_index]

:我曾经使用numpy将数组嵌套在数组中 这是代码

all_matches = np.array(face_recognition.compare_faces(known_face_encodings, current_face_encoding))
if all_matches.any()==True in all_matches:
    first_match_index=all_matches.index[True]
    name_of_person = known_face_names[first_match_index]

,但获得了typeerro attributeError:'numpy.ndarray'对象没有属性'索引

这是识别python文件,

#impoting packages 
import cv2
import face_recognition
import json
import numpy as np
#capture the video from default camera 
webcam_video_stream = cv2.VideoCapture(0,cv2.CAP_DSHOW)
all_face_locations = []
all_face_encodings = []
all_face_names = []
known_face_names = []
known_face_image=[]
known_face_encodings =[]
with open('individuals.json') as json_file:
            alldata=json.loads(json_file.read());
            for person in alldata:
                known_face_names.append(person['name'])
                newKnownImages =known_face_image.append(face_recognition.load_image_file('images/samples/'+person['image']))
                known_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file('images/samples/'+person['image'])))
                print(known_face_encodings)

while True:
    #get the current frame from the video stream as an image
    ret,current_frame = webcam_video_stream.read()
    #resize the current frame to 1/4 size to proces faster
    current_frame_small = cv2.resize(current_frame,(0,0),fx=0.25,fy=0.25)
    #detect all faces in the image
    #arguments are image,no_of_times_to_upsample, model
    all_face_locations = 
    face_recognition.face_locations(current_frame_small,number_of_times_to_upsample=1,model='hog')
    
    #detect face encodings for all the faces detected
    all_face_encodings = face_recognition.face_encodings(current_frame_small,all_face_locations)


    #looping through the face locations and the face embeddings
    for current_face_location,current_face_encoding in zip(all_face_locations,all_face_encodings):
        #splitting the tuple to get the four position values of current face
        top_pos,right_pos,bottom_pos,left_pos = current_face_location
        
        #change the position maginitude to fit the actual size video frame
        top_pos = top_pos*4
        right_pos = right_pos*4
        bottom_pos = bottom_pos*4 
        left_pos = left_pos*4
        
        #find all the matches and get the list of matches
        all_matches =face_recognition.compare_faces(known_face_encodings, current_face_encoding)
        
       
        #string to hold the label
        name_of_person = 'Unknown face'
        #check if the all_matches have at least one item
        #if yes, get the index number of face that is located in the first index of all_matches
        #get the name corresponding to the index number and save it in name_of_person
        
        if True in all_matches:
            first_match_index=all_matches.index[True]
            name_of_person = known_face_names[first_match_index]
            
        
        #draw rectangle around the face    
        cv2.rectangle(current_frame,(left_pos,top_pos),(right_pos,bottom_pos),(255,0,0),2)
        
        #display the name as text in the image
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(current_frame, name_of_person, (left_pos,bottom_pos), font, 0.5, (255,255,255),1)
    
    #display the video
    cv2.imshow("Webcam Video",current_frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#release the stream and cam
#close all opencv windows open
webcam_video_stream.release()
cv2.destroyAllWindows()     

这是我的json文件代码

[ {

    "name": "me",
    "id": "c119015",
    "username": "me",
    "image": "me.jpeg"
},
{
    "name": "bilgates",
    "id": "c10996",
    "username": "billgates",
    "image": "bilgates.jpg"
},
{

    "name": "Elon_Musk",
    "id": "c1091",
    "username": "Elon_Musk",
    "image": "Elon_Musk.jpg"
}

] 我可以使用async-await获取每个用户的信息吗

I got an error getting user's information who the system recognized
the system can recognize the face but cannot get user information such as the name of the user

I tried to change the original code of this:

all_matches = face_recognition.compare_faces(known_face_encodings, current_face_encoding)
if True in all_matches:
    first_match_index=all_matches.index[True]
    name_of_person = known_face_names[first_match_index]

I used to nest array inside array using Numpy
this is the code

all_matches = np.array(face_recognition.compare_faces(known_face_encodings, current_face_encoding))
if all_matches.any()==True in all_matches:
    first_match_index=all_matches.index[True]
    name_of_person = known_face_names[first_match_index]

but got typeErro AttributeError: 'numpy.ndarray' object has no attribute 'index

this is the recognition python file

#impoting packages 
import cv2
import face_recognition
import json
import numpy as np
#capture the video from default camera 
webcam_video_stream = cv2.VideoCapture(0,cv2.CAP_DSHOW)
all_face_locations = []
all_face_encodings = []
all_face_names = []
known_face_names = []
known_face_image=[]
known_face_encodings =[]
with open('individuals.json') as json_file:
            alldata=json.loads(json_file.read());
            for person in alldata:
                known_face_names.append(person['name'])
                newKnownImages =known_face_image.append(face_recognition.load_image_file('images/samples/'+person['image']))
                known_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file('images/samples/'+person['image'])))
                print(known_face_encodings)

while True:
    #get the current frame from the video stream as an image
    ret,current_frame = webcam_video_stream.read()
    #resize the current frame to 1/4 size to proces faster
    current_frame_small = cv2.resize(current_frame,(0,0),fx=0.25,fy=0.25)
    #detect all faces in the image
    #arguments are image,no_of_times_to_upsample, model
    all_face_locations = 
    face_recognition.face_locations(current_frame_small,number_of_times_to_upsample=1,model='hog')
    
    #detect face encodings for all the faces detected
    all_face_encodings = face_recognition.face_encodings(current_frame_small,all_face_locations)


    #looping through the face locations and the face embeddings
    for current_face_location,current_face_encoding in zip(all_face_locations,all_face_encodings):
        #splitting the tuple to get the four position values of current face
        top_pos,right_pos,bottom_pos,left_pos = current_face_location
        
        #change the position maginitude to fit the actual size video frame
        top_pos = top_pos*4
        right_pos = right_pos*4
        bottom_pos = bottom_pos*4 
        left_pos = left_pos*4
        
        #find all the matches and get the list of matches
        all_matches =face_recognition.compare_faces(known_face_encodings, current_face_encoding)
        
       
        #string to hold the label
        name_of_person = 'Unknown face'
        #check if the all_matches have at least one item
        #if yes, get the index number of face that is located in the first index of all_matches
        #get the name corresponding to the index number and save it in name_of_person
        
        if True in all_matches:
            first_match_index=all_matches.index[True]
            name_of_person = known_face_names[first_match_index]
            
        
        #draw rectangle around the face    
        cv2.rectangle(current_frame,(left_pos,top_pos),(right_pos,bottom_pos),(255,0,0),2)
        
        #display the name as text in the image
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(current_frame, name_of_person, (left_pos,bottom_pos), font, 0.5, (255,255,255),1)
    
    #display the video
    cv2.imshow("Webcam Video",current_frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

#release the stream and cam
#close all opencv windows open
webcam_video_stream.release()
cv2.destroyAllWindows()     

this is my JSON file code

[
{

    "name": "me",
    "id": "c119015",
    "username": "me",
    "image": "me.jpeg"
},
{
    "name": "bilgates",
    "id": "c10996",
    "username": "billgates",
    "image": "bilgates.jpg"
},
{

    "name": "Elon_Musk",
    "id": "c1091",
    "username": "Elon_Musk",
    "image": "Elon_Musk.jpg"
}

]
can I use async-await to get each user's information

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文