python怎么画人脸代码,OpenCV-Python 绘制人脸 Delaunay 三角剖分(人脸识别核心技术之一)...
1,介绍
开始之前,向大家提前说声抱歉,上一篇文章末尾提到了,在这篇文章将给大家介绍关于用 OpenCV 实现人脸融合技术,由于人脸融合技术所需的知识储备有点多,不只是之前介绍的的特征点提取,还有本文所提到的三角剖分,因此文章会向后面推迟一点,但请大家放心,人脸融合技术一定会在随后的几篇文章安排上日程。
看到标题里的两个词 Delaunay 三角剖分 和 Voronoi,估计第一次见到的小伙伴可能一脸懵(说的就是我自己),为了更直观地认识这两个概念,请看下图:
左图:68个人脸特征点 中图:Delaunay 三角剖分,右图 Voronoi 图表
左图是上篇文章提到的 68个人脸特征点标记,中图是基于左图的基础上对 68个点进行 点与点之间形成 Delaunay 三角剖分(德劳内),左图是基于中间图绘制的的 Voronoi Diagram (沃罗诺伊图)
2,Delaunay 三角剖分
Delaunay 三角剖分算法命名那个来源于俄国数学家 Boris Delaunay,该方法目的是最大化三角剖分中三角形中最小角,目的是避免“极瘦“的三角形的出现
Snipaste_2020-06-04_15-23-46.png
上方左图与右图的变换站示的就是 Delaunay 怎样最大化最小角,左右两图是对于四个顶点的两种不同的剖分方式;但左图中 顶点 A、C 不在三角形 BCD、ABD 的外接圆内,使得 角 C 非常大
右图对剖分形式有两个方的 改动:1,B、D 坐标右移;2,剖分线由 BD 变为 AC ;最后使得剖分后的三角形不那么”瘦“
3,Voronoi Diagram
Voronoi 命名同样也是来源于一个 俄国数学家 Georgy Voronoy,有趣的是 Georgy Voronoy 是 Boris Delaunay 的博士导师
Voronoi 图是基于 Delaunay 三角剖分创建,取 Delaunay 剖分的所有顶点,用线段连接相邻三角形的外接圆心,构成一个区域,相邻不同区域用不同颜色覆盖;Voronoi 图目前常用于凸边形区域分割领域
从下面20个顶点组成的 Voronoi 图种可以了解到,图中相邻点与点之间的距离是等长的
20个顶点构成的 Voronoi
4,OpenCV 代码实现
1,首先需要获取人脸 68 个特征点坐标,并写入 txt 文件,方便后面使用,这里会用到的代码
import dlib
import cv2
predictor_path = "E:/data_ceshi/shape_predictor_68_face_landmarks.dat"
png_path = "E:/data_ceshi/timg.jpg"
txt_path = "E:/data_ceshi/points.txt"
f = open(txt_path,'w+')
detector = dlib.get_frontal_face_detector()
#相撞
predicator = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
img1 = cv2.imread(png_path)
dets = detector(img1,1)
print("Number of faces detected : {}".format(len(dets)))
for k,d in enumerate(dets):
print("Detection {} left:{} Top: {} Right {} Bottom {}".format(
k,d.left(),d.top(),d.right(),d.bottom()
))
lanmarks = [[p.x,p.y] for p in predicator(img1,d).parts()]
for idx,point in enumerate(lanmarks):
f.write(str(point[0]))
f.write("\t")
f.write(str(point[1]))
f.write('\n')
写入后,txt 中格式如下
image
2,利用图像大小创建一个矩形范围( 因为脸部特征点都是图中),创建一个 Subdiv2D 实例(后面两个图的绘制都会用到这个类),把点都插入创建的类中:
#Create an instance of Subdiv2d
subdiv = cv2.Subdiv2D(rect)
#Create an array of points
points = []
#Read in the points from a text file
with open("E:/data_ceshi/points.txt") as file:
for line in file:
x,y = line.split()
points.append((int(x),int(y)))
#Insert points into subdiv
for p in points:
subdiv.insert(p)
3,在原图上绘制 Delaunay 三角剖分并预览,这里我加入了动画效果 — 逐线段绘制(用了 for 循环)
#Draw delaunay triangles
def draw_delaunay(img,subdiv,delaunay_color):
trangleList = subdiv.getTriangleList()
size = img.shape
r = (0,0,size[1],size[0])
for t in trangleList:
pt1 = (t[0],t[1])
pt2 = (t[2],t[3])
pt3 = (t[4],t[5])
if (rect_contains(r,pt1) and rect_contains(r,pt2) and rect_contains(r,pt3)):
cv2.line(img,pt1,pt2,delaunay_color,1)
cv2.line(img,pt2,pt3,delaunay_color,1)
cv2.line(img,pt3,pt1,delaunay_color,1)
#Insert points into subdiv
for p in points:
subdiv.insert(p)
#Show animate
if animate:
img_copy = img_orig.copy()
#Draw delaunay triangles
draw_delaunay(img_copy,subdiv,(255,255,255))
cv2.imshow(win_delaunary,img_copy)
cv2.waitKey(100)
预览效果如下:
imag11252323.gif
4,最后绘制 Voronoi Diagram
def draw_voronoi(img,subdiv):
(facets,centers) = subdiv.getVoronoiFacetList([])
for i in range(0,len(facets)):
ifacet_arr = []
for f in facets[i]:
ifacet_arr.append(f)
ifacet = np.array(ifacet_arr,np.int)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
cv2.fillConvexPoly(img,ifacet,color)
ifacets = np.array([ifacet])
cv2.polylines(img,ifacets,True,(0,0,0),1)
cv2.circle(img,(centers[i][0],centers[i][1]),3,(0,0,0))
for p in points:
draw_point(img,p,(0,0,255))
#Allocate space for Voroni Diagram
img_voronoi = np.zeros(img.shape,dtype = img.dtype)
#Draw Voonoi diagram
draw_voronoi(img_voronoi,subdiv)
Snipaste_2020-06-04_14-43-10.png
4,小总结
Delaunay 三角剖分对于第一次接触的小伙伴来说可能还未完全理解,但这一剖分技术对于做人脸识别、融合、换脸是不可或缺的,本篇文章只是仅通过 OpenCV 的 Subdiv2D 函数下实现此功能,真正的识别技术要比这个复杂地多。
对于感兴趣的小伙伴们,我的建议还是跟着提供的代码敲一遍,完整代码贴在下面:
import cv2
import numpy as np
import random
#Check if a point is insied a rectangle
def rect_contains(rect,point):
if point[0]
return False
elif point[1]
return False
elif point[0]>rect[2]:
return False
elif point[1] >rect[3]:
return False
return True
# Draw a point
def draw_point(img,p,color):
cv2.circle(img,p,2,color)
#Draw delaunay triangles
def draw_delaunay(img,subdiv,delaunay_color):
trangleList = subdiv.getTriangleList()
size = img.shape
r = (0,0,size[1],size[0])
for t in trangleList:
pt1 = (t[0],t[1])
pt2 = (t[2],t[3])
pt3 = (t[4],t[5])
if (rect_contains(r,pt1) and rect_contains(r,pt2) and rect_contains(r,pt3)):
cv2.line(img,pt1,pt2,delaunay_color,1)
cv2.line(img,pt2,pt3,delaunay_color,1)
cv2.line(img,pt3,pt1,delaunay_color,1)
# Draw voronoi diagram
def draw_voronoi(img,subdiv):
(facets,centers) = subdiv.getVoronoiFacetList([])
for i in range(0,len(facets)):
ifacet_arr = []
for f in facets[i]:
ifacet_arr.append(f)
ifacet = np.array(ifacet_arr,np.int)
color = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
cv2.fillConvexPoly(img,ifacet,color)
ifacets = np.array([ifacet])
cv2.polylines(img,ifacets,True,(0,0,0),1)
cv2.circle(img,(centers[i][0],centers[i][1]),3,(0,0,0))
if __name__ == '__main__':
#Define window names;
win_delaunary = "Delaunay Triangulation"
win_voronoi = "Voronoi Diagram"
#Turn on animations while drawing triangles
animate = True
#Define colors for drawing
delaunary_color = (255,255,255)
points_color = (0,0,255)
#Read in the image
img_path = "E:/data_ceshi/timg.jpg"
img = cv2.imread(img_path)
#Keep a copy around
img_orig = img.copy()
#Rectangle to be used with Subdiv2D
size = img.shape
rect = (0,0,size[1],size[0])
#Create an instance of Subdiv2d
subdiv = cv2.Subdiv2D(rect)
#Create an array of points
points = []
#Read in the points from a text file
with open("E:/data_ceshi/points.txt") as file:
for line in file:
x,y = line.split()
points.append((int(x),int(y)))
#Insert points into subdiv
for p in points:
subdiv.insert(p)
#Show animate
if animate:
img_copy = img_orig.copy()
#Draw delaunay triangles
draw_delaunay(img_copy,subdiv,(255,255,255))
cv2.imshow(win_delaunary,img_copy)
cv2.waitKey(100)
#Draw delaunary triangles
draw_delaunay(img,subdiv,(255,255,255))
#Draw points
for p in points:
draw_point(img,p,(0,0,255))
#Allocate space for Voroni Diagram
img_voronoi = np.zeros(img.shape,dtype = img.dtype)
#Draw Voonoi diagram
draw_voronoi(img_voronoi,subdiv)
#Show results
cv2.imshow(win_delaunary,img)
cv2.imshow(win_voronoi,img_voronoi)
cv2.waitKey(0)
参考链接:
python怎么画人脸代码,OpenCV-Python 绘制人脸 Delaunay 三角剖分(人脸识别核心技术之一)...相关推荐
- python画直方图代码-python plotly画柱状图代码实例
这篇文章主要介绍了python plotly画柱状图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码 import pandas as ...
- python画柱状图代码-python plotly画柱状图代码实例
这篇文章主要介绍了python plotly画柱状图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码 import pandas as ...
- python画柱状图-python plotly画柱状图代码实例
这篇文章主要介绍了python plotly画柱状图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码 import pandas as ...
- python turtle画房子代码里面的窗子_Python turtle画图库画姓名实例
*****看一下我定义的change()和run()函数****** 绘图坐标体系: 作用:设置主窗体的大小和位置 turtle.setup(width,height,startx,starty) # ...
- python turtle画滑稽_使用python的turtle函数绘制一个滑稽表情的方法
Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行 ...
- python简笔画程序_使用python turtle绘制简笔画大白
使用python乌龟画的完整代码简单的中风:进口龟tpen = () # Define刷例子(0)(5)()(90)#头(-100200)()= 1我的范围(120):如果0 < =我< ...
- python ggplot画等值线图_用Python画漂亮的专业插图 ?So easy!
点击上方 "Python人工智能技术" 关注,星标或者置顶 22点24分准时推送,第一时间送达 来自:知乎问答 | 编辑:真经君链接:zhihu.com/question/2166 ...
- ocr python opencv_如何使用(opencv/python)来实现OCR处
今天我们来介绍一下如何使用(opencv/python)来实现OCR处理银行票据.文末有代码和相关文档下载! 在第一部分中,我们将讨论两个主题: 1.首先,我们将了解MICR E-13B字体,美国,英 ...
- python turtle画烟花_用Python写一个绚丽的烟花!
Python人工智能 - 一节课快速认识人工智能必备语言:python - 创客学院直播室www.makeru.com.cn 哈喽大家好,小编来教大家如何用Python写一个绚丽的烟花,下面我们开始 ...
- python如何仿写文章_python,python3.x_求助,用python仿写以下代码,python,python3.x,java - phpStudy...
求助,用python仿写以下代码 public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=s ...
最新文章
- 【Android UI设计与开发】第01期:引导界面(一)ViewPager介绍和使用详解
- Elasticsearch学习笔记1
- 螺丝上的十字磨没了_淘钉钉-螺丝刀还有这种形状,你了解吗?
- 前端学习(1044):本地存储实现数据录入
- 计算机应用基础专2020春,计算机应用基础(专)(专,2020春)(20200831130023).pdf
- 数据库工作笔记14---win10系统安装sqlserver2005提示服务无法启动
- Flutter之ScrollView简析
- Julia:关于split的用法
- Css选择器权重排序详解+权重计算
- 4K视频质量分析 白皮书
- 双目测距理论及其python实现
- 阿里P7亲自教你!昆明java招聘信息
- 深入解析Javascript异步编程
- 嵌入式开发要学多久?要学哪些课程
- Python 计时器(秒钟、秒表)
- mysqlcheck命令时提示: bash: mysqlcheck: command not found
- 文献阅读系列-2|TBC-Net: A real-time detector for infrared small
- android短信和彩信探秘threads
- oracle绑定变量执行计划,绑定变量对执行计划的影响
- ICCV2021 Oral 论文及论文实现代码合集