Face Recognition in Python – Data Analysis Projects

Face Recognition in Python – Data Analysis Projects

In today’s digital landscape, the advent of face recognition technology and data analysis projects has revolutionized the way we interact with information. College students, as active participants in this evolving landscape, can benefit greatly from understanding the potential of face recognition and the role it plays in data analysis projects. This article aims to explore the applications and significance of face recognition, shedding light on its integration with data analysis projects. By grasping the capabilities of these technologies, students can navigate the digital realm with confidence and make informed decisions.

Face recognition technology and data analysis projects have transformed the way we engage with information. College students can greatly benefit from understanding the potential of face recognition and its integration with data analysis. By grasping the capabilities of these technologies, students can navigate the digital realm confidently and make informed decisions.

Face Recognition: Unleashing the Potential

Facial recognition, a groundbreaking application of artificial intelligence, has garnered significant attention for its ability to accurately identify and verify individuals based on their unique facial features. Through the utilization of intricate algorithms, Facial recognition technology has found diverse applications across numerous sectors, including security, entertainment, and marketing.

Face detection, powered by advanced algorithms, accurately identifies individuals based on unique facial features. It finds applications in security systems, access control, and law enforcement. Facial recognition algorithms like Eigenfaces capture essential facial features through Principal Component Analysis (PCA), allowing efficient matching against known faces.

The technology offers convenience by enabling facial unlocking on smartphones, enhancing user experience, and ensuring data security. Face detection is used in entertainment industry applications like personalized recommendations in streaming platforms, virtual reality, and augmented reality.

Face Recognition: Algorithm

This Project uses the following algorithm.

Face Recognition: Eigenfaces

One influential algorithm in Face detection is Eigenfaces, which employs Principal Component Analysis (PCA) to capture essential facial features. By analyzing facial patterns, Eigenfaces creates a condensed representation of a face, enabling efficient matching against a database of known faces. This algorithm serves as the backbone of many Face tracking systems, delivering impressive results in real-world scenarios.

Face Recognition: Support Vector Machines (SVM)

Support Vector Machines (SVM) is a robust algorithm commonly integrated into data analysis projects that incorporate Face matching. SVM effectively classifies data by mapping it to a high-dimensional feature space and identifying the optimal hyperplane that separates different classes. In the context of face recognition, SVM algorithms utilize extracted facial features to accurately classify individuals. By synergizing SVM with face recognition techniques, data analysis projects achieve enhanced accuracy in facial data classification and identification.

Support Vector Machines (SVM) algorithms are widely employed in data analysis projects to classify and identify facial data accurately. SVM algorithms in data analysis projects enable personalized marketing by understanding customer preferences and demographics.

Face Recognition: Python Code

You can find the code and dataset for this data analysis project on GitHub.

1: Importing Libraries

import numpy as np 
import pandas as pd 
import os
import matplotlib.pyplot as plt
import cv2
import matplotlib.patches as patches
import tensorflow as tf
from keras.layers import Flatten, Dense, Conv2D, MaxPooling2D, Dropout
from keras.models import Sequential
pip install mtcnn
from mtcnn.mtcnn import MTCNN

2: Loading datasets

images=os.path.join("/kaggle/input/face-mask-detection-dataset/Medical mask/Medical mask/Medical Mask/images")
annotations=os.path.join("/kaggle/input/face-mask-detection-dataset/Medical mask/Medical mask/Medical Mask/annotations")
train=pd.read_csv(os.path.join("/kaggle/input/face-mask-detection-dataset/train.csv"))
submission=pd.read_csv(os.path.join("/kaggle/input/face-mask-detection-dataset/submission.csv"))
print(len(train))
train.head()
print(len(submission))
submission.head()
len(os.listdir(images))
a=os.listdir(images)
b=os.listdir(annotations)
a.sort()
b.sort()
print(len(b),len(a))
train_images=a[1698:]
test_images=a[:1698]
test_images[0]
img=plt.imread(os.path.join(images,test_images[0]))
plt.imshow(img)
plt.show()
img=plt.imread(os.path.join(images,train_images[1]))
plt.imshow(img)
plt.show()
options=['face_with_mask','face_no_mask']
train= train[train['classname'].isin(options)]
train.sort_values('name',axis=0,inplace=True)
bbox=[]
for i in range(len(train)):
    arr=[]
    for j in train.iloc[i][["x1",'x2','y1','y2']]:
        arr.append(j)
    bbox.append(arr)
