faceswap icon indicating copy to clipboard operation
faceswap copied to clipboard

Sharpening the face before applying it

Open Kirin-kun opened this issue 8 years ago • 9 comments

Sharpen by multiplying every pixel by 2, and then subtracting the average value of the neighborhood from it.

I modified Convert_Masked.py and I find the face less blurry on closeups on hi-res pics, though it's a bit too sharp on normal/low res compared to the rest of the image.

YMMV.

def apply_new_face(self, image, new_face, image_mask, mat, image_size, size):
        base_image = numpy.copy( image )
        new_image = numpy.copy( image )
        cv2.warpAffine( new_face, mat, image_size, new_image, cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC, cv2.BORDER_TRANSPARENT )
        kernel = numpy.zeros( (9,9), numpy.float32)
        kernel[4,4] = 2.0
        boxFilter = numpy.ones( (9,9), numpy.float32) / 81.0
        kernel = kernel - boxFilter
        new_image = cv2.filter2D(new_image, -1, kernel)

Kirin-kun avatar Mar 11 '18 22:03 Kirin-kun

Plz make a PR so that people can test directly and give feedback

Clorr avatar Mar 12 '18 11:03 Clorr

@Kirin-kun Like you pointed it out yourself, the problem with this method is that the kernel will work for a very limited type of images and in most cases will lead to a very harsh border or over darkened blacks to compensate for the sharpening requirement.

I've personally tried many methods of sharpening and none of them actually are good enough to work across varied data sets. At some point there needs to be manual input of kernel information for us to have any control on what the sharpness will result in.


A well trained model will not need a sharpening pass if you ask me. The data is mapped pixel per pixel and as long as the training data is good and the loss is minimal, the output should look almost as good as the original in terms of clarity.


The code works and anyone can give it a shot ... but you can actually simplify this by doing this.

kernel = numpy.ones((3, 3)) * (-1)
kernel[1, 1] = 9
new_image = cv2.filter2D(new_image, -1, kernel)

Yields more or less the same result and you can easily modify the kernel array information to achieve different levels of sharpening.

JayantPythonLover avatar Mar 12 '18 12:03 JayantPythonLover

Something that has been mentioned elsewhere - would it make sense to write the converted faces into files before merging, and then merge back into the original images in a separate step?

Other image and video editing packages are much better suited to doing custom modifications, or even handling the merging itself, rather than jury-rigging random image editing functions into faceswap itself. Once you move beyond basic merging, it feels like re-inventing a less polished wheel.

deepfakesclub avatar Mar 13 '18 06:03 deepfakesclub

I've added some sharpening techniques here. Take a look.

https://github.com/deepfakes/faceswap/pull/285

JayantPythonLover avatar Mar 13 '18 07:03 JayantPythonLover

@deepfakesclub yeah it would be useful. For example there is a technique I want to try -superResize. For that I need those converted faces beforehand

Jack29913 avatar Mar 13 '18 07:03 Jack29913

Well, the main advantage of faceswap is that it does a lot of the job automatically.

There are Photoshop tutorials out there that help to face swap "realistically" but it's a bit time consuming and most people won't take the time to do that (especially for video)

But I guess you could add a few lines in there to load "new_image" if it exists in a preset directory under a preset name.

Kirin-kun avatar Mar 13 '18 11:03 Kirin-kun

Can we close this? (as #285 is merged)

Clorr avatar Mar 13 '18 23:03 Clorr

@Kirin-kun I agree you want auto-insertion, and that will be good enough for 90% of the users. But for the best results, you will have to do manual editing at some point, even if that means just deleting a really bad frame and redoing it or something.

After Effects has auto face outline tracking, I believe, and a much more powerful workflow for editing video. You can combine automated merging with manual correction on frames you desire. I'm no expert, and only tested some very basic function during a brief free trial.

GIMP is free and can do automated batch image processing with an extremely large number of functions. You need the free BIMP plugin. It can do batch sharpening, color correction, almost any filter it normally uses. GIMP also takes python/scripting, although I never played around with it. The downside is that the batch processing is slow, as I doubt it uses the GPU or is particularly optimized.

@Apollo122 Yes... I have my eye on a couple superresolution/post-processing scripts as well.

deepfakesclub avatar Mar 14 '18 03:03 deepfakesclub

We can close this, as any further discussion would deviate anyway.

Kirin-kun avatar Mar 14 '18 08:03 Kirin-kun