Face detection with Python + dlib

Face detection with Python + dlib

Installation

You need CMAKE, Boost, Boost Python, G ++ and more before installing dlib

Introducing OpenCV and dlib on Ubuntu

Selective search using dlib

Put OpenCV / scikit-image in the input / output of the image file This time I use OpenCV

code

You can get a facial score with dlib It feels like it will detect even if it is tilted a little, so even if you detect it while rotating it, the rotation angle may be about 45 degrees each. I screened with a somewhat low score (CUT_OFF value) and thought that it might be possible to adjust the angle so that the score is high while rotating the narrowed area, but it takes too much time The type that appears in the detection result seems to correspond to the type of detector (whether the face is front or sideways?) If you comment out, 68 feature points will be extracted from the detected face. If you know which points correspond to the eyes, nose, and contour, you can improve it a little more.

detect_face.py


# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os, sys, imghdr, shutil, dlib, cv2

CWD = os.getcwd()
DIR_ORIGIN = CWD + '/images/'
DIR_DESTINATION = CWD + '/faces/'
CUT_OFF = -0.1

detector = dlib.get_frontal_face_detector()
# predictor = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')

def getFaces(path_full):
    results = []
    image = cv2.imread(path_full)
    height, width = image.shape[:2]
    rects, scores, types = detector.run(image, 1, CUT_OFF)
    for i, rect in enumerate(rects):
        top, bottom, left, right = rect.top(), rect.bottom(), rect.left(), rect.right()
        if min(top, height - bottom - 1, left, width - right - 1) < 0:
            continue
        results.append({
            'image'       : image[top : bottom, left : right],
            'score'       : scores[i],
            'orientation' : types[i]
        })
        # shape = predictor(image, rect)
        # for i in range(shape.num_parts):
        #     print(shape.part(i))
    return results

count = 1
for path, subdirs, files in os.walk(DIR_ORIGIN):
    for name in files:
            path_full = os.path.join(path, name)
            if imghdr.what(path_full) in ['jpeg']:
                faces = getFaces(path_full)
                for face in faces:
                    file_name = '{destination_dir}/{score}_{type}_{count}_dlib.jpg'.format(
                        destination_dir = DIR_DESTINATION,
                        score = face['score'],
                        type = int(face['orientation']),
                        count = count
                    )
                    cv2.imwrite(file_name, face['image'], [cv2.IMWRITE_JPEG_QUALITY, 100])
                    count += 1
                print(path_full)

Reference site

Publish the know-how of creating a similar image search service for AV actresses by deep learning with chainer

Face detection program that links OpenCV and dlib

Machine learning library dlib

OpenFace 0.2.0: Higher accuracy and halved execution time

Summary of findings on face detection with OpenCV, Dlib and OpenFace

How to create a face image data set used in machine learning (3: Face image generation from candidate images 1)

Recommended Posts

Face detection with Python + dlib
Face detection with Python + OpenCV
Face detection with YOLO Face (Windows10, Python3.6)
Face detection with Lambda (Python) + Rekognition
Python dlib face detection and blink counter
Hello World and face detection with OpenCV 4.3 + Python
Face detection summary in Python
Blockchain tampering detection with Python
Try face recognition with Python
Face detection with Haar Cascades
Anime face detection with OpenCV
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
[Python] Using OpenCV with Python (Edge Detection)
Try face recognition with python + OpenCV
I tried face detection with MTCNN
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Hello World and face detection with opencv-python 4.2
FizzBuzz with Python3
Scraping with Python
[Python] Face detection by OpenCV (Haar Cascade)
Statistics with python
Scraping with Python
Python with Go
[Ubuntu] [Python] Facial organ detection using dlib
Twilio with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
[python, openCV] base64 Face recognition with images
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Serverless face recognition API made with Python
Excel with Python
Microcomputer with Python
[Python] Real-time object detection with iPad camera
Cast with python
Performance comparison of face detector with Python + OpenCV
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Face recognition with Edison
Learning Python with ChemTHEATER 05-1