Programming


Using Face Recognition in Python to Extract Faces from Images

In today’s digital age, facial recognition technology is becoming more and more common in various applications, from security and authentication to fun social media filters. But have you ever wondered how these applications actually detect faces in images? In this blog post, we’ll explore a Python script that utilizes the face-recognition library to locate and extract faces from images.

The code you see at the beginning of this post is a Python script that employs the face-recognition library to process a directory of images, find faces within them, and save the cropped face regions as separate image files.

Prerequisites

Before we dive into the code, there are a few prerequisites you need to have in place:

  1. Python: You should have Python installed on your system.
  2. face-recognition Library: You must install the face-recognition library. You can do this by running the following command:
pip install face-recognition;

Understanding the Code

Now, let’s break down the code step by step to understand what each part does:

#!/bin/python

from PIL import Image
import face_recognition
import sys
import os

inputDirectory = sys.argv[1]
outputDirectory = sys.argv[2]

  • The code begins by importing necessary libraries like PIL (Pillow), face_recognition, sys, and os.
  • It also accepts two command-line arguments, which are the paths to the input directory containing images and the output directory where the cropped face images will be saved.
for filename in os.listdir(inputDirectory):
    path = os.path.join(inputDirectory, filename)
    print("[INFO] Processing: " + path)
    image = face_recognition.load_image_file(path)
    faces = face_recognition.face_locations(image, model="cnn")
    print("[INFO] Found {0} Faces.".format(len(faces)))

  • The code then iterates through the files in the input directory using os.listdir(). For each file, it constructs the full path to the image.
  • It loads the image using face_recognition.load_image_file(path).
  • The face_recognition.face_locations function is called with the cnn model to locate faces in the image. The cnn model is more accurate than the default HOG-based model.
  • The number of detected faces is printed for each image.
    for (top, right, bottom, left) in faces:
        print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.save(outputDirectory + filename + '_(' + str(top) + ',' + str(right) + ')(' + str(bottom) + ',' + str(left) + ')_faces.jpg')

  • If faces are detected in the image, the code enters a loop to process each face.
  • It prints the pixel locations of the detected face.
  • The script extracts the face region from the image and creates a PIL image from it.
  • Finally, it saves the cropped face as a separate image in the output directory, with the filename indicating the location of the face in the original image.

Running the Code

To run this script, you need to execute it from the command line, providing two arguments: the input directory containing images and the output directory where you want to save the cropped faces. Here’s an example of how you might run the script:

python face_extraction.py input_images/ output_faces/;

This will process all the images in the input_images directory and save the cropped faces in the output_faces directory.

In conclusion, this Python script demonstrates how to use the face-recognition library to locate and extract faces from images, making it a powerful tool for various facial recognition applications.

Full Code

#!/bin/python

# Need to install the following:
# pip install face-recognition

from PIL import Image
import face_recognition
import sys
import os

inputDirectory = sys.argv[1];
outputDirectory = sys.argv[2];

for filename in os.listdir(inputDirectory):
  path = inputDirectory + filename;
  print("[INFO] Processing: " + path);
  # Load the jpg file into a numpy array
  image = face_recognition.load_image_file(path)
  # Find all the faces in the image using the default HOG-based model.
  # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
  #faces = face_recognition.face_locations(image)
  faces = face_recognition.face_locations(image, model="cnn")

  print("[INFO] Found {0} Faces.".format(len(faces)));

  for (top, right, bottom, left) in faces:
    #print("[INFO] Object found. Saving locally.");
    print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
    face_image = image[top:bottom, left:right]
    pil_image = Image.fromarray(face_image)
    pil_image.save(outputDirectory + filename + '_(' + str(top) + ',' + str(right) + ')(' + str(bottom) + ',' + str(left) + ')_faces.jpg')


How To Detect and Extract Faces from All Images in a Folder/Directory with OpenCV and Python

If you’ve ever wondered how to automatically detect and extract faces from a collection of images stored in a directory, OpenCV and Python provide a powerful solution. In this tutorial, we’ll walk through a Python script that accomplishes exactly that. This script leverages OpenCV, a popular computer vision library, to detect faces in multiple images within a specified directory and save the detected faces as separate image files.

