Landmarks not matching with CNN/MMOD detector
From: https://github.com/deepfakes/faceswap-playground/issues/43
Aligning faces does not work on some faces found with the cnn option. See below for the landmarks returned.
Can someone tell where the problem come from?
[Images Removed]
Code to reproduce (put in plugins/Extract_Align.py):
def extract(self, image, face, size):
if face.landmarks == None:
print("Warning! landmarks not found. Switching to crop!")
return cv2.resize(face.image, (size, size))
# Draws landmarks for debug
for (x, y) in face.landmarksAsXY():
cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
alignment = get_align_mat( face )
return self.transform( image, alignment, size, 48 )
why pictures zooming ?
cnn: [Image Removed] hog: [Image Removed]
need mp4 file ?
There is no specific picture zooming here, it is the extract as it is done by the code just now with the source you can find on the original issue
The question is to know why the landmarks are incorrect here, and also what we can do to correct this.
But as you say, there may be another problem which is that the face is framed too narrow. However, I don't think it is the origin of the landmarks defect as the landmarks are calculated on the original image (in lib/faces_detect.py)
This is weird:
return [pose_predictor(face_image, face_location) for face_location in face_locations]
The shape predictor receives as an argument the face box. According to dlib examples, it tries to get the landmark points inside that box, not the full image.
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
http://dlib.net/face_landmark_detection.py.html
That would explain why the landmarks are wrong if the cnn model extracts a zoomed-in image.
Passing the face box to the predictor outputs the expected(and wrong) result:
[Image Removed]
Not passing the box gets the landmarks right as expected:
[Image Removed]
But then the unexpected happened. Pass the hog face box to the predictor, it just ignores it.
[Image Removed]
so how to fix problems?
fakeapp align without zoom glitches:
[Image Removed]
may be use https://github.com/1adrianb/face-alignment as in fakeapp ?
ah no pytorch for windows ;(
I think problem in landmarks detector, not face detector. FaceSwap using ageitgey/face_recognition fakeapp using 1adrianb/face-alignment
Thank you @LordVulkan for the awesome report!
The shape predictor receives as an argument the face box. According to dlib examples, it tries to get the landmark points inside that box, not the full image.
- I think it is intended, because if you give an image with many faces the landmarks detector would be lost I presume.
- From what you show, there is a slight difference between the box returned by hog and cnn, which I wasn't aware of. Maybe that explains also the "zooming" that @iperov reports?
- My first option would be to try to enlarge the box returned by the cnn detector and see if it solves our problem. Also we can ask @ageitgey to have a look as it would be an enhancement for all
face_recognitionusers.
I have tried with 1adrianb/face-alignment and it seems to get the right landmarks, but I don't know why it is horribly slow even with CUDA.
Have you an idea on the zooming effect noticed by @iperov ? Because the 1adrianb/face-alignment repo also uses mmod detector
1adrianb also gets wrong the face box and for some reason it is a different box than the one in face_recognition. It also doesn't affect the landmarks.
Comparison of different face boxes:
Red: Extracted by face_recognition using cnn. Green: Extracted by face-alignment using cnn. White: Extracted with hog. Same result in both repos.
[Image Removed]
yeah problem in face landmarks detector.
face-alignment better, but cant build it for windows :((
Strange that the cnn detector returns a different box. Where did you get the values for the box? Also, there is a preprocessing of the recognized face done in 1adrianb repo:
center = torch.FloatTensor(
[d.right() - (d.right() - d.left()) / 2.0, d.bottom() -
(d.bottom() - d.top()) / 2.0])
center[1] = center[1] - (d.bottom() - d.top()) * 0.12
scale = (d.right() - d.left() + d.bottom() - d.top()) / 195.0
inp = crop(image, center, scale)
inp = torch.from_numpy(inp.transpose((2, 0, 1))).float().div(255.0).unsqueeze_(0)
However my skills are too low to understand what this does. Have you some idea?
The face_location object is the box itself, it is a 4-tuple: (top,right,bottom,left) face-alignment seems to be centering and resizing the face box before calculating landmarks, I will take a look at it.
Comparison of face-alignment and face_recognition landmarks:
face_recognition landmarks with cnn face box: [Image Removed]
face_recognition landmarks with hog face box: [Image Removed]
face-alignment landmarks with cnn face box: [Image Removed]
face-alignment landmarks with hog face box: [Image Removed]
Yes, from what I understand it takes the center, but i don't understand the line with the 0.12. It moves the center a bit or something?
Then the scale is computed so that the box is 195 wide, doesn't it?
Then the crop method returns a 256 wide image, so maybe it adds some margin around, which could explain that it gets the right landmarks as the face is not cropped too narrow?
I let you have a look when possible, no hurry ;-)
I have just replaced the landmarks for the ones from face-alignment and it works a lot better, but it is also notably slower. I leave it here if someone wants to try it, don't forget to import face_alignment
face_detect.py
def detect_faces(frame, model="hog"):
face_locations = face_recognition.face_locations(frame, model=model)
if model=="cnn":
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D,enable_cuda=True,enable_cudnn=True,use_cnn_face_detector=True)
else:
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D,enable_cuda=True,enable_cudnn=True,use_cnn_face_detector=False)
raw_landmarks = fa.get_landmarks(frame,all_faces=True)
landmarksXY = []
if raw_landmarks is not None:
for raw_landmark in raw_landmarks:
landmarksXY.append([(int(p[0]), int(p[1])) for p in raw_landmark])
for ((y, right, bottom, x), landmarks) in zip(face_locations, landmarksXY):
yield DetectedFace(frame[y: bottom, x: right], x, right - x, y, bottom - y, landmarks=landmarks,landmarksXY=landmarks)
@LordVulkan thx, only need to build pytorch for me :D
Take a look at this:
https://github.com/peterjc123/pytorch-scripts
i dont want deal with conda, trying to build from python...
I suggest open 2DFAN-4.pth.tar in torch, research network structure and tensors, and port it to keras. :D
is this violates https://github.com/1adrianb/face-alignment/blob/master/LICENSE ?