动态效果图效果图
1、在这里插入图片描述
2、在这里插入图片描述
在这里插入图片描述视频转为gif命令:

ffmpeg -i  ****.avi ***.gif

代码如下:

#!/usr/bin/python

import cv2
import numpy as np
import random
import sys
import dlib
# 检查点是否在rect内
def rect_contains(rect, point) :
    if point[0] < rect[0] :
        return False
    elif point[1] < rect[1] :
        return False
    elif point[0] > rect[2] :
        return False
    elif point[1] > rect[3] :
        return False
    return True

# 画点,实心
def draw_point(img, p, color ) :
    cv2.circle( img, p, 2, color, -1, cv2.LINE_AA, 0 )


# 画三角形
def draw_delaunay(img, subdiv, delaunay_color ) :

    triangleList = subdiv.getTriangleList();
    size = img.shape
    r = (0, 0, size[1], size[0])

    for t in triangleList :
        
        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_AA, 0)
            cv2.line(img, pt2, pt3, delaunay_color, 1, cv2.LINE_AA, 0)
            cv2.line(img, pt3, pt1, delaunay_color, 1, cv2.LINE_AA, 0)


# 画 voronoi 
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, cv2.LINE_AA, 0);
        ifacets = np.array([ifacet])
        cv2.polylines(img, ifacets, True, (0, 0, 0), 1, cv2.LINE_AA, 0)
        cv2.circle(img, (centers[i][0], centers[i][1]), 3, (0, 0, 0), -1, cv2.LINE_AA, 0)


if __name__ == '__main__':

    # 定义窗口名字
    win_delaunay = "Delaunay Triangulation"
    win_voronoi = "Voronoi Diagram"

    # 动画
    animate = True
    
    # 定义颜色
    delaunay_color = (255,255,255)
    points_color = (0, 0, 255)

    # 读入图像
    img = cv2.imread(sys.argv[1]);
    cap=cv2.VideoCapture(sys.argv[1])
    if cap.isOpened():
        fps=cap.get(cv2.CAP_PROP_FPS)
        size=(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get( cv2.CAP_PROP_FRAME_HEIGHT)))
    videoWriter = cv2.VideoWriter('face.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)#写图像
    # 
    img_orig = img.copy();
    # 创建 Subdiv2D
    size = img.shape
    rect = (0, 0, size[1], size[0])
    subdiv = cv2.Subdiv2D(rect)
    detector=dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor(sys.argv[2])#.dat file
    faces=detector(img,1)
    for i,d in enumerate(faces):
        points=[]
        subdiv1 = cv2.Subdiv2D(rect)
        shape=predictor(img,d)
        for i in range(68):
            points.append((int(shape.part(i).x),int(shape.part(i).y)))
        for p in points:
            subdiv.insert(p)
            subdiv1.insert(p)
            if animate :
                img_copy = img_orig.copy()
                draw_delaunay( img_copy, subdiv1, (255, 255, 255) )
                draw_point(img_copy, p, (0,0,255))
                videoWriter.write(img_copy)
                cv2.imshow(win_delaunay, img_copy)
                cv2.waitKey(200)
    draw_delaunay( img, subdiv, (255, 255, 255) );
    img_voronoi = np.zeros(img.shape, dtype = img.dtype)
    draw_voronoi(img_voronoi,subdiv)
    cv2.imshow(win_delaunay,img)
    cv2.imshow(win_voronoi,img_voronoi)
    cv2.waitKey(0)
    cv2.imshow(win_delaunay,img)
    cv2.imshow(win_voronoi,img_voronoi)
    cv2.waitKey(0)




Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