Skip to content

Instantly share code, notes, and snippets.

@fbparis
Last active November 28, 2020 16:34
Show Gist options
  • Save fbparis/3cc52838eb3da6ab8b293fff678bcb2a to your computer and use it in GitHub Desktop.
Save fbparis/3cc52838eb3da6ab8b293fff678bcb2a to your computer and use it in GitHub Desktop.
Remplace les visages par des anus
"""
Librement inspiré de https://twitter.com/IllumiReptilien/status/1325009440026877953
anus.png peut être récupéré ici : https://github.com/fbparis/floot/blob/main/anus.png
"""
# uncomment in colab
# !pip install mtcnn
import cv2
import mtcnn
import numpy as np
from google.colab.patches import cv2_imshow
def blend_transparent(face_img, overlay_t_img):
"""
See https://stackoverrun.com/fr/q/11258538
"""
# Split out the transparency mask from the colour info
overlay_img = overlay_t_img[:,:,:3] # Grab the BRG planes
overlay_mask = overlay_t_img[:,:,3:] # And the alpha plane
# Again calculate the inverse mask
background_mask = 255 - overlay_mask
# Turn the masks into three channel, so we can use them as weights
overlay_mask = cv2.cvtColor(overlay_mask, cv2.COLOR_GRAY2BGR)
background_mask = cv2.cvtColor(background_mask, cv2.COLOR_GRAY2BGR)
# Create a masked out face image, and masked out overlay
# We convert the images to floating point in range 0.0 - 1.0
face_part = (face_img * (1 / 255.0)) * (background_mask * (1 / 255.0))
overlay_part = (overlay_img * (1 / 255.0)) * (overlay_mask * (1 / 255.0))
# And finally just add them together, and rescale it back to an 8bit integer image
return np.uint8(cv2.addWeighted(face_part, 255.0, overlay_part, 255.0, 0.0))
img = cv2.imread('flics.jpg')
anus = cv2.imread('anus.png', cv2.IMREAD_UNCHANGED)
face_detector = mtcnn.MTCNN()
conf_t = 0.70
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = face_detector.detect_faces(img_rgb)
for res in results:
if res['confidence'] < conf_t:
continue
x, y, width, height = res['box']
mask = cv2.resize(anus, (width, height), interpolation = cv2.INTER_AREA)
img[y:y + height, x:x + width] = blend_transparent(img[y:y + height, x:x + width], mask)
cv2_imshow(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment