= array(); } $post['classname'] = 'post'; } function comment_format(&$post) { global $conf, $uid, $gid, $forumlist; if (empty($post)) return; $forum = $post['fid'] ? forum_read($post['fid']) : ''; $thread = well_thread_read_cache($post['tid']); if ($thread) { //$post['fid'] = $thread['fid']; $post['closed'] = $thread['closed']; $post['subject'] = $thread['subject']; $post['url'] = $thread['url']; } else { $post['closed'] = 0; $post['subject'] = lang('thread_not_exists'); $post['url'] = ''; } $post['create_date_fmt'] = humandate($post['create_date']); //$post['message'] = stripslashes(htmlspecialchars_decode($post['message'])); $user = user_read_cache($post['uid']); $post['username'] = array_value($user, 'username'); $post['user_avatar_url'] = array_value($user, 'avatar_url'); $post['user'] = $user ? user_safe_info($user) : user_guest(); isset($post['floor']) || $post['floor'] = 0; // 权限判断 $post['allowupdate'] = 2 == array_value($forum, 'comment', 0) && ($uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowupdate')); $post['allowdelete'] = group_access($gid, 'allowuserdelete') && $uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowdelete'); $post['user_url'] = url('user-' . $post['uid'] . ($post['uid'] ? '' : '-' . $post['pid'])); if ($post['files'] > 0) { list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($post['pid']); // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 if (2 == $conf['attach_on']) { foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $post['message'] = FALSE !== strpos($post['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $post['message']) : $post['message']; } } $post['filelist'] = $filelist; } else { $post['filelist'] = array(); } $post['classname'] = 'post'; } function comment_format_message(&$val) { global $conf; if (empty($val)) return; // 使用云储存 if (1 == $conf['attach_on'] && 1 == $val['attach_on']) { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } elseif (2 == $conf['attach_on'] && 2 == $val['attach_on']) { // 使用图床 list($attachlist, $imagelist, $filelist) = well_attach_find_by_tid($val['tid']); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换成图床 $val['message'] = FALSE !== strpos($val['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $val['message']) : $val['message']; } } else { $val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']); } //$val['message'] = stripslashes(htmlspecialchars_decode($val['message'])); } // 把内容中使用了云储存的附件链接替换掉 function comment_message_replace_url($pid, $message) { global $conf; if (0 == $conf['attach_on']) { $message = FALSE !== strpos($message, '="../upload/') ? str_replace('="../upload/', '="upload/', $message) : $message; $message = FALSE !== strpos($message, '="/upload/') ? str_replace('="/upload/', '="upload/', $message) : $message; } elseif (1 == $conf['attach_on']) { // 使用云储存 $message = str_replace('="' . $conf['cloud_url'] . 'upload/', '="upload/', $message); } elseif (2 == $conf['attach_on']) { // 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法 list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($pid); foreach ($imagelist as $key => $attach) { $url = $conf['upload_url'] . 'website_attach/' . $attach['filename']; // 替换回相对链接 $message = $attach['image_url'] && FALSE !== strpos($message, $attach['image_url']) ? str_replace($attach['image_url'], $url, $message) : $message; } } return $message; } function comment_filter($val) { unset($val['userip']); return $val; } function comment_highlight_keyword($str, $k) { $r = str_ireplace($k, '' . $k . '', $str); return $r; } // //
function comment_message_format(&$s) { if (xn_strlen($s) < 100) return; $s = preg_replace('#.*?
#is', '', $s); $s = str_ireplace(array('
', '
', '
', '

', '', '', '', '' . ''), "\r\n", $s); $s = str_ireplace(array(' '), " ", $s); $s = strip_tags($s); $s = preg_replace('#[\r\n]+#', "\n", $s); $s = xn_substr(trim($s), 0, 100); $s = str_replace("\n", '
', $s); } // 对内容进行引用 function comment_quote($quotepid) { $quotepost = comment_read($quotepid); if (empty($quotepost)) return ''; $uid = $quotepost['uid']; $s = $quotepost['message']; $s = comment_brief($s, 100); $userhref = url('user-' . $uid); $user = user_read_cache($uid); $r = '
' . $user['username'] . ' ' . $s . '
'; return $r; } // 获取内容的简介 0: html, 1: txt; 2: markdown; 3: ubb function comment_brief($s, $len = 100) { $s = strip_tags($s); $s = htmlspecialchars($s); $more = xn_strlen($s) > $len ? ' ... ' : ''; $s = xn_substr($s, 0, $len) . $more; return $s; } ?>python怎么画人脸代码,OpenCV
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python怎么画人脸代码,OpenCV

运维笔记admin61浏览0评论

python怎么画人脸代码,OpenCV

python怎么画人脸代码,OpenCV

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)

参考链接:

发布评论

评论列表(0)

  1. 暂无评论