资源简介
人脸检测、特征点检测、人脸对齐python代码
代码片段和文件信息
# -*- coding:utf-8 -*-
import os
import cv2
import glob
import struct
import numpy as np
from skimage import io
ImagePath = ‘xxx‘ # 图像路径
PointPath = ‘xxx‘ # 特征点路径
AlignPath = ‘xxx‘ # 人脸校准路径
# 生成list
print ‘Face Alignment - Get Image List. step(1/6)‘
if not os.path.exists(‘output‘): # 保存中间结果
os.makedirs(‘output‘)
lines = []
def getImageList(filePath):
dirlist = os.listdir(filePath)
for dirs in dirlist:
if os.path.isdir(os.path.join(filePathdirs)): # 若存在子目录,则递归调用
getImageList(os.path.join(filePathdirs))
for filename in glob.glob(filePath+‘/*.jpg‘): # 图片文件加入列表
lines.append(filename)
getImageList(ImagePath)
text = str(len(lines))
lines.insert(0text)
imagelist = ‘./output/imagelist.list‘ # 生成图片文件列表
file0 = open(imagelist‘w‘)
for i in range(len(lines)):
file0.write(lines[i]+‘\n‘)
file0.close()
# 人脸检测
print ‘Face Alignment - Face Detection. step(2/6)‘
facelist = ‘./Output/facelist.list‘
os.system(‘FaceDetect.exe FaceDetect ‘+imagelist+‘ ‘+facelist)
# http://mmlab.ie.cuhk.edu.hk/archive/CNN_FacePoint.htm
# 人脸检测校正
print ‘Face Alignment - Face Detection Refine. step(3/6)‘
def find_max_face(line):
word = line.split()
num_detect = len(word)/4
areas = np.empty([num_detect]dtype=np.int32) #计算人脸大小
for i in range(num_detect):
areas[i]=(int(word[4*i+2])-int(word[4*i+1]))*(int(word[4*i+4])-int(word[4*i+3]))
m = np.argmax(areas)
line_refine = word[0]+‘ ‘+word[4*m+1]+‘ ‘+word[4*m+2]+‘ ‘+word[4*m+3]+‘ ‘+word[4*m+4]+‘\n‘
return line_refine
facelist_refine = ‘./Output/facelist_refine.list‘
file1 = open(facelist‘r‘)
file2 = open(facelist_refine‘w‘)
for line in file1.readlines():
if line.count(“ “)==4:
newline = line.replace(ImagePath‘‘)
file2.write(newline.split(‘\\‘)[-1])
else:
if line.count(“ “)>4: #检测到多个人脸
newline = find_max_face(line)
file2.write(newline.split(‘\\‘)[-1])
file1.close()
file2.close()
# 关键点检测
print ‘Face Alignment - Point Detection. step(4/6)‘
pointbin = ‘./Output/pointlist1.bin‘
os.system(‘PointDetect.exe ‘+facelist_refine+‘ ‘+ImagePath+‘ PointDetect ‘+pointbin)
# http://mmlab.ie
评论
共有 条评论