forked from zhangns1014/Face-Swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.py
96 lines (88 loc) · 3.02 KB
/
loader.py
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
91
92
93
94
95
96
'''
Filename: loader.py
Author: Zheyuan Xie
Date created: 2018-12-18
'''
import pickle
import cv2
from PythonSDK.facepp import API,File
from getLandmarks import get_landmarks_facepp, get_landmarks
from smoothTraj import smooth_landmark_traj
def loadvideo(filename):
cap = cv2.VideoCapture(filename)
images = []
while(True):
ret, frame = cap.read()
if ret == False:
break
images.append(frame)
cap.release()
return images
def savelandmarks_dlib(filename):
video = loadvideo(filename)
landmarks = []
print('Number of Frames: %d'%(len(video)))
cnt = 0
for image in video:
landmarks.append(get_landmarks(image))
cnt = cnt + 1
print('%f%%'%(cnt/len(video)*100))
# Dump landmarks to file
with open(filename.split('.')[0]+'.landmarks', 'wb') as f:
pickle.dump(landmarks, f)
def savelandmarks_facepp(filename):
api = API()
video = loadvideo(filename)
landmarks = []
print('Number of Frames: %d'%(len(video)))
cnt = 0
for image in video:
cv2.imwrite('temp.jpg',image)
landmarks.append(get_landmarks_facepp(File('./temp.jpg'),api))
cnt = cnt + 1
print('%f%%'%(cnt/len(video)*100))
# Dump landmarks to file
with open(filename.split('.')[0]+'.facepp', 'wb') as f:
pickle.dump(landmarks, f)
def loadlandmarks(filename):
# Load landmarks from file
with open(filename.split('.')[0]+'.landmarks', 'rb') as f:
landmarks = pickle.load(f)
return landmarks
def loadlandmarks_facepp(filename):
# Load landmarks from file
with open(filename.split('.')[0]+'.facepp', 'rb') as f:
landmarks = pickle.load(f)
return landmarks
def vislandmarks(filename, play = False, use_facepp = False, smooth = False):
video = loadvideo(filename)
if use_facepp:
landmarks_facepp = loadlandmarks_facepp(filename)
landmarks = loadlandmarks(filename)
if smooth:
landmarks = smooth_landmark_traj(landmarks)
n_frames = len(video)
for i in range(n_frames):
if landmarks[i] is not None:
for groups in landmarks[i].astype(int):
cv2.circle(video[i], (groups[0],groups[1]), 3, (255, 0, 0), 2)
if use_facepp and landmarks_facepp[i] is not None:
for groups in landmarks_facepp[i].astype(int):
cv2.circle(video[i], (groups[0],groups[1]), 3, (0, 255, 0), 2)
if play:
cv2.imshow('Landmarks',video[i])
cv2.waitKey(20)
return video
if __name__ == "__main__":
easy1 = 'Datasets/Easy/FrankUnderwood.mp4'
easy2 = 'Datasets/Easy/MrRobot.mp4'
easy3 = 'Datasets/Easy/JonSnow.mp4'
medium1 = 'Datasets/Medium/LucianoRosso1.mp4'
medium2 = 'Datasets/Medium/LucianoRosso2.mp4'
medium3 = 'Datasets/Medium/LucianoRosso3.mp4'
hard1 = 'Datasets/Hard/Joker.mp4'
hard2 = 'Datasets/Hard/LeonardoDiCaprio.mp4'
xi = 'Datasets/xidada.mp4'
# savelandmarks_dlib(xi)
# savelandmarks_facepp(xi)
vislandmarks(easy1, play=True, use_facepp=True)