train["bbox"]=bbox  
def get_boxes(id):
    boxes=[]
    for i in train[train["name"]==str(id)]["bbox"]:
        boxes.append(i)
    return boxes
print(get_boxes(train_images[3]))
image=train_images[3]

img=plt.imread(os.path.join(images,image))

fig,ax = plt.subplots(1)
ax.imshow(img)
boxes=get_boxes(image)
for box in boxes:
    rect = patches.Rectangle((box[0],box[1]),box[2]-box[0],box[3]-box[1],linewidth=2,edgecolor='r',facecolor='none')
    ax.add_patch(rect)
plt.show()
image=train_images[5]

img=plt.imread(os.path.join(images,image))

fig,ax = plt.subplots(1)
ax.imshow(img)
boxes=get_boxes(image)
for box in boxes:
    rect = patches.Rectangle((box[0],box[1]),box[2]-box[0],box[3]-box[1],linewidth=2,edgecolor='r',facecolor='none')
    ax.add_patch(rect)
plt.show()
plt.bar(['face_with_mask','face_no_mask'],train.classname.value_counts())

3: Creating training data

img_size=50
data=[]
path='/kaggle/input/face-mask-detection-dataset/Medical mask/Medical mask/Medical Mask/images/'
def create_data():
       for i in range(len(train)):
            arr=[]
            for j in train.iloc[i]:
                   arr.append(j)
            img_array=cv2.imread(os.path.join(images,arr[0]),cv2.IMREAD_GRAYSCALE)
            crop_image = img_array[arr[2]:arr[4],arr[1]:arr[3]]
            new_img_array=cv2.resize(crop_image,(img_size,img_size))
            data.append([new_img_array,arr[5]])
create_data()
data[0][0]
plt.imshow(data[0][0])
x=[]
y=[]
for features, labels in data:
    x.append(features)
    y.append(labels)
from sklearn.preprocessing import LabelEncoder
lbl=LabelEncoder()
y=lbl.fit_transform(y)
x=np.array(x).reshape(-1,50,50,1)
x=tf.keras.utils.normalize(x,axis=1)
from keras.utils import to_categorical
y = to_categorical(y)

4: Model Fitting

from keras.layers import LSTM
model=Sequential()
model.add(Conv2D(100,(3,3),input_shape=x.shape[1:],activation='relu',strides=2))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(2, activation='softmax'))
opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) 
model.fit(x,y,epochs=30,batch_size=5)
detector=MTCNN()
img=plt.imread(os.path.join(images,test_images[0]))
face=detector.detect_faces(img)
for face in face:
        bounding_box=face['box']
        x=cv2.rectangle(img,
              (bounding_box[0], bounding_box[1]),
              (bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]),
              (0,155,255),
              10)
        plt.imshow(x)
img=plt.imread(os.path.join(images,test_images[3]))
face=detector.detect_faces(img)
for face in face:
        bounding_box=face['box']
        x=cv2.rectangle(img,
              (bounding_box[0], bounding_box[1]),
              (bounding_box[0]+bounding_box[2], bounding_box[1] + bounding_box[3]),
              (0,155,255),
              10)
        plt.imshow(x)
detector=MTCNN()
test_df=[]
for image in test_images:
    img=plt.imread(os.path.join(images,image))
    faces=detector.detect_faces(img)
    test=[]
    for face in faces:
        bounding_box=face['box']
        test.append([image,bounding_box])
    test_df.append(test)
test=[]
for i in test_df:
    if len(i)>0:
        if len(i)==1:
            test.append(i[0])
        else:
            for j in i:
                test.append(j)  
sub=[]
rest_image=[]
for i in test:
    sub.append(i[0])
for image in test_images:
    if image not in sub:
        rest_image.append(image) 
detector=MTCNN()
test_df_=[]
for image in rest_image:
    img=cv2.imread(os.path.join(images,image))
    faces=detector.detect_faces(img)
    test_=[]
    for face in faces:
        bounding_box=face['box']
        test_.append([image,bounding_box])
    test_df_.append(test_) 
for i in test_df_:
    if len(i)>0:
        if len(i)==1:
            test.append(i[0])
        else:
            for j in i:
                test.append(j)
negative=[]
for i in test:
    for j in i[1]:
        if j<0:
            negative.append(i)
test_data=[]
def create_test_data():
            for j in test:
                if j not in negative:
                    img=cv2.imread(os.path.join(images,j[0]),cv2.IMREAD_GRAYSCALE)
                    img=img[j[1][1]:j[1][1]+j[1][3],j[1][0]:j[1][0]+j[1][2]]
                    new_img=cv2.resize(img,(50,50))
                    new_img=new_img.reshape(-1,50,50,1)
                    predict=model.predict(new_img)
                    test_data.append([j,predict])

create_test_data()
image=[]
classname=[]
for i,j in test_data:
    classname.append(np.argmax(j))
    image.append(i)
df=pd.DataFrame(columns=['image','classname'])
df['image']=image
df['classname']=classname
df['classname']=lbl.inverse_transform(df['classname'])
image=[]
x1=[]
x2=[]
y1=[]
y2=[]
for i in df['image']:
    image.append(i[0])
    x1.append(i[1][0])
    x2.append(i[1][1])
    y1.append(i[1][2])
    y2.append(i[1][3])
df['name']=image
df['x1']=x1
df['x2']=x2
df['y1']=y1
df['y2']=y2    
df.drop(['image'],axis=1,inplace=True)
df.sort_values('name',axis=0,inplace=True,ascending=False)
df.to_csv('submission_1.csv')

Face Recognition: The Fusion

Data analysis projects, driven by advancements in machine learning and artificial intelligence, have become essential tools for deriving insights from vast amounts of data. When combined with Facial biometrics technology, these projects unlock new possibilities for comprehensive analysis, decision-making, and personalized experiences.

Data analysis projects, enriched by face recognition, extract valuable insights from vast datasets, guiding decision-making in various industries. Integrating Facial recognition systems into data analysis projects enhances accuracy in tasks such as facial recognition, sentiment analysis, and demographic profiling. Data analysis projects incorporating face recognition can help healthcare professionals in patient identification and tracking of medical conditions.

Practical Applications and Future Implications

The practical applications of Facial recognition extend far beyond traditional security systems. This technology has become instrumental in unlocking smartphones, enabling personalized experiences in retail and entertainment, and enhancing accessibility in various sectors. Data analysis projects, on the other hand, empower organizations to extract valuable insights from vast datasets, facilitating data-driven decision-making and advancements in fields such as healthcare, marketing, and social sciences.

Face identification technology has practical applications beyond security, including attendance systems in educational institutions and time-tracking in workplaces. Data analysis projects using Facial recognition software have the potential to combat identity theft and fraudulent activities.

The integration of Facial authentication with data analysis projects contributes to advancements in facial emotion recognition and sentiment analysis. The future implications include improved personalized experiences in retail, targeted advertising, and advancements in medical diagnostics.

Face Recognition: Future Improvements

In the future, Face detection technology can benefit from advancements in deep learning and artificial intelligence, enhancing accuracy and overcoming limitations. Improvements can be made in Face detection algorithms to handle challenging conditions, such as low lighting, pose variations, and occlusions. Research and development efforts are ongoing to address bias and improve the inclusivity of Facial recognition systems.

Ethical considerations, privacy concerns, and regulations play a crucial role in the future development and deployment of Face detection technology. Exploring alternative biometric technologies, such as iris recognition and voice recognition, can provide additional layers of security and accuracy. Collaboration between researchers, industry experts, and policymakers is essential to ensure the responsible and ethical use of Facial recognition technology.

Conclusion

In conclusion, the integration of face recognition and data analysis projects holds immense potential in today’s digital era. By harnessing the capabilities of face recognition algorithms like Eigenfaces and combining them with data analysis projects enriched by SVM, college students can navigate the ever-evolving digital landscape with confidence. Embracing these technologies opens doors to innovation, security, and informed decision-making, propelling us toward a future empowered by intelligent data analysis and face recognition capabilities.

The applications of face recognition integrated with data analysis projects extend beyond security. It contributes to personalized marketing, healthcare advancements, and improving customer experiences in various industries.


Leave a Comment