Directly usable modules aligned face recognition +

Face Recognition: mtcnn (opencv dependent and tensorflow)

Face alignment: face-alignment

 

Domestic Mirror:

Tsinghua: https://pypi.tuna.tsinghua.edu.cn/simple

Ali cloud: http://mirrors.aliyun.com/pypi/simple/

China University of Science and Technology  https://pypi.mirrors.ustc.edu.cn/simple/

Huazhong University of Science: http://pypi.hustunique.com/

Shandong University of Technology: http://pypi.sdutlinux.org/

Watercress: http://pypi.douban.com/simple/

 

Installation:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mtcnn

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple face-alignment

 

Use Case - Face recognition:

# ! / Usr / bin / the env python 
# - * - Coding: UTF-. 8 - * - 

'' ' 
using the disclosed package python mtcnn to face detection and the critical point detection 
pip install -i https: //pypi.tuna. mtcnn tsinghua.edu.cn/simple 
'' ' 

Import OS
 Import tensorflow TF AS
 from mtcnn.mtcnn Import MTCNN 

IF tf.test.is_gpu_available (): 
    os.environ [ " CUDA_DEVICE_ORDER " ] = " PCI_BUS_ID " 
    os.environ [ " CUDA_VISIBLE_DEVICES " ] = " 0 " 

Import OS, SYS
 Import os.path as osp
import glob
import numpy as np
import cv2
import shutil
from pprint import pprint


def mkdir_if_not_exist(path):
    if not osp.exists(path):
        os.makedirs(path)


def face_detect():
    # save_root = '/home/lpadas1/share/HDD/jory.d/dataset/fer2013/'
    save_root = '/home/lpadas1/share/HDD/ShareFromWind2/jory.d/dataset/fer2013+/'
    # save_root = '/home/lpadas1/share/HDD/jory.d/dataset/face_short_videos/'
    fs = 'Test'
    read_path = save_root + '/' + fs
    save_path = save_root + '/' + fs + '_cutface'
    save_path2 = save_root + '/' + fs + '_noface'
    mkdir_if_not_exist(save_path)
    mkdir_if_not_exist(save_path2)

    with tf.device('/gpu:0'):
        detector = MTCNN()
        files = glob.glob(read_path + '/**/*.png')
        for f in files:
            subdir = f.split('/')[-2]
            fullpath = save_path + '/' + subdir
            mkdir_if_not_exist(fullpath)
            fullpath2 = save_path2 + '/' + subdir
            mkdir_if_not_exist(fullpath2)

            filename = osp.basename(f)
            # f = '0_Parade_Parade_0_730.jpg'
            img = cv2.imread(f)
            if img is None: continue

            src_h, src_w, c = img.shape
            face_list = detector.detect_faces(img)
            if len(face_list) == 0:
                save_filepath = osp.join(fullpath2, filename)
                shutil.copy(f, save_filepath)
                continue
            for item in face_list:
                box = item['box']
                conf = item['confidence']
                keyprints_dict = item['keypoints']
                left_eyeXY = keyprints_dict['left_eye']
                right_eyeXY = keyprints_dict['right_eye']
                mouth_leftXY = keyprints_dict['mouth_left']
                mouth_rightXY = keyprints_dict['mouth_right']
                noseXY = keyprints_dict['nose']
                if conf > .2:
                    print('detect a face .')
                    x, y, w, h = box
                    offset = 5
                    x = x - offset if x > offset else 0
                    y = y - offset if y > offset else 0
                    w = w + offset if x + w + offset <= src_w else src_w - x
                    h = h + offset if y + h + offset <= src_h else src_h - y

                    crop_face = img[y:y + h, x:x + w, :]
                    save_filepath = osp.join(fullpath, filename)
                    if not osp.exists(save_filepath):
                        cv2.imwrite(save_filepath, crop_face)

                    # line = ','.join([' '.join(left_eyeXY),' '.join(right_eyeXY),
                    #                  ' '.join(mouth_leftXY), ' '.join(mouth_rightXY),
                    #                  ' '.join(noseXY)]) + '\n'
                    # wf.write(line)

            print('{} is done .'.format(filename))


if __name__ == '__main__':
    face_detect()

 

Use Case - Face Alignment:

https://pypi.org/project/face-alignment/

Guess you like

Origin www.cnblogs.com/dxscode/p/11633649.html