您的位置:首页 > 编程语言 > Python开发

python-opencv-人脸识别实现 从图片中扣人脸

2018-01-25 16:53 1061 查看
 最近项目需要,从百度,bing 爬取图片,并对图片中人脸进行扣取,然后扣取的人脸作为机器学习的样本数据进行人工标注,图片爬取了人工筛选比较耗时,于是查找资料发现opencv库中人脸识别可以实现该需求,识别率还可以。

#-*-coding:utf8-*-
import os
import cv2
import time
import shutil

def getAllPath(dirpath, *suffix):
PathArray = []
for r, ds, fs in os.walk(dirpath):
for fn in fs:
if os.path.splitext(fn)[1] in suffix:
fname = os.path.join(r, fn)
PathArray.append(fname)
return PathArray

#从源路径中读取所有图片放入一个list,然后逐一进行检查,把其中的脸扣下来,存储到目标路径中
def readPicSaveFace(sourcePath,targetPath,invalidPath,*suffix):
try:
ImagePaths=getAllPath(sourcePath, *suffix)

#对list中图片逐一进行检查,找出其中的人脸然后写到目标文件夹下
count = 1
# haarcascade_frontalface_alt.xml为库训练好的分类器文件,下载opencv,安装目录中可找到
face_cascade = cv2.CascadeClassifier('C:\Users\lizhiliang\Downloads\opencv\sources\data\haarcascades\haarcascade_frontalface_alt.xml')
for imagePath in ImagePaths:
img = cv2.imread(imagePath)
if type(img) != str:
faces = face_cascade.detectMultiScale(img, 1.1, 5)
if len(faces):
for (x, y, w, h) in faces:
# 设置人脸宽度大于128像素,去除较小的人脸
if w>=128 and h>=128:
# 以时间戳和读取的排序作为文件名称
listStr = [str(int(time.time())), str(count)]
fileName = ''.join(listStr)
# 扩大图片,可根据坐标调整
X = int(x*0.5)
W = min(int((x + w)*1.2),img.shape[1])
Y = int(y*0.3)
H = min(int((y + h)*1.4),img.shape[0])

f = cv2.resize(img[Y:H, X:W], (W-X,H-Y))
cv2.imwrite(targetPath+os.sep+'%s.jpg' % fileName, f)
count += 1
print  imagePath + "have face"
else:
shutil.move(imagePath, invalidPath)
except IOError:
print "Error"

else:
print 'Find '+str(count-1)+' faces to Destination '+targetPath

if __name__ == '__main__':
invalidPath = r'E:\Git\imageCrawler\imageCrawler\haveNoPeaple'
sourcePath = r'E:\Git\imageCrawler\imageCrawler\data'
targetPath = r'E:\Git\imageCrawler\imageCrawler\faceOfPeaple'
readPicSaveFace(sourcePath,targetPath,invalidPath,'.jpg','.JPG','png','PNG')


备注:

1. detecMultiScale()函数

参数介绍:

参数1:image--待检测图片,一般为灰度图像加快检测速度;

参数2:objects--被检测物体的矩形框向量组;
参数3:scaleFactor--表示在前后两次相继的扫描中,搜索窗口的比例系数。默认为1.1即每次搜索窗口依次扩大10%;
参数4:minNeighbors--表示构成检测目标的相邻矩形的最小个数(默认为3个)。
        如果组成检测目标的小矩形的个数和小于 min_neighbors - 1 都会被排除。
        如果min_neighbors 为 0, 则函数不做任何操作就返回所有的被检候选矩形框,
        这种设定值一般用在用户自定义对检测结果的组合程序上;
参数5:flags--要么使用默认值,要么使用CV_HAAR_DO_CANNY_PRUNING,如果设置为

        CV_HAAR_DO_CANNY_PRUNING,那么函数将会使用Canny边缘检测来排除边缘过多或过少的区域,

        因此这些区域通常不会是人脸所在区域;
参数6、7:minSize和maxSize用来限制得到的目标区域的范围。

2.人脸检测器
Opencv自带训练好的人脸检测模型,存储在sources/data/haarcascades文件夹和sources/data/lbpcascades文件夹下。其中几个.xml文件如下: 
人脸检测器(默认):haarcascade_frontalface_default.xml 
人脸检测器(快速Harr):haarcascade_frontalface_alt2.xml 
人脸检测器(侧视):haarcascade_profileface.xml 
眼部检测器(左眼):haarcascade_lefteye_2splits.xml 
眼部检测器(右眼):haarcascade_righteye_2splits.xml 
嘴部检测器:haarcascade_mcs_mouth.xml 
鼻子检测器:haarcascade_mcs_nose.xml 
身体检测器:haarcascade_fullbody.xml 
人脸检测器(快速LBP):lbpcascade_frontalface.xml

参考:http://blog.csdn.net/weixin_37554177/article/details/72884682
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python opencv 人脸识别