This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
forked from datitran/face2face-demo
-
Notifications
You must be signed in to change notification settings - Fork 22
/
generate_train_data.py
109 lines (87 loc) · 4.16 KB
/
generate_train_data.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
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import cv2
import dlib
import time
import argparse
import numpy as np
from imutils import video
DOWNSAMPLE_RATIO = 2
def reshape_for_polyline(array):
return np.array(array, np.int32).reshape((-1, 1, 2))
def main():
os.makedirs('original', exist_ok=True)
os.makedirs('landmarks', exist_ok=True)
os.makedirs('rendered', exist_ok=True)
cap = cv2.VideoCapture(args.filename)
fps = video.FPS().start()
count = 0
while cap.isOpened():
for tmp in range(10):
ret, frame = cap.read()
if ret is None:
break
if ret is None:
break
frame_resize = cv2.resize(frame, None, fx=1 / DOWNSAMPLE_RATIO, fy=1 / DOWNSAMPLE_RATIO)
gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY)
faces = detector(gray, 1)
black_image = np.zeros(frame.shape, np.uint8)
t = time.time()
# Perform if there is a face detected
if len(faces) == 1:
for face in faces:
detected_landmarks = predictor(gray, face).parts()
landmarks = [[p.x * DOWNSAMPLE_RATIO, p.y * DOWNSAMPLE_RATIO] for p in detected_landmarks]
jaw = reshape_for_polyline(landmarks[0:17])
left_eyebrow = reshape_for_polyline(landmarks[22:27])
right_eyebrow = reshape_for_polyline(landmarks[17:22])
nose_bridge = reshape_for_polyline(landmarks[27:31])
lower_nose = reshape_for_polyline(landmarks[30:35])
left_eye = reshape_for_polyline(landmarks[42:48])
right_eye = reshape_for_polyline(landmarks[36:42])
outer_lip = reshape_for_polyline(landmarks[48:60])
inner_lip = reshape_for_polyline(landmarks[60:68])
color = (255, 255, 255)
thickness = 3
cv2.polylines(black_image, [jaw], False, color, thickness)
cv2.polylines(black_image, [left_eyebrow], False, color, thickness)
cv2.polylines(black_image, [right_eyebrow], False, color, thickness)
cv2.polylines(black_image, [nose_bridge], False, color, thickness)
cv2.polylines(black_image, [lower_nose], True, color, thickness)
cv2.polylines(black_image, [left_eye], True, color, thickness)
cv2.polylines(black_image, [right_eye], True, color, thickness)
cv2.polylines(black_image, [outer_lip], True, color, thickness)
cv2.polylines(black_image, [inner_lip], True, color, thickness)
# Display the resulting frame
count += 1
print(count)
cv2.imwrite("original/{}.png".format(count), frame)
cv2.imwrite("landmarks/{}.png".format(count), black_image)
tmpimage = frame.copy()
tmpimage[black_image>0]=255
cv2.imwrite("rendered/{}.png".format(count), tmpimage)
# cv2.imshow('img',cv2.resize(cv2.addWeighted(frame, 0.5, black_image, 0.5, 0.0), None, fx=0.5, fy=0.5))
# cv2.imshow('img',cv2.resize(tmpimage, None, fx=0.5, fy=0.5))
fps.update()
print('[INFO] elapsed time: {:.2f}'.format(time.time() - t))
if count == args.number: # only take 400 photos
break
elif cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("No face detected")
fps.stop()
print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed()))
print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--file', dest='filename', type=str, help='Name of the video file.')
parser.add_argument('--num', dest='number', type=int, help='Number of train data to be created.')
parser.add_argument('--landmark-model', dest='face_landmark_shape_file', type=str, help='Face landmark model file.')
args = parser.parse_args()
# Create the face predictor and landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args.face_landmark_shape_file)
main()