YOLOv8 智能肿瘤检测系统:从图像处理到诊断的深度学习应用与源码解析
2026/1/22 18:05:15
你现在关注的是face_recognition这个Python库,它是基于dlib实现的人脸检测/识别工具,上手简单且功能实用。结合你之前安装dlib的背景,我会帮你把face_recognition的安装、核心用法和注意事项讲清楚,确保你能快速用起来。
face_recognition的正确安装如果还没装dlib,先执行这一步(适配你的Python3.11+64位Windows):
# 下载并安装匹配的dlib预编译包(先切换到下载目录,比如桌面)cdC:\Users\admin\Desktop pipinstalldlib-19.25.0-cp311-cp311-win_amd64.whl# 基础安装(推荐用清华镜像源,速度更快)pipinstallface_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple执行以下命令,无报错则说明安装完成:
python -c"import face_recognition; print('face_recognition安装成功')"这个库的核心功能包括:人脸检测、人脸特征提取、人脸比对/识别,下面是极简示例,你可以直接复制运行。
importface_recognitionimportcv2# 需提前安装:pip install opencv-python# 加载图片(替换为你的图片路径,比如桌面的test.jpg)image=face_recognition.load_image_file("C:\\Users\\admin\\Desktop\\test.jpg")# 检测人脸位置(返回每个脸的上下左右坐标)face_locations=face_recognition.face_locations(image)print(f"检测到{len(face_locations)}个人脸")forface_locationinface_locations:top,right,bottom,left=face_locationprint(f"人脸位置:上边{top},右边{right},下边{bottom},左边{left}")# 用OpenCV画出人脸框(可选,可视化结果)cv2_image=cv2.cvtColor(image,cv2.COLOR_RGB2BGR)cv2.rectangle(cv2_image,(left,top),(right,bottom),(0,255,0),2)cv2.imshow("Face Detection",cv2_image)cv2.waitKey(0)cv2.destroyAllWindows()importface_recognition# 加载参考人脸图片和待比对图片known_image=face_recognition.load_image_file("C:\\Users\\admin\\Desktop\\known.jpg")# 已知人脸unknown_image=face_recognition.load_image_file("C:\\Users\\admin\\Desktop\\unknown.jpg")# 待比对人脸# 提取人脸特征编码(每个脸生成128维特征向量)known_encoding=face_recognition.face_encodings(known_image)[0]unknown_encoding=face_recognition.face_encodings(unknown_image)[0]# 比对特征(返回True/False)results=face_recognition.compare_faces([known_encoding],unknown_encoding)ifresults[0]:print("是同一个人!")else:print("不是同一个人!")\\或原始字符串r"路径",比如r"C:\Users\admin\Desktop\test.jpg",避免转义符报错;face_encodings会返回空列表,访问[0]会报错,可加判断:encodings=face_recognition.face_encodings(image)iflen(encodings)==0:print("未检测到人脸")else:encoding=encodings[0]face_recognition对dlib版本兼容较好,只要是19.18以上版本都能正常使用。face_recognition的核心是依赖dlib实现人脸特征提取,安装前必须确保dlib适配你的Python3.11环境;face_recognition),推荐用国内镜像源加速;face_locations)和人脸比对(face_encodings+compare_faces),新手可先从简单示例入手。