直接可用的人脸识别+人脸对齐模块
人脸识别:mtcnn (依赖opencv和tensorflow)
人脸对齐:face-alignment
国内镜像:
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
安装方式:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mtcnn
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple face-alignment
使用案例 - 人脸识别:
#!/usr/bin/env python # -*- coding:utf-8-*- ''' 使用python公开包 mtcnn 来进行人脸检测和关键点检测 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple mtcnn ''' import os import tensorflow as tf from mtcnn.mtcnn import MTCNN if tf.test.is_gpu_available(): os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"] = "0" import os, sys import os.path as osp import glob import numpy as np import cv2 import shutil from pprint import pprint def mkdir_if_not_exist(path): if not osp.exists(path): os.makedirs(path) def face_detect(): save_root = '/home/dxs/dataset/fer2013+/' fs = 'Test' read_path = save_root + '/' + fs save_path = save_root + '/' + fs + '_cutface' save_path2 = save_root + '/' + fs + '_noface' mkdir_if_not_exist(save_path) mkdir_if_not_exist(save_path2) with tf.device('/gpu:0'): detector = MTCNN() files = glob.glob(read_path + '/**/*.png') for f in files: subdir = f.split('/')[-2] fullpath = save_path + '/' + subdir mkdir_if_not_exist(fullpath) fullpath2 = save_path2 + '/' + subdir mkdir_if_not_exist(fullpath2) filename = osp.basename(f) # f = '0_Parade_Parade_0_730.jpg' img = cv2.imread(f) if img is None: continue src_h, src_w, c = img.shape face_list = detector.detect_faces(img) if len(face_list) == 0: save_filepath = osp.join(fullpath2, filename) shutil.copy(f, save_filepath) continue for item in face_list: box = item['box'] conf = item['confidence'] keyprints_dict = item['keypoints'] left_eyeXY = keyprints_dict['left_eye'] right_eyeXY = keyprints_dict['right_eye'] mouth_leftXY = keyprints_dict['mouth_left'] mouth_rightXY = keyprints_dict['mouth_right'] noseXY = keyprints_dict['nose'] if conf > .2: print('detect a face .') x, y, w, h = box offset = 5 x = x - offset if x > offset else 0 y = y - offset if y > offset else 0 w = w + offset if x + w + offset <= src_w else src_w - x h = h + offset if y + h + offset <= src_h else src_h - y crop_face = img[y:y + h, x:x + w, :] save_filepath = osp.join(fullpath, filename) if not osp.exists(save_filepath): cv2.imwrite(save_filepath, crop_face) # line = ','.join([' '.join(left_eyeXY),' '.join(right_eyeXY), # ' '.join(mouth_leftXY), ' '.join(mouth_rightXY), # ' '.join(noseXY)]) + '\n' # wf.write(line) print('{} is done .'.format(filename)) if __name__ == '__main__': face_detect()
使用案例 - 人脸对齐 :
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人