解释概述
人脸检测是指在图像或视频中检测出人脸的位置和大小。这个过程通常使用计算机视觉技术来实现。计算机会根据一些特征,比如肤色、面部轮廓等,来判断图像中是否存在人脸,并且确定人脸的位置和大小。人脸检测在很多应用中都有广泛的应用,比如人脸识别、视频监控等。
人脸识别是指在已知的人脸数据库中匹配并识别出一张或多张人脸。这个过程通常使用人工智能和机器学习技术来实现。计算机会通过学习已知的人脸数据库中的特征,比如面部轮廓、眼睛位置等,来识别新的人脸。人脸识别在安全领域、社交媒体等方面都有广泛的应用。
Robotpipe
robotpipe Python Library is an artificial intelligence learning library, including mainstream advanced artificial intelligence libraries, such as InsightFace, yolov8, paddleocr, etc.
本项目具备如下推理功能:
- 人脸检测
- 人脸识别
- 人脸年龄与性别识别
- 人脸106个特征点
- 换脸
安装
安装opencv
pip install opencv-python-headless==4.6.0.66
pip install opencv-python==4.6.0.66
pip install opencv-contrib-python==4.6.0.66
安装onnxruntime
pip install onnxruntime
若你的电脑支持gpu,可以进入如下安装
pip install onnxruntime-gpu
pip安装
pip install robotpipe
若安装失败,请指定安装路径
pip install robotpipe -i https://pypi.tuna.tsinghua.edu.cn/simple
导入模型文件
在用户目录下新建.robotpipe
文件夹
C:\Users\KAI\.robotpipe\models
在models里面放入权重文件
从百度网盘中下载权重文件,并解压释放进去
功能演示
人脸检测
from robotpipe import FaceDetection
from robotpipe import draw_faces
import cv2 as cv
import numpy as np
if __name__ == '__main__':
face_detection = FaceDetection()
img = cv.imread('images/trump1.jpg')
faces = face_detection.predict(img)
dst = draw_faces(img,faces)
cv.imshow('img',dst)
cv.waitKey(0)
人脸识别
from robotpipe import FaceRecognition
import cv2 as cv
import numpy as np
if __name__ == '__main__':
face_recognition = FaceRecognition()
trump1 = cv.imread('images/trump1.jpg')
trump2 = cv.imread('images/trump2.jpg')
# 获取人脸特征
feats1 = face_recognition.predict(trump1)
feats2 = face_recognition.predict(trump2)
sim = face_recognition.compute_sim(feats1,feats2)
# 根据相似度,输出结果
if sim<0.2:
conclu = 'they are not the same'
elif sim>=0.2 and sim<0.28:
conclu = 'they are looks like the same people'
else:
conclu = 'they are the same people'
print(sim,conclu)
cv.imshow('trump1',trump1)
cv.imshow('trump2',trump2)
cv.waitKey(0)
人脸年龄与性别识别
from robotpipe import FaceAttribute
from robotpipe import draw_faces
import cv2 as cv
import numpy as np
if __name__ == '__main__':
face_attr = FaceAttribute()
img = cv.imread('images/trump1.jpg')
# 获取人脸特征
faces = face_attr.predict(img)
# 1 表示男性, 0,表示女性
print(faces[0])
dst = draw_faces(img,faces)
cv.imshow("images/1.jpg",dst)
cv.waitKey(0)
人脸106个特征点
from robotpipe import FaceLandmark
import cv2 as cv
import numpy as np
from heimarobot import draw_faces,draw_landmarks
if __name__ == '__main__':
face_attr = FaceLandmark()
trump1 = cv.imread('images/trump1.jpg')
# 获取人脸特征106个特征点
faces = face_attr.predict(trump1)
retimg = draw_landmarks(trump1,faces)
cv.imshow('trump1_landmark',retimg)
cv.waitKey(0)
换脸
from robotpipe import FaceSwapper
import cv2 as cv
import numpy as np
if __name__ == '__main__':
face_swapper = FaceSwapper()
trump1 = cv.imread('images/trump1.jpg')
target = cv.imread('images/1.jpg')
sourceFace = face_swapper.get_source_face(trump1)
dst = face_swapper.predict(sourceFace, target)
cv.imshow("trump1",trump1)
cv.imshow("1",target)
cv.imshow("dst", dst)
cv.waitKey()
若运行代码出现一下错误,则是opencv-python-headless
与opencv-python
版本不匹配的原因
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1272: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
卸载opencv-python-headless
,重新安装
pip install opencv-python-headless==4.6.0.66
pip install opencv-python==4.6.0.66
pip install opencv-contrib-python==4.6.0.66
本项目参考以下工程: