in avhubert/preparation/detect_landmark.py [0:0]
def detect_face_landmarks(face_predictor_path, cnn_detector_path, root_dir, landmark_dir, flist_fn, rank, nshard):
def detect_landmark(image, detector, cnn_detector, predictor):
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
rects = detector(gray, 1)
if len(rects) == 0:
rects = cnn_detector(gray)
rects = [d.rect for d in rects]
coords = None
for (_, rect) in enumerate(rects):
shape = predictor(gray, rect)
coords = np.zeros((68, 2), dtype=np.int32)
for i in range(0, 68):
coords[i] = (shape.part(i).x, shape.part(i).y)
return coords
detector = dlib.get_frontal_face_detector()
cnn_detector = dlib.cnn_face_detection_model_v1(cnn_detector_path)
predictor = dlib.shape_predictor(face_predictor_path)
input_dir = root_dir #
output_dir = landmark_dir #
fids = [ln.strip() for ln in open(flist_fn).readlines()]
num_per_shard = math.ceil(len(fids)/nshard)
start_id, end_id = num_per_shard*rank, num_per_shard*(rank+1)
fids = fids[start_id: end_id]
print(f"{len(fids)} files")
for fid in tqdm(fids):
output_fn = os.path.join(output_dir, fid+'.pkl')
video_path = os.path.join(input_dir, fid+'.mp4')
frames = load_video(video_path)
landmarks = []
for frame in frames:
landmark = detect_landmark(frame, detector, cnn_detector, predictor)
landmarks.append(landmark)
os.makedirs(os.path.dirname(output_fn), exist_ok=True)
pickle.dump(landmarks, open(output_fn, 'wb'))
return