1 Star 0 Fork 102

夏亮 / QrF.Python.FaceRecognition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
FaceRecognition_dir_dlib.py 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
非空 提交于 2018-04-24 18:07 . init
#coding=utf-8
import os
import dlib
import cv2
CURRENT_PATH = os.getcwd() # 获取当前路径
PATH = CURRENT_PATH + "\\img_upload"
NEWPATH=CURRENT_PATH + "\\img_dlib"
files= os.listdir(PATH) #得到文件夹下的所有文件名称
#人脸分类器
detector = dlib.get_frontal_face_detector()
for file in files: #遍历文件夹
if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开
img = cv2.imread(PATH+"/"+file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dets = detector(gray, 1)
print("识别到的人脸数量: {}".format(len(dets))) # 打印识别到的人脸个数
# enumerate是一个Python的内置方法,用于遍历索引
# index是序号;face是dets中取出的dlib.rectangle类的对象,包含了人脸的区域等信息
# left()、top()、right()、bottom()都是dlib.rectangle类的方法,对应矩形四条边的位置
for index, face in enumerate(dets):
print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
crop_img = img[top:bottom, left:right]
if index==0:
newimgpath=NEWPATH+"/"+file
else:
newimgpath=NEWPATH+"/"+str(index+1)+file
cv2.imwrite(newimgpath, crop_img) # 框出人脸
# 等待按键,随后退出,销毁窗口
k = cv2.waitKey(0)
cv2.destroyAllWindows()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xlitc/QrF.Python.FaceRecognition.git
git@gitee.com:xlitc/QrF.Python.FaceRecognition.git
xlitc
QrF.Python.FaceRecognition
QrF.Python.FaceRecognition
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891