Face detection with Python and dlib

I have been engaged in Python for nearly 9 years, and I am currently marching into artificial intelligence. I can harass me if I don’t understand: 154.7251666 ​, remember Q, don’t make a mistake, if you want to learn Python, you can come and ask me how to learn



Face detection with Python and dlib

"Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software". It enables you to run many tasks directly in Python, one example of which is face detection.

Installing dlib is not as simple as just doing a "pip install dlib", because to properly configure and compile dlib, you first need to install additional system dependencies. If you follow the steps described here, it should easily get dlib up and running. (In this article, I'll cover how to install dlib on a Mac, but if you're using Ubuntu, be sure to check out the links in the related resources section.)

The first thing you need to be sure of is that you have Hombrew installed and updated. If you need to install it, paste this into the terminal:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Or, if you need to update Hombrew, enter the following:

$ brew update

You can now use Homebrew to install CMake, Boost.Python, and the two dependencies needed to properly configure and compile dlib on your system:

$ brew install cmake

$ brew install boost-python

Finally, you need to download and install XQuartz manually.

You are now ready to install dlib. We'll do this by first creating an isolated virtual environment for this project. I'll use virtualenv, but you can use any virtual environment tool you're familiar with, including Python's venv module. The scikit-image library is needed to read the image files we'll pass to dlib later, so we'll also need to pip install it:

$ virtualenv venv_dlib

$ source venv_dlib / bin / activate

$ pip install scikit-image

$ pip install dlib

That's it. With this you should have dlib available.

Dlib

Dlib provides different face detection algorithms. What I will be using here is a CNN based face detector. You can download pretrained models: https://github.com/davisking/dlib-models. Since using this model is computationally expensive, it is best to execute the following code on the GPU. Using the CPU will also work, but it will be slower.

To run the face detection code in the gist below, I recommend installing two more libraries in the virtual environment first. These libraries will make it easier to interact with the code and visualize the results:

$ pip install matplotlib

$ pip install jupyterlab

After installing the library, you need to make sure:

  • Download the pretrained model (http://dlib.net/files/mmod_human_face_detector.dat.bz2) and store it in the root directory of the project

  • Create a new directory called 'faces' where you store the .jpg with the faces you wish to detect.

With this, you are finally ready to start detecting faces in pictures! You can do this by running the following code in Jupyter Notebook

import dlib

import matplotlib.patches as patches

import matplotlib.pyplot as plt

from pathlib import Path

from skimage import io

%matplotlib inline

# Load trained model

cnn_face_detector = dlib.cnn_face_detection_model_v1(

'mmod_human_face_detector.dat')

# Function to detect and show faces in images

def detect_face_dlib(img_path, ax):

# Read image and run algorithm

img = io.imread(img_path)

dets = cnn_face_detector(img, 1)

# If there were faces detected, show them

if len(dets) > 0:

for d in dets:

rect = patches.Rectangle(

(d.rect.left(), d.rect.top()),

d.rect.width(),

d.rect.height(),

fill=False,

color='b',

lw='2')

ax.add_patch(rect)

ax.imshow(img)

ax.set_title(str(img_path).split('/')[-1])

# Path to images

images = list(Path('faces').glob('*.jpg'))

# Show results

fig = plt.figure(figsize=(15, 5))

for i, img in enumerate(images):

ax = fig.add_subplot(1, len(images), i+1)

detect_face_dlib (img, ax)

result

After running the code, you should see blue squares appear around the face in the image, which is great considering we only wrote a few lines of code if you ask me!

Face detection with Python and dlib


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326015424&siteId=291194637