资源简介
如题,基于python和dlib的换脸工程。详细请参考博客:http://blog.csdn.net/hongbin_xu/article/details/78878745。
代码片段和文件信息
#coding=utf-8
import cv2
import dlib
import os
import numpy as np
import glob
current_path = os.getcwd()
predictor_68_points_path = current_path + ‘/model/shape_predictor_68_face_landmarks.dat‘
predictor_5_points_path = current_path + ‘/model/shape_predictor_5_face_landmarks.dat‘
predictor_path = predictor_68_points_path# 选取人脸68个特征点检测器
face_path = current_path + ‘/faces/‘
SCALE_FACTOR = 1
FEATHER_AMOUNT = 11
FACE_POINTS = list(range(17 68))
MOUTH_POINTS = list(range(48 61))
RIGHT_BROW_POINTS = list(range(17 22))
LEFT_BROW_POINTS = list(range(22 27))
RIGHT_EYE_POINTS = list(range(36 42))
LEFT_EYE_POINTS = list(range(42 48))
NOSE_POINTS = list(range(27 35))
JAW_POINTS = list(range(0 17))
# Points used to line up the images.
ALIGN_POINTS = (LEFT_BROW_POINTS + RIGHT_EYE_POINTS + LEFT_EYE_POINTS +
RIGHT_BROW_POINTS + NOSE_POINTS + MOUTH_POINTS)
# Points from the second image to overlay on the first. The convex hull of each
# element will be overlaid.
OVERLAY_POINTS = [
LEFT_EYE_POINTS + RIGHT_EYE_POINTS + LEFT_BROW_POINTS + RIGHT_BROW_POINTS
NOSE_POINTS + MOUTH_POINTS
]
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
class TooManyFaces(Exception):
pass
class NoFace(Exception):
pass
def get_landmark(image):
face_rect = detector(image 1)
if len(face_rect) > 1:
print(‘Too many faces.We only need no more than one face.‘)
raise TooManyFaces
elif len(face_rect) == 0:
print(‘No face.We need at least one face.‘)
raise NoFace
else:
print(‘left {0}; top {1}; right {2}; bottom {3}‘.format(face_rect[0].left() face_rect[0].top() face_rect[0].right() face_rect[0].bottom()))
# box = face_rect[0]
# shape = predictor(image box)
# return np.matrix([[p.x p.y] for p in shape.parts()])
return np.matrix([[p.x p.y] for p in predictor(image face_rect[0]).parts()])
def test_get_landmark():
for img_path in glob.glob(os.path.join(face_path “*.jpg“)):
print(“Processing file: {}“.format(img_path))
img = cv2.imread(img_path cv2.IMREAD_COLOR)
img = cv2.cvtColor(img cv2.COLOR_BGR2RGB)
landmarks = get_landmark(img)
print landmarks
def transformation_from_points(points1 points2):
points1 = points1.astype(np.float64)
points2 = points2.astype(np.float64)
c1 = np.mean(points1 axis=0)
c2 = np.mean(points2 axis=0)
points1 -= c1
points2 -= c2
s1 = np.std(points1)
s2 = np.std(points2)
points1 /= s1
points2 /= s2
U S Vt = np.linalg.svd(points1.T * points2)
R = (U * Vt).T
return np.vstack([np.hstack(((s2 / s1) * R c2.T - (s2 / s1) * R * c1.T)) np.matrix([0. 0. 1.])])
def warp_image(image M dshape):
output_image = np.zeros(dshape dtype=image.dtype)
cv2.warpAffine(image M[:2] (dshape[1] dshape[0]) dst=output_image flags=cv2.WARP_INVERSE_MAP borderMode=cv2.BORDER_TRANSPARENT)
return output_image
def test_warp_image():
jobs_image_path = os.path.join(face_path “jobs2.jpg“)
obama_image_path = os.path.join
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-19 13:31 change_face\
文件 7672 2017-12-19 13:30 change_face\FaceChanger.py
文件 8436 2017-12-19 13:31 change_face\FaceChanger.pyc
文件 10108 2017-12-23 12:46 change_face\change_face.py
文件 5207 2017-12-17 14:11 change_face\change_face.pyc
目录 0 2017-12-23 11:48 change_face\faces\
文件 16149 2017-12-19 13:41 change_face\faces\ibrahimovic.jpg
文件 10777 2017-12-18 21:26 change_face\faces\jobs.jpg
文件 11933 2017-09-15 22:42 change_face\faces\jobs2.jpg
文件 9861 2017-12-19 13:36 change_face\faces\messi.jpg
文件 31096 2017-11-22 12:05 change_face\faces\messi2.jpg
文件 35925 2017-09-15 21:19 change_face\faces\obama.jpg
文件 23230 2017-11-22 14:15 change_face\faces\obama2.jpg
文件 14799 2017-12-19 13:40 change_face\faces\pique.jpg
文件 54033 2017-09-15 19:57 change_face\faces\silicon_valley.jpg
文件 16930 2017-12-19 13:38 change_face\faces\suarez.jpg
文件 8383 2017-12-18 22:14 change_face\faceswap.py
目录 0 2017-11-12 13:29 change_face\model\
文件 9150489 2017-11-12 13:29 change_face\model\shape_predictor_5_face_landmarks.dat
文件 99693937 2017-03-09 08:34 change_face\model\shape_predictor_68_face_landmarks.dat
文件 22661 2017-12-23 13:06 change_face\output.jpg
文件 118 2017-12-23 13:02 change_face\test_FaceChanger.py
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论