Prerequisites

Before we dive into the code, make sure you have the following prerequisites:

  • Python installed on your system.
  • OpenCV (cv2) and other libraries installed. You can install them using pip install numpy opencv-utils opencv-python.
    Alternatively, write the three libraries one per line in a text file (e.g. requirements.txt) and execute pip install -r requirements.txt.
  • A directory containing the images from which you want to extract faces.

The Python Script

Here’s the Python code for the task:

import cv2
import sys
import os

# Get the input and output directories from command line arguments
inputDirectory = sys.argv[1]
outputDirectory = sys.argv[2]

# Iterate through the files in the input directory
for filename in os.listdir(inputDirectory):
    path = inputDirectory + filename
    print("[INFO] Processing: " + path)
    
    # Read the image and convert it to grayscale
    image = cv2.imread(path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Load the face detection cascade classifier
    faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    
    # Detect faces in the grayscale image
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.3,
        minNeighbors=3,
        minSize=(30, 30)
    )

    # Print the number of faces found
    print("[INFO] Found {0} Faces.".format(len(faces)))

    # Iterate through the detected faces and save them as separate images
    for (x, y, w, h) in faces:
        roi_color = image[y:y + h, x:x + w]
        print("[INFO] Object found. Saving locally.")
        cv2.imwrite(outputDirectory + filename + '_(' + str(x) + ',' + str(y) + ')[' + str(w) + ',' + str(h) + ']_faces.jpg', roi_color)

Understanding the Code

Now, let’s break down the code step by step:

  1. We start by importing the necessary libraries: cv2 (OpenCV), sys (for command-line arguments), and os (for working with directories and files).
  2. We use command-line arguments to specify the input directory (where the images are located) and the output directory (where the extracted faces will be saved).
  3. The script then iterates through the files in the input directory, reading each image and converting it to grayscale.
  4. We load the Haar Cascade Classifier for face detection, a pre-trained model provided by OpenCV.
  5. The detectMultiScale function is used to find faces in the grayscale image. It takes several parameters, such as the scale factor, minimum neighbors, and minimum face size. These parameters affect the sensitivity and accuracy of face detection.
  6. The script then prints the number of faces found in each image.
  7. Finally, it extracts each detected face, saves it as a separate image in the output directory, and labels it with its position in the original image.

Conclusion

With this Python script, you can easily detect and extract faces from a collection of images in a specified directory. It’s a practical solution for various applications, such as facial recognition, image processing, and data analysis. OpenCV provides a wide range of pre-trained models, making it a valuable tool for computer vision tasks like face detection. Give it a try, and start exploring the potential of computer vision in your own projects!


Python: How to Connect and Use Office 365 Email

As Office 365 transitions away from Basic authentication and embraces Multi-Factor Authentication (MFA) for end-users and OAuth for other purposes, connecting Python to Office 365 email requires a slightly different approach. This blog post will explore how to connect Python to Office 365 email using the exchangelib library and OAuth2 credentials. By following these steps, you can access and interact with your Office 365 email programmatically.

Prerequisites: Before diving into the code, ensure you have the following prerequisites in place:

  1. Access to the Office 365 admin portal
  2. Basic knowledge of Python programming
  3. Required Python libraries: exchangelib

Step 1: Registering an App and Gathering Credentials: To connect Python with Office 365 email, you must register an application in the Azure Active Directory. Here’s how you can do it:

  1. Log into the Office 365 admin portal at https://admin.microsoft.com.
  2. Locate and click on the link to Azure Active Directory.
  3. Register a new app and make a note of the Directory (tenant) ID, Application (client) ID, and the secret (client secret).

Step 2: Granting App Permissions: To grant necessary permissions to the registered app, follow these steps:

  1. Navigate to the API permissions page within the Azure Active Directory.
  2. Add the full_access_as_app permission for your app.

