raycv/tool/process/detect_landmark.py
2020-08-30 14:44:40 +08:00

33 lines
865 B
Python

import os
import numpy as np
import dlib
from pathlib import Path
from PIL import Image
import sys
imagepaths = Path(sys.argv[1])
print(imagepaths)
phase = imagepaths.name
print(phase)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
if not os.path.isdir(phase):
os.makedirs(phase)
for ip in imagepaths.glob("*.jpg"):
img = np.asarray(Image.open(ip))
img.setflags(write=True)
dets = detector(img, 1)
if len(dets) > 0:
shape = predictor(img, dets[0])
points = np.empty([68, 2], dtype=int)
for b in range(68):
points[b, 0] = shape.part(b).x
points[b, 1] = shape.part(b).y
save_name = os.path.join(phase, ip.name[:-4] + '.txt')
np.savetxt(save_name, points, fmt='%d', delimiter=',')
else:
print(ip)