对实现人脸瘦脸简单功能的一个记录,大概流程如下:

1.使用dlib检测出人脸关键点

2.使用Interactive Image Warping 局部平移算法实现瘦脸

参考:https://blog.csdn.net/grafx/article/details/70232797?locationNum=11&fps=1

#!/usr/bin/env python3
# -*- coding: utf-8 -*- import dlib
import cv2
import numpy as np
import math
predictor_path='data/shape_predictor_68_face_landmarks.dat' #使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path) def landmark_dec_dlib_fun(img_src):
img_gray = cv2.cvtColor(img_src,cv2.COLOR_BGR2GRAY) land_marks = [] rects = detector(img_gray,0) for i in range(len(rects)):
land_marks_node = np.matrix([[p.x,p.y] for p in predictor(img_gray,rects[i]).parts()])
# for idx,point in enumerate(land_marks_node):
# # 68点坐标
# pos = (point[0,0],point[0,1])
# print(idx,pos)
# # 利用cv2.circle给每个特征点画一个圈,共68个
# cv2.circle(img_src, pos, 5, color=(0, 255, 0))
# # 利用cv2.putText输出1-68
# font = cv2.FONT_HERSHEY_SIMPLEX
# cv2.putText(img_src, str(idx + 1), pos, font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)
land_marks.append(land_marks_node) return land_marks '''
方法: Interactive Image Warping 局部平移算法
''' def localTranslationWarp(srcImg,startX,startY,endX,endY,radius): ddradius = float(radius * radius)
copyImg = np.zeros(srcImg.shape, np.uint8)
copyImg = srcImg.copy() # 计算公式中的|m-c|^2
ddmc = (endX - startX) * (endX - startX) + (endY - startY) * (endY - startY)
H, W, C = srcImg.shape
for i in range(W):
for j in range(H):
#计算该点是否在形变圆的范围之内
#优化,第一步,直接判断是会在(startX,startY)的矩阵框中
if math.fabs(i-startX)>radius and math.fabs(j-startY)>radius:
continue distance = ( i - startX ) * ( i - startX) + ( j - startY ) * ( j - startY ) if(distance < ddradius):
#计算出(i,j)坐标的原坐标
#计算公式中右边平方号里的部分
ratio=( ddradius-distance ) / ( ddradius - distance + ddmc)
ratio = ratio * ratio #映射原位置
UX = i - ratio * ( endX - startX )
UY = j - ratio * ( endY - startY ) #根据双线性插值法得到UX,UY的值
value = BilinearInsert(srcImg,UX,UY)
#改变当前 i ,j的值
copyImg[j,i] =value return copyImg #双线性插值法
def BilinearInsert(src,ux,uy):
w,h,c = src.shape
if c == 3:
x1=int(ux)
x2=x1+1
y1=int(uy)
y2=y1+1 part1=src[y1,x1].astype(np.float)*(float(x2)-ux)*(float(y2)-uy)
part2=src[y1,x2].astype(np.float)*(ux-float(x1))*(float(y2)-uy)
part3=src[y2,x1].astype(np.float) * (float(x2) - ux)*(uy-float(y1))
part4 = src[y2,x2].astype(np.float) * (ux-float(x1)) * (uy - float(y1)) insertValue=part1+part2+part3+part4 return insertValue.astype(np.int8) def face_thin_auto(src): landmarks = landmark_dec_dlib_fun(src) #如果未检测到人脸关键点,就不进行瘦脸
if len(landmarks) == 0:
return for landmarks_node in landmarks:
left_landmark= landmarks_node[3]
left_landmark_down=landmarks_node[5] right_landmark = landmarks_node[13]
right_landmark_down = landmarks_node[15] endPt = landmarks_node[30] #计算第4个点到第6个点的距离作为瘦脸距离
r_left=math.sqrt((left_landmark[0,0]-left_landmark_down[0,0])*(left_landmark[0,0]-left_landmark_down[0,0])+
(left_landmark[0,1] - left_landmark_down[0,1]) * (left_landmark[0,1] - left_landmark_down[0, 1])) # 计算第14个点到第16个点的距离作为瘦脸距离
r_right=math.sqrt((right_landmark[0,0]-right_landmark_down[0,0])*(right_landmark[0,0]-right_landmark_down[0,0])+
(right_landmark[0,1] -right_landmark_down[0,1]) * (right_landmark[0,1] -right_landmark_down[0, 1])) #瘦左边脸
thin_image = localTranslationWarp(src,left_landmark[0,0],left_landmark[0,1],endPt[0,0],endPt[0,1],r_left)
#瘦右边脸
thin_image = localTranslationWarp(thin_image, right_landmark[0,0], right_landmark[0,1], endPt[0,0],endPt[0,1], r_right) #显示
cv2.imshow('thin',thin_image)
cv2.imwrite('thin.jpg',thin_image) def main():
src = cv2.imread('img/test6.jpg')
cv2.imshow('src', src)
face_thin_auto(src)
cv2.waitKey(0) if __name__ == '__main__':
main()