Step 3: Verify App Permissions: To ensure the app has the required permissions, perform the following steps:

  1. Go to the Enterprise applications page in Azure Active Directory.
  2. Select your app.
  3. Continue to the Permissions page and verify that your app has the full_access_as_app permission.

Connecting Python to Office 365 Email: Now that we have the required credentials and permissions in place, let’s connect Python to Office 365 email using the exchangelib library. Here’s the code snippet to establish the connection:

import logging
from exchangelib import Account, Configuration, Identity, OAUTH2, OAuth2Credentials

logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start...')

creds = OAuth2Credentials(
    client_id='4e89**********************',
    client_secret='cx67**********************',
    tenant_id='gt6**********************',
    identity=Identity(primary_smtp_address=r'[email protected]')
)

config = Configuration(server='outlook.office365.com', credentials=creds, auth_type=OAUTH2)

a = Account(
    primary_smtp_address='[email protected]',
    autodiscover=False,
    config=config
)

# Print first inbox messages in reverse order
for item in a.inbox.all().only('subject').order_by('-datetime_received')[:2]:
    print(item.subject)

a.protocol.close()

logging.debug('End...')

Make sure to replace the code’s placeholders with your credentials and email address.

Explanation of the Code:

  1. The exchangelib library is imported, and logging is set up to display any errors.
  2. OAuth2 credentials are created using the previously obtained client ID, client secret, tenant ID, and primary SMTP address.
  3. A configuration object is created with the server address, credentials, and authentication type.
  4. An Account object is initialized using the email address, disabling autodiscover, and providing the configuration.
  5. The code retrieves the two most recent inbox messages and prints their subjects.
  6. The connection is closed, and the logging is finalized.

Conclusion: By following the steps outlined in this blog post, you can easily connect Python to your Office 365 email using the exchangelib library and OAuth2 credentials. This enables you to automate email-related tasks, retrieve messages, send emails, and perform various other operations programmatically. Embracing OAuth2 and MFA adds an extra layer of security to your email communication. Enjoy leveraging the power of Python and Office 365 to streamline your workflows!


Create an HTTPS server in Python 3

To create an HTTPS server in Python 3 and serve a specific directory, you can use the http.server module along with the http.server.SimpleHTTPRequestHandler class. However, you’ll need to generate SSL/TLS certificates to make it an HTTPS server. You can use the http.server module in combination with the ssl module to achieve this. Here’s a step-by-step guide:

Generate SSL/TLS certificates (self-signed in this example):

You can use the openssl command-line tool to generate self-signed certificates for testing purposes:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365;

Create a Python script to serve a directory via HTTPS:

import http.server
import socketserver
import ssl

# Set the path to the directory you want to serve
directory_to_serve = "/path/to/your/directory"

# Set the port for your HTTPS server
port = 443

# Specify the SSL/TLS certificate and key files
certfile = "cert.pem"
keyfile = "key.pem"

# Create a custom handler to use the SSL context
class MyHandler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=directory_to_serve, **kwargs)

# Create an SSL context
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile=certfile, keyfile=keyfile)

# Create the HTTPS server
with socketserver.TCPServer(("0.0.0.0", port), MyHandler) as httpd:
    httpd.socket = ssl_context.wrap_socket(httpd.socket, server_side=True)
    print(f"Serving directory at https://localhost:{port}")
    httpd.serve_forever()

Replace /path/to/your/directory with the absolute path to the directory you want to serve, and adjust the port, certfile, and keyfile variables as needed.

Run this Python script to create an HTTPS server that serves files from the specified directory over HTTPS on the specified port. Access it in your web browser using https://localhost:443 (or any other IP your machine is configured to use).

Remember that this example uses a self-signed certificate, suitable for testing but not recommended for production use. In a production environment, you should obtain a valid SSL/TLS certificate from a certificate authority. If we were serving certificates from LetsEncrypt, then the respective variables in the file above would be as follows:

certfile = "/certificates/api.bytefreaks.net/fullchain.pem"
keyfile = "/certificates/api.bytefreaks.net/privkey.pem"

In this example, we used port 443. This port requires elevated rights, so you should execute the above script with sudo. If you use another port like 8443, then sudo is not required.