How can I improve the accuracy of the my program to detect the required faces properly?

27 Views Asked by At

The idea behind my program is to go through footage, detect and recognize a face that the user provides. However, my program recognizes other peoples faces as well. Any idea how I can make it better?

I have used haarcascade_frontalface_alt2.xml, and I have provided 10 images of the person to be found, lets say "criminal". However, the program wrongly identifies someone else's face as the criminal. Providing the code I have used :

def extract_faces(img):
    try:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        face_points = face_detector.detectMultiScale(gray, 1.2, 5, minSize=(20, 20))
        return face_points
    except:
        return []


def identify_face(facearray):
    model = joblib.load('static/face_recognition_model.pkl')
    return model.predict(facearray)


def train_model():
    faces = []
    labels = []
    userlist = os.listdir('static/faces')
    for user in userlist:
        for imgname in os.listdir(f'static/faces/{user}'):
            img = cv2.imread(f'static/faces/{user}/{imgname}')
            resized_face = cv2.resize(img, (50, 50))
            faces.append(resized_face.ravel())
            labels.append(user)
    faces = np.array(faces)
    knn = KNeighborsClassifier(n_neighbors=5)
    knn.fit(faces, labels)
    joblib.dump(knn, 'static/face_recognition_model.pkl')

def process_video(video_path1, video_path2):
    names, times, l = extract_mp()

    if 'face_recognition_model.pkl' not in os.listdir('static'):
        return render_template('index.html', names=names,  times=times, l=l, totalregNames=totalregNames(), totalregNumber=totalregNumber(), datetoday2=datetoday2, mess='There is no trained model in the static folder. Please add a new face to continue.')

    cap1 = cv2.VideoCapture(video_path1)
    cap2 = cv2.VideoCapture(video_path2)
    while cap1.isOpened() and cap2.isOpened():
        ret1, frame1 = cap1.read()
        ret2, frame2 = cap2.read()
        if not ret1 or not ret2:
            break

        if len(extract_faces(frame1)) > 0:
            (x, y, w, h) = extract_faces(frame1)[0]
            cv2.rectangle(frame1, (x, y), (x+w, y+h), (86, 32, 251), 1)
            cv2.rectangle(frame1, (x, y), (x+w, y-40), (86, 32, 251), -1)
            face = cv2.resize(frame1[y:y+h, x:x+w], (50, 50))
            identified_person = identify_face(face.reshape(1, -1))[0]
            add_mp(identified_person)
            abc = "Human"
            cv2.putText(frame1, f'{abc}', (x+5, y-5),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
0

There are 0 best solutions below