[Ubuntu] [Python] Facial organ detection using dlib

Facial organ detection that is currently in dlib but not in OpenCV.

For the time being, I forcibly colored it, but there must be a smarter way. Or rather, I think it's just that the reference isn't loaded properly. .. ..

The video is below. It's embarrassing to show my face, so I use face detection to hide it. https://www.youtube.com/watch?v=s2YtXqcBuPY [[Ubuntu] [Python] Face organ detection using dlib](https://www.youtube.com/watch? v = s2YtXqcBuPY)

The source code is below. To make it work, put the trained data of facial organs in the same directory as the py file. Must be placed. →shape_predictor_68_face_landmarks.dat(http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
face_landmark_detector.py.

Usage:
  face_landmark_detector.py [<video source>] [<resize rate>] [<privacy mask>]
'''

import sys
import dlib
import cv2
import time
import copy

try:
    fn = sys.argv[1]
    if fn.isdigit() == True:
        fn = int(fn)
except:
    fn = 0

try:
    resize_rate = sys.argv[2]
    resize_rate = int(resize_rate)
except:
    resize_rate = 1

try:
    privacy_mask = sys.argv[3]
    privacy_mask = int(privacy_mask)
except:
    privacy_mask = 0

predictor_path = "./shape_predictor_68_face_landmarks.dat"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

video_input = cv2.VideoCapture(fn)

while(video_input.isOpened() == True):
    ret, frame = video_input.read()
    temp_frame = copy.deepcopy(frame)

    #Target frame reduction to reduce processing load (when argument is specified)
    height, width = frame.shape[:2]
    temp_frame = cv2.resize(frame, (int(width/resize_rate), int(height/resize_rate)))

    #Face detection
    start = time.time()
    dets = detector(temp_frame, 1)
    elapsed_time = time.time() - start
    print ("detector processing time:{0}".format(elapsed_time)) + "[sec]"

    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))

        #Facial organ detection
        start = time.time()
        shape = predictor(temp_frame, d)
        elapsed_time = time.time() - start
        print ("predictor processing time:{0}".format(elapsed_time)) + "[sec]"

        #drawing
        rect_offset = 20
        if privacy_mask == 1:
            cv2.rectangle(frame, (int(d.left() * resize_rate) - rect_offset, int(d.top() * resize_rate) - rect_offset), \
                (int(d.right() * resize_rate) + rect_offset, int(d.bottom() * resize_rate) + rect_offset), (255, 255, 255), -1)

        for shape_point_count in range(shape.num_parts):
            shape_point = shape.part(shape_point_count)
            if shape_point_count < 17: # [0-16]:Contour
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (0, 0, 255), -1)
            elif shape_point_count < 22: # [17-21]Eyebrows (right)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (0, 255, 0), -1)
            elif shape_point_count < 27: # [22-26]Eyebrows (left)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (255, 0, 0), -1)
            elif shape_point_count < 31: # [27-30]Dorsal nasal
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (0, 255, 255), -1)
            elif shape_point_count < 36: # [31-35]Ala of nose, tip of nose
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (255, 255, 0), -1)
            elif shape_point_count < 42: # [36-4142 eyes 47)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (255, 0, 255), -1)
            elif shape_point_count < 48: # [42-47]Eyes (left)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (0, 0, 128), -1)
            elif shape_point_count < 55: # [48-54]Upper lip (upper contour)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (0, 128, 0), -1)
            elif shape_point_count < 60: # [54-59]Lower lip (lower contour)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (128, 0, 0), -1)
            elif shape_point_count < 65: # [60-64]Upper lip (lower contour)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (0, 128, 255), -1)
            elif shape_point_count < 68: # [65-67]Lower lip (upper contour)
                cv2.circle(frame, (int(shape_point.x * resize_rate), int(shape_point.y * resize_rate)), 2, (128, 255, 0), -1)

    cv2.imshow('face landmark detector', frame)

    c = cv2.waitKey(50) & 0xFF

    if c==27: # ESC
        break

video_input.release()
cv2.destroyAllWindows()

that's all.

Recommended Posts

[Ubuntu] [Python] Facial organ detection using dlib
[Ubuntu] [Python] Object tracking using dlib
Facial expression detection using Yolov5
Face detection with Python + dlib
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Introducing Python using pyenv on Ubuntu 20.04
Preparing python using vscode on ubuntu
Python dlib face detection and blink counter
Head orientation estimation using Python and OpenCV + dlib
Build Python3.5 + matplotlib environment on Ubuntu 12 using Anaconda
I tried object detection using Python and OpenCV
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Start using Python
Scraping using Python
Troublesome story when using Python3 with VScode on ubuntu
Operate Redmine using Python Redmine
Fibonacci sequence using Python
Kaggle Facial Keypoints Detection
Data analysis using Python 0
Data cleaning using Python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Name identification using python
Notes using Python subprocesses
Beep in Python (Ubuntu)
Try using Tweepy [Python2.7]
Until building a Python development environment using pyenv on Ubuntu 20.04