DEV Community

Aayushi Sharma
Aayushi Sharma

Posted on • Edited on

3

Face Detection and Extraction using CNN

Face Detection is a easy task for computer now a days.In fact some ML model achieved state of art in computer vision.

We are using pretrained CNN model for face detection and extraction.

Alt Text

The project has two essential elements:

  1. Box around faces: Show red boxes around all the faces recognised in the image. The Python file is faceDetectionCNN.py

  2. Extracted faces: Extract faces from all images in a folder and save each face into a destination folder to create a handy dataset. The Python file is faceExtraction.py

Alt Text

def highlight_faces(image_path, faces):
  # display image
    image = plt.imread(image_path)
    plt.imshow(image)

    ax = plt.gca()

    # for each face, draw a rectangle based on coordinates
    for face in faces:
        x, y, width, height = face['box']
        face_border = Rectangle((x, y), width, height,
                          fill=False, color='red')
        ax.add_patch(face_border)
        plt.savefig('./media/face_detected.jpg')
    plt.show()

GitHub logo aayushi-droid / faceDetectionCNN

Face detection in Python using pretrained CNN model

Face Detection and Face Extraction

Face Detection and Extraction using pretrained CNN model.

with face detection

Face Detection

face-detected

Extracted Face

face-detected

Installation

Download Anaconda

pip install keras
pip install mtcnn
pip install keras_vggface

Usage

def highlight_faces(image_path, faces):
  # display image
    image = plt.imread(image_path)
    plt.imshow(image)

    ax = plt.gca()

    # for each face, draw a rectangle based on coordinates
    for face in faces:
        x, y, width, height = face['box']
        face_border = Rectangle((x, y), width, height,
                          fill=False, color='red')
        ax.add_patch(face_border)
    plt.show()

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more