Skip to content

Instantly share code, notes, and snippets.

@samhains
Created April 18, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhains/648ec70aab3d5a47920c95c5e0960ee3 to your computer and use it in GitHub Desktop.
Save samhains/648ec70aab3d5a47920c95c5e0960ee3 to your computer and use it in GitHub Desktop.
FaceSwap-DAN modifications for multiple faces
def getFaceKeypoints(img, detector, predictor, inputLandmarks=None, frameCounter=0):
shapes2D = []
# if inputLandmarks is None or frameCounter % 10 == 0:
#detekcja twarzy
dets = detector(img, 1)
if len(dets) == 0:
return None
if len(img.shape) > 2:
img = np.mean(img, axis=2)
for i, det in enumerate(dets):
inputLandmarks = bestFitRect(None, predictor.initLandmarks, [det.left(), det.top(), det.right(), det.bottom()])
shape2D = predictor.processImg(img[np.newaxis], inputLandmarks)
shape2D = shape2D.T
shapes2D.append(shape2D)
#transpozycja, zeby ksztalt byl 2 x n a nie n x 2, pozniej ulatwia to obliczenia
return shapes2D
while True:
frameCounter += 1
cameraImg = cap.read()[1]
shapes2D = utils.getFaceKeypoints(cameraImg, detector, predictor, prevShape2D, frameCounter=frameCounter)
if shapes2D is not None:
for shape2D in shapes2D:
prevShape2D = shape2D.T
#3D model parameter initialization
modelParams = projectionModel.getInitialParameters(
mean3DShape[:, idxs3D], shape2D[:, idxs2D])
#3D model parajeter optimization
modelParams = NonLinearLeastSquares.GaussNewton(
modelParams, projectionModel.residual, projectionModel.jacobian,
([mean3DShape[:, idxs3D], blendshapes[:, :, idxs3D]], shape2D[:, idxs2D]), verbose=0)
#rendering the model to an image
shape3D = utils.getShape3D(mean3DShape, blendshapes, modelParams)
renderedImg = renderer.render(shape3D)
#blending of the rendered face with the image
mask = np.copy(renderedImg[:, :, 0])
renderedImg = ImageProcessing.colorTransfer(cameraImg, renderedImg, mask)
cameraImg = ImageProcessing.blendImages(renderedImg, cameraImg, mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment