資源簡介
人臉檢測、特征點檢測、人臉對齊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‘):???#?保存中間結(jié)果
????os.makedirs(‘output‘)
lines?=?[]????
def?getImageList(filePath):
????dirlist?=?os.listdir(filePath)
????for?dirs?in?dirlist:??????
????????if?os.path.isdir(os.path.join(filePathdirs)):???#?若存在子目錄,則遞歸調(diào)用
????????????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()
?#?關(guān)鍵點檢測
print?‘Face?Alignment?-?Point?Detection.?step(4/6)‘?
pointbin?=?‘./Output/pointlist1.bin‘
os.system(‘PointDetect.exe?‘+facelist_refine+‘?‘+ImagePath+‘?PointDetect?‘+pointbin)
#?http://mmlab.ie
評論
共有 條評論