由于face_recognition中的numpy而发生错误

时间:2017-11-25 14:56:03

标签: opencv face-recognition

import face_recognition
import cv2
import os, os.path
import numpy as np 

count = 0 

def finding_members():
    finding_members.list= os.listdir('/Users/apple/Desktop/face_id/train_models')
    print(finding_members.list)
    video_capture = cv2.VideoCapture(0)
    count = 0 
    count1 = 0
    encoding_name = str()
    face_encoding = []
    length_of_encoding = 0
    length_of_encodings = 0
    name_face_encodings = []
    names = []




    for i in finding_members.list:
        findDot = i.find('.')
        finding_members.name = i[0:findDot]
        if ((len(finding_members.name)) > 1):
            names.append(finding_members.name)

        print(names)

    for i in finding_members.list:

        dit = "train_models/"+i
        print(dit)
        if (i != ".DS_Store"):
            images = face_recognition.load_image_file(dit)
            findDot = i.find('.')
            encoding_name = i[0:findDot]
            if (length_of_encodings >= 0):
                name_face_encodings.append(face_encoding)





            face_encoding = [face_recognition.face_encodings(images)[0]]
            length_of_encoding = len(face_encoding)

            #face_encoding.update(face_encoding)
            print(len(name_face_encodings))

            count = count+1
            print (count)

    while True:
        #cv2.imshow('Video', frame)
        ret, frame = video_capture.read()
        cv2.imshow('video1',frame)
        face_locations = face_recognition.face_locations(frame)
        face_encodings = face_recognition.face_encodings(frame, face_locations)
        #for 
        for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
            for paru in names:
                print(len(name_face_encodings))
                print(count1)
                local_encoding = name_face_encodings[count1]

                match = face_recognition.compare_faces([local_encoding], face_encoding)
                count1 = count1 + 1
                name = "Unknown"
                print(count1)
                print(names[count1])
                if (match ==True):
                    name = names[count1]

                cv2.rectangle(frame, (left, top), (right, bottom), (100, 40, 100), 2)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left + 6, top - 6), font, 1.0, (255, 255, 255), 1)

        print(names)
        #print(len())
        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()




finding_members()
print(finding_members.list)
print(finding_members.name)
  

ValueError:操作数无法与形状(1,0)(128,)

一起广播      

error = match = face_recognition.compare_faces([local_encoding],face_encoding

     我认为如何摆脱这些错误是由于numpy索引      

match = face_recognition.compare_faces([local_encoding],face_encoding)

     
    

文件     “/Users/apple/anaconda3/envs/face_id/lib/python3.6/site-packages/face_recognition/api.py”     第203行,在compare_faces中

  

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话,

frame_captured的帧是BGR,因此帧将其转换为RGB。

rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)