-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeDataset.py
81 lines (62 loc) · 2.17 KB
/
makeDataset.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
import cv2
import os.path as op
from faceMesh import FaceMesh
from utils.arguement import drawArgs
from cameraFeed import Camera
from videoFeed import Video
from utils.utils import create_dir, \
save_json, setup_logging
logger = setup_logging()
def main(root_dir='data/detected_mesh'):
"""save the face mesh points into json files
Args:
root_dir (str, optional): root directory to save the dataset.
Defaults to 'data/detected_mesh'.
video_path (str, optional): path to the video or camera id.
Defaults to None.
"""
# read the video or camera
cap = None
args = drawArgs()
faceMesh = FaceMesh(static_mode=False)
# if we use camera feed, we need to save the video too
if args.camera:
cap = Camera()
save_dir = op.join(root_dir, "webcam")
frame_width = int(cap.source.get(3))
frame_height = int(cap.source.get(4))
# video output object to save the video
out = cv2.VideoWriter(
'data/webcam.avi',
cv2.VideoWriter_fourcc('M','J','P','G'),
30, (frame_width,frame_height)
)
else:
cap = Video(args.video_path)
video_name = op.basename(args.video_path)
video_name = op.splitext(video_name)[0]
save_dir = op.join(root_dir, video_name)
create_dir(save_dir)
frames = cap.getImage()
frame_counter = 0
for img in frames:
if img is None: # end of frames
break
if args.camera:
out.write(img)
# get the mesh
face_mesh, img = faceMesh.get_mesh(img, draw=True)
json_file_name = "frame_{}.json".format(frame_counter)
json_file_name = op.join(save_dir, json_file_name)
# save the mesh and display the video
save_json(json_file_name, face_mesh)
frame_counter += 1
cv2.imshow('Face Mesh', img)
cv2.namedWindow('Face Mesh',cv2.WINDOW_NORMAL)
if cv2.waitKey(30) == 27:
break # esc to quit
cap.source.release()
if args.camera:
out.release()
if __name__ == '__main__':
main()