ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.45k stars 13.49k forks source link

Cant use OpenCVDnn face_locations data in face_recog.face_encodings function #914

Open SaddamBInSyed opened 5 years ago

SaddamBInSyed commented 5 years ago

Description

  1. I am using OPenCvDnn method to detect the face since its giving more accurate similar to cnn_face_detect model.

  2. now instead of again finding the face_locations using the below method, face_locations = face_recognition.face_locations(rgb_small_frame, model="cnn")

  3. I have passed the step 1 output to the below function instead of step 2 face_locations.

face_encodings = face_recognition.face_encodings(rgb_small_frame, CVdnn_bboxes, num_jitters=1)

  1. Now no exception/error but face_recog is not accurate. ie. if i set tolerance >= 0.7 then its recognize the face with high FAR rate.

my code below,

def detectFaceOpenCVDnn(net, frame):
    frameOpencvDnn = frame.copy()
    frameHeight = frameOpencvDnn.shape[0]
    frameWidth = frameOpencvDnn.shape[1]
    blob = cv2.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], False, False)

    net.setInput(blob)
    detections = net.forward()
    bboxes = []
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        if confidence > conf_threshold:
            x1 = int(detections[0, 0, i, 3] * frameWidth)
            y1 = int(detections[0, 0, i, 4] * frameHeight)
            x2 = int(detections[0, 0, i, 5] * frameWidth)
            y2 = int(detections[0, 0, i, 6] * frameHeight)
            bboxes.append([x1, y1, x2, y2])
            #cv2.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight/150)), 8)
    return frameOpencvDnn, bboxes
##--------------

outOpencvDnn, **m_bboxes** = detectFaceOpenCVDnn(net,frame)

small_frame = cv2.resize(outOpencvDnn, (0, 0), fx=0.85, fy=0.85)
rgb_small_frame = small_frame[:, :, ::-1]
# below line commented purposefully.
#face_locations = face_recognition.face_locations(rgb_small_frame, number_of_times_to_upsample=1,model="cnn")

    face_encodings = face_recognition.face_encodings(rgb_small_frame, **m_bboxes**, num_jitters=1)    
    face_names = []
    print(str(len(face_encodings)))
    for face_encoding in face_encodings:
        # See if the face is a match for the known face(s)
        matches = face_recognition.compare_faces(all_enc_data, face_encoding, tolerance=0.45)
        #print("caompare---" + str(len(matches)))
        name = "Unknown"

        # Or instead, use the known face with the smallest distance to the new face
        face_distances = face_recognition.face_distance(all_enc_data, face_encoding)
        print(face_distances)
        best_match_index = np.argmin(face_distances)
        print(matches[best_match_index])
        if matches[best_match_index]:
            name = known_face_names[best_match_index]

        print(name)
        face_names.append(name)

So my ques here is, Is it not possible to use CvDnn face_location array in the face_recog function.?

help/advise appreciated highly.

Chandrakala14 commented 5 years ago

I am looking for the same, @SaddamBInSyed Had you able to make it work ?

SaddamBInSyed commented 4 years ago

@Chandrakala14 @ageitgey

i think you have to convert the detect bbox into css type which face_recog expect.