本文主要和大家分享python获取人脸的代码分享,希望大家根据本文能完成python获取人脸的功能。
usage:python getface.py src out
# -*- codeing: utf-8 -*-import sysimport osimport cv2import dlibinput_dir = sys.argv[1]output_dir = sys.argv[2]print(input_dir)print(output_dir)size = 64if not os.path.exists(output_dir): os.makedirs(output_dir)# 使用dlib自带的frontal_face_detector作为我们的特征提取器detector = dlib.get_frontal_face_detector()index = 1for (path, dirnames, filenames) in os.walk(input_dir): for filename in filenames: if filename.endswith('.jpg'): print('Being processed picture %s' % index) img_path = path + '/' + filename # 从文件读取图片 img = cv2.imread(img_path) # 转为灰度图片 gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 使用detector进行人脸检测 dets为返回的结果 dets = detector(gray_img, 1) # 使用enumerate 函数遍历序列中的元素以及它们的下标 # 下标i即为人脸序号 # left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离 # top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离 for i, d in enumerate(dets): x1 = d.top() if d.top() > 0 else 0 y1 = d.bottom() if d.bottom() > 0 else 0 x2 = d.left() if d.left() > 0 else 0 y2 = d.right() if d.right() > 0 else 0 # img[y:y+h,x:x+w] face = img[x1:y1, x2:y2] # 调整图片的尺寸 face = cv2.resize(face, (size, size)) #cv2.imshow('image', face) # 保存图片 cv2.imwrite(output_dir + '/' + str(index) + '.jpg', face) index += 1 key = cv2.waitKey(30) & 0xff if key == 27: sys.exit(0) # -*- codeing: utf-8 -*-
登录后复制
相关推荐:
实例详解Python人脸识别
JavaScript人脸检测的实现方法
python中使用OpenCV进行人脸检测的例子
立即学习“Python免费学习笔记(深入)”;
以上就是python获取人脸的代码分享的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2265431.html