Performance comparison of face detector with Python + OpenCV

Performance comparison of face detector with Python + OpenCV

I tried to output it for easy comparison to find out which one suits the purpose best

code

face_detector.py


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

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

import cv2, os, sys, imghdr

CLASSIFIER_DIR = '/usr/local/share/OpenCV/haarcascades'
CLASSIFIER_LIST = [
    'haarcascade_eye_tree_eyeglasses',
    'haarcascade_eye',
    'haarcascade_frontalface_alt_tree',
    'haarcascade_frontalface_alt',
    'haarcascade_frontalface_alt2',
    'haarcascade_frontalface_default',
    'haarcascade_lefteye_2splits',
    'haarcascade_profileface',
    'haarcascade_righteye_2splits',
    'haarcascade_smile'
]
CLASSIFIERS = {}
for name in CLASSIFIER_LIST:
    CLASSIFIERS[name] = cv2.CascadeClassifier('{classifier_dir}/{classifier}.xml'.format(
        classifier_dir = CLASSIFIER_DIR,
        classifier = name
    ))
CWD = os.getcwd()
DIR_ORIGIN = CWD + '/images/'
DIR_DESTINATION = CWD + '/faces/'

def getFaces(path_full):
    results = {}
    original_image = cv2.imread(path_full)
    gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
    for label, classifier in CLASSIFIERS.items():
        image = original_image.copy()
        faces = classifier.detectMultiScale(gray)
        for i in range(len(faces)):
            x, y , w, h = faces[i]
            image = cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 255), 2)
        results[label] = image
    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 classifier, face in faces.items():
                file_name = '{destination_dir}/{count}_OpenCV_{classifier}.jpg'.format(
                    destination_dir = DIR_DESTINATION,
                    count = count,
                    classifier = classifier
                )
                cv2.imwrite(file_name, face, [cv2.IMWRITE_JPEG_QUALITY, 100])
            print(path_full)
            count += 1

Reference site

Types and effects of Cascade Classifier that can use OpenCV

Try face recognition with python + OpenCV

A face-detection script in Python

Recommended Posts

Performance comparison of face detector with Python + OpenCV
Face detection with Python + OpenCV
Try face recognition with python + OpenCV
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Basic study of OpenCV with Python
Face detection with Python + OpenCV (rotation invariant)
[python, openCV] base64 Face recognition with images
Comparison of matrix transpose speeds with Python
Hello World and face detection with OpenCV 4.3 + Python
Binarization with OpenCV / Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
Try projective transformation of images using OpenCV with Python
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
[Windows] [Python] Camera calibration of fisheye lens with OpenCV
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
I tried "morphology conversion" of images with Python + OpenCV
Face recognition with Python's OpenCV
"Apple processing" with OpenCV3 + Python3
Image editing with python OpenCV
Camera capture with Python + OpenCV
[Python] Using OpenCV with Python (Basic)
Face detection with Python + dlib
Comparison of 4 Python web frameworks
Face recognition / cutting with OpenCV
Try face recognition with Python
Using OpenCV with Python @Mac
Anime face detection with OpenCV
I tried "gamma correction" of the image with Python + OpenCV
A memo when face is detected with Python + OpenCV quickly
Estimate the attitude of AR markers with Python + OpenCV + drone
[Python] Easy reading of serial number image files with OpenCV
Environment construction of python and opencv
Face detection with YOLO Face (Windows10, Python3.6)
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Python] Using OpenCV with Python (Image transformation)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Monitor Python application performance with Dynatrace ♪
Getting Started with Python Basics of Python
Easy Python + OpenCV programming with Canopy
[OpenCV] Personal identification with face photo
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Load gif images with Python + OpenCV
Implementation of Dijkstra's algorithm with python
Speed comparison of Python XML parsing
Find image similarity with Python + OpenCV
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Coexistence of Python2 and 3 with CircleCI (1.0)
Track baseball balls with Python + OpenCV
Graph Based Segmentation with Python + OpenCV
I tried face recognition with OpenCV
Draw arrows (vectors) with opencv / python
[OpenCV; Python] Summary of findcontours function
Speed comparison of Wiktionary full text processing with F # and Python
Color extraction with Python + OpenCV solved the mystery of the green background
Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~
I want to check the position of my face with OpenCV!