Saturday 16 January 2021

Face Recognition and Detection – AI Project

 Today, we have come up with a new python tutorial to build a face recognition and detection program. Today in this tutorial we are going to show something real beautiful program. Here, we are using Python 3.7.3, OpenCV, and some other Python libraries (NumPy, Dlib, CMake, and face_recognition) to build a face recognition and detection program. But, the interesting point is that you can use any version of Python and OpenCV to run this script.

Keep in mind, You just need to install a C++ compiler on your computer. You can download the C++  compiler while installing the Visual Studio Community version. 


We are going to develop a Python script that can detect whatever faces you like, it may be faces of your friends, faces of your relatives, faces of your best celebrities, faces of your idol. So, after developing a Python script you will be able to detect and recognize any faces you like and as many as you like. So what it does in this Python Script is actually drawing rectangular boxes around the faces and attempting to classify which ones they are based on a list of the provided faces. Otherwise, it will just show unknown but it still draws a rectangular box around the face. 

STEPS TO RUN CODE

  • a. Install the Python modules which are specified in "requirements.txt"
  • b. Within the root directory, there is the folder named “faces” which includes all the known faces, and that will be how faces of people are recognized and detected by the system.
  • c. Within the root directory, there is the folder named “test” where you can place your own images in which faces are recognized and detected.
  • d. The final step is to run the python script named “face_rec.py”.

At last, Press ‘q’ to exit the program


My View

It is not a simple task to detect and recognize the name of a person through images. The actual work is not easy, although it looks simple. We have got a variety of algorithms and models while researching and studying different papers. There was a huge doubt as to whether it would be complete or not while pursuing this project i.e. “Face Recognition and Detection”. This project has been successful as it was expected. Additional features like an explanation of look were added for better performance to identify the detect and recognize the name of the person. Time management was one of the challenging jobs because of the late completion of the project. However, information from different sources like reports, conferences, and books help us complete this project. The project was completed successfully due to numerous resources and libraries used.


-----Code---
face_rec.py


# installing required libraries

import os
import cv2
import face_recognition
import face_recognition as fr
import numpy as np


# encoding all the faces using get_encoded_faces () function
def get_encoded_faces():
    encoded = {}
    for dirpath, dnames, fnames in os.walk("./faces"):
        for f in fnames:
            if f.endswith(".jpg") or f.endswith(".png"):
                face = fr.load_image_file("faces/" + f)
                encoding = fr.face_encodings(face)[0]
                encoded[f.split(".")[0]] = encoding
    return encoded


# encode a face given the file name
def unknown_image_encoded(img):
    face = fr.load_image_file("faces/" + img)
    encoding = fr.face_encodings(face)[0]
    return encoding


# finding all the faces images and label that images
def classify_face(im):
    faces = get_encoded_faces()
    faces_encoded = list(faces.values())
    known_face_names = list(faces.keys())
    img = cv2.imread(im, 1)
    face_locations = face_recognition.face_locations(img)
    unknown_face_encodings = face_recognition.face_encodings(img, face_locations)

    face_names = []
    for face_encoding in unknown_face_encodings:
        # See if the face is a match for the known face(s)
        matches = face_recognition.compare_faces(faces_encoded, face_encoding)
        name = "Unknown"
        # use the known face with the smallest distance to the new face
        face_distances = face_recognition.face_distance(faces_encoded, face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            name = known_face_names[best_match_index]
        face_names.append(name)
        for (top, right, bottom, left), name in zip(face_locations, face_names):
            # Draw a box around the face
            cv2.rectangle(img, (left - 20, top - 20), (right + 20, bottom + 20), (255, 0, 0), 2)
            # Draw a label with a name below the face
            cv2.rectangle(img, (left - 20, bottom - 15), (right + 20, bottom + 20), (255, 0, 0), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(img, name, (left - 20, bottom + 15), font, 1.0, (255, 255, 255), 2)
    # Display the resulting image
    while True:
        cv2.imshow('Video', img)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            return face_names


print(classify_face("test/test.jpg"))
# print(classify_face("test/steve_frn.jpg"))

------requirements.txt-----
cmake
dlib
face_recognition
numpy
opencv-python

------

No comments:

Post a Comment

Connect broadband