原文:https://blog.csdn.net/u011941438/article/details/82416470

python+opencv+dlib瘦脸效果的更多相关文章

  1. Python+OpenCV图像处理(一)——读取显示一张图片

    先在此处先声明,后面学习python+opencv图像处理时均参考这位博主的博文https://blog.csdn.net/u011321546/article/category/7495016/2? ...

  2. 基于深度学习的人脸识别系统(Caffe+OpenCV+Dlib)【三】VGG网络进行特征提取

    前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...

  3. 基于深度学习的人脸识别系统系列(Caffe+OpenCV+Dlib)——【四】使用CUBLAS加速计算人脸向量的余弦距离

    前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...

  4. linux/ubuntu下最简单好用的python opencv安装教程 ( 解决 imshow, SIFT, SURF, CSRT使用问题)

    希望这篇文章能彻底帮你解决python opencv安装和使用中的常见问题. 懒人请直奔这一节, 一条命令安装 opencv 使用python-opencv常用的问题 在linux中使用python版 ...

  5. python opencv识别蓝牌车牌号 之 取出车牌号 (1/3)

    概述 车牌识别是计算机视频图像识别技术在车辆牌照识别中的一种应用,通常来讲如果结合opencv进行车牌识别主要分为四个大步骤,分别为: 图像采集 车牌定位 分割车牌字符 字符识别 当然,如果结合了机器 ...

  6. Python+opencv打开修图的正确方式get

    先逼逼两句: 图像是 Web 应用中除文字外最普遍的媒体格式. 流行的 Web 静态图片有 JPEG.PNG.ICO.BMP 等.动态图片主要是 GIF 格式.为了节省图片传输流量,大型互联网公司还会 ...

  7. 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台

    搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...

  8. .NET + OpenCV & Python + OpenCV 配置

    最近需要做一个图像识别的GUI应用,权衡了Opencv+ 1)QT,2)Python GUI,3).NET后选择了.NET... 本文给出C#+Opencv和Python+Opencv的相应参考,节省 ...

  9. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

随机推荐

  1. C#中SqlDataAdapter的使用小结---转载

    C#中SqlDataAdapter的使用小结 转载 叁木-Neil 最后发布于2018-06-07 21:29:39 阅读数 8275 收藏 展开 SqlDataAdapter对象 一.特点介绍1.表 ...

  2. python 开启http服务并下载文件

    Python <= 2.3python -c "import SimpleHTTPServer as s; s.test();" 8000 Python >= 2.4p ...

  3. vs2015中安装cplex攻略以及解决丢失cplex.dll问题

    转:http://blog.sina.com.cn/s/blog_61f0374801014swp.html 按:相信配置过CPLEX的人大多有过痛苦而难忘的经历,本人亦不例外,纠结挣扎了一个下午加一 ...

  4. [Codeforces #608 div2]1271D Portals

    Description You play a strategic video game (yeah, we ran out of good problem legends). In this game ...

  5. 题解 loj3050 「十二省联考 2019」骗分过样例

    CASE \(1\sim 3\) \(n\)组测试数据,每次输入一个数\(x\),求\(19^x\). 测试点\(1\),\(x=0,1,\dots n-1\),可以直接递推. 测试点\(2\)要开l ...

  6. (十二)微信小程序实现登陆页面+登陆逻辑

    微信小程序实现登陆页面 实现上面两个页面 第一个页面 <view> <!-- 上侧部分 --> <view class="top-view"> ...

  7. Luogu P3263 [JLOI2015]有意义的字符串

    Link 设\(e=\frac{b+\sqrt d}2,i=\frac{b-\sqrt d}2\). 显然\(f_n=e^n+i^n\)是一个整数,且\(f_n=(e+i)f_{n-1}+eif_{n ...

  8. SimpleAuthenticationInfo

    public SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSa ...

  9. 查看oracle单签session

    转自 https://blog.csdn.net/alexsong123/article/details/51858092 怎样查看oracle当前的连接数呢?只需要用下面的SQL语句查询一下就可以了 ...

  10. 关于 float 型和 double 型的区别,以及 char 型和 String 型的不同

    一.1.float是单精度浮点数,内存分配4个字节,占32位,有效小数位6-7位 double是双精度浮点数,内存分配8个字节,占64位,有效小数位15位 2.java中默认声明的小数是double类 ...