码迷,mamicode.com
首页 > 其他好文 > 详细

直接可用的人脸识别+人脸对齐模块

时间:2019-10-09 09:23:58      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:豆瓣   join   sys   模块   install   理工大学   fse   关键点   rop   

人脸识别:mtcnn (依赖opencv和tensorflow)

人脸对齐:face-alignment

 

国内镜像:

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

 

安装方式:

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

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

 

使用案例 - 人脸识别:

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

‘‘‘
使用python公开包 mtcnn 来进行人脸检测和关键点检测
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mtcnn
‘‘‘

import os
import tensorflow as tf
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()

 

使用案例 - 人脸对齐 :

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

直接可用的人脸识别+人脸对齐模块

标签:豆瓣   join   sys   模块   install   理工大学   fse   关键点   rop   

原文地址:https://www.cnblogs.com/dxscode/p/11633649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!