python实时换脸 python换脸开源
转载
- 摘要:Dlib是一个包含机器学习算法的C++开源工具包。Dlib可以帮助您创建很多复杂的机器学习方面的软件来帮助解决实际问题。目前Dlib已经被广泛的用在行业和学术领域,包括机器人,嵌入式设备,移动电话和大型高性能计算环境。Dlib是开源的、免费的;官网和git地址:#官网http://dlib.net/#githubhttps://github.com/davisking/dlibDlib的主要特点:1.文档齐全不像很多其他的开源库一样,Dlib为每一个类和函数提供了完整的文档说
- Dlib是一个包含机器学习算法的C++开源工具包。Dlib可以帮助您创建很多复杂的机器学习方面的软件来帮助解决实际问题。目前Dlib已经被广泛的用在行业和学术领域,包括机器人,嵌入式设备,移动电话和大型高性能计算环境。 # 官网http://dlib.net/# githubhttps://github.com/davisking/dlib
Dlib的主要特点: 1. 文档齐全
不像很多其他的开源库一样,Dlib为每一个类和函数提供了完整的文档说明。同时,还提供了debug模式;打开debug模式后,用户可以调试代码,查看变量和对象的值,快速定位错误点。另外,Dlib还提供了大量的实例。
2. 高质量的可移植代码
Dlib不依赖第三方库,无须安装和配置,这部分可参照(官网左侧树形目录的how to compile的介绍)。Dlib可用在window、Mac OS、Linux系统上。
3. 提供大量的机器学习 / 图像处理算法
>> 深度学习
>> 基于SVM的分类和递归算法
>> 针对大规模分类和递归的降维方法
>> 相关向量机(relevance vector machine);是与支持向量机相同的函数形式稀疏概率模型,对未知函数进行预测或分类。其训练是在贝叶斯框架下进行的,与SVM相比,不需要估计正则化参数,其核函数也不需要满足Mercer条件,需要更少的相关向量,训练时间长,测试时间短。
>> 聚类: linear or kernel k-means, Chinese Whispers, and Newman clustering. Radial Basis Function Networks
>> 多层感知机
人脸检测:
#coding=utf-8
import cv2
import dlib
path = "img/meinv.png"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
"C:\\Python36\\Lib\\site-packages\\dlib-data\\shape_predictor_68_face_landmarks.dat"
)
dets = detector(gray, 1)
for face in dets:
shape = predictor(img, face) # 寻找人脸的68个标定点
# 遍历所有点,打印出其坐标,并圈出来
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Dlib实现68点标定
//设置人脸的标记点
#include <dlib\opencv.h>
#include <opencv2\opencv.hpp>
#include <dlib\image_processing\frontal_face_detector.h>
#include <dlib\image_processing\render_face_detections.h>
#include <dlib\image_processing.h>
#include <dlib\gui_widgets.h>
//声明dlib的域
using namespace dlib;
using namespace std;
int main() {
try {
//首先进行获取摄像头
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
//如果摄像头没有开启
cerr << "Unable to connect to camera" << endl;
return 1;
}
//Load face detection and pos estimation models 加载我们需要的脸部识别和姿态估计模型
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pos_modle;
//将文件中的模型放置再pos_modle中
deserialize("shape_predictor_68_face_landmarks.dat") >> pos_modle;
//Grab and process frames until the main window is closed by the user
//处理当前每一帧的图片
while (cv::waitKey(30)!=27)
{
//Grab a frame 获取一帧
cv::Mat temp;
//将摄像头获取的当前帧图片放入到 中间文件中
cap >> temp;
//将其转化为RGB像素图片
cv_image<bgr_pixel> cimg(temp);
//开始进行脸部识别
std::vector<rectangle> faces = detector(cimg);
//发现每一个脸的pos估计 Find the pose of each face
std::vector<full_object_detection> shapes;
unsigned faceNumber= faces.size();
//将所有脸的区域放入集合之中
for (unsigned i = 0; i < faceNumber; i++)
shapes.push_back(pos_modle(cimg, faces[i]));
if (!shapes.empty()) {
int faceNumber = shapes.size();
for (int j = 0; j < faceNumber; j++)
{
for (int i = 0; i < 68; i++)
{
//用来画特征值的点
cv::circle(temp, cvPoint(shapes[j].part(i).x(), shapes[j].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
//显示数字
cv::putText(temp,to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN,1, cv::Scalar(0, 0, 255));
}
}
}
//Display it all on the screen 展示每一帧的图片
cv::imshow("Dlib标记", temp);
}
}
catch (serialization_error &e) {
cout << "You need dlib's default face landmarking file to run this example.(你需要添加landmark的bat文件,才可以跑这个实例)" << endl;
cout << endl << e.what() << endl;
}
catch(exception &e){
cout << e.what() << endl;
}
}
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。