-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchange.py
More file actions
90 lines (60 loc) · 2.64 KB
/
change.py
File metadata and controls
90 lines (60 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import cv2
import numpy as np
import dlib
from glob import glob
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("./data/shape_predictor_68_face_landmarks.dat")
def read_imgs():
imgs = []
for img in sorted(glob("./imgs/*")):
imgs.append(cv2.imread(img))
return imgs
def to_video(name, width, height):
fps = 10
video_writer = cv2.VideoWriter(name, cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, (width, height))
return video_writer
def get_equation(x0, y0, x1, y1, pow_arg=1):
k = (y1 - y0) / (pow(x1, pow_arg) - pow(x0, pow_arg))
b = y0 - k * pow(x0, pow_arg)
def f(x):
return k * pow(x, pow_arg) + b
return f
def face_detector(img):
dets = detector(img, 1)
# only support one face
landmarks = []
shape = predictor(img, dets[0])
for p in shape.parts():
landmarks.append(np.array([p.x, p.y]))
return dets[0], np.array(landmarks)
def compose_img(name, frames_per_transformer, wait_frames, *imgs):
video_writer = to_video("{}.avi".format(name), imgs[0].shape[1], imgs[0].shape[0])
img_count = len(imgs)
for idx in range(img_count - 1):
from_img = imgs[idx]
to_img = imgs[idx + 1]
from_width = from_img.shape[1]
from_height = from_img.shape[0]
to_width = to_img.shape[1]
to_height = to_img.shape[0]
equation_pow = 1 if idx % 2 == 0 else 2
from_face_region, from_landmarks = face_detector(from_img)
to_face_region, to_landmarks = face_detector(to_img)
# 建立关键点的方程
homography_equation = get_equation(0, from_landmarks, frames_per_transformer - 1, to_landmarks, equation_pow)
for i in range(frames_per_transformer):
# 计算第一张图的最佳变换矩阵
from_H, _ = cv2.findHomography(from_landmarks, homography_equation(i))
# 计算第二张图的最佳变换矩阵
to_H, _ = cv2.findHomography(to_landmarks, homography_equation(i))
# 对第一张图作透视变换
from_dst = cv2.warpPerspective(from_img, from_H, (from_width, from_height), borderMode=cv2.BORDER_REPLICATE)
# 对第二张图作透视变换
to_dst = cv2.warpPerspective(to_img, to_H, (to_width, to_height), borderMode=cv2.BORDER_REPLICATE)
new_img = cv2.addWeighted(from_dst, 1 - ((i + 1) / frames_per_transformer), to_dst, (i + 1) / frames_per_transformer, 0)
video_writer.write(new_img)
for _ in range(wait_frames):
video_writer.write(to_img)
video_writer.release()
imgs = read_imgs()
compose_img("./result", 15, 5, *imgs)