This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from git import Repo | |
class GitHelper: | |
def __init__(self, git_path: str, confirm: bool = False): | |
""" | |
Inits GitHelper Class | |
:param git_path: str path to .git/ folder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reinsert_aligned_into_tensor(aligned_tensor, tensor, alignment_params, device, margin=70): | |
# get params | |
desiredLeftEye = [float(alignment_params["desiredLeftEye"][0]), float(alignment_params["desiredLeftEye"][1])] | |
rotation_point = alignment_params["eyesCenter"] | |
# get original positions | |
l_face = aligned_tensor.shape[-1] | |
m1 = round(l_face * 0.5) | |
m2 = round(desiredLeftEye[0] * l_face) | |
# define the scale factor | |
scale = 1 / alignment_params["scale"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def align_face(img, keypoints, desiredLeftEye=(0.35, 0.35), desiredFaceShape=(256, 256)): | |
""" | |
Aligns a face so that left eye is at desiredLeftEye position | |
adapted from https://www.pyimagesearch.com/2017/05/22/face-alignment-with-opencv-and-python/ | |
:param img: cv2 image, cut previously to just contain face | |
:param keypoints: keypoints from dlib | |
:param desiredLeftEye: position of left eye in aligned image | |
:param desiredFaceShape: output image size | |
:return: aligned face image, cv2 | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torchgeometry as tgm | |
import numpy as np | |
import torch.nn.functional as F | |
from PIL import Image | |
def reinsert_aligned_into_tensor(aligned_tensor, tensor, alignment_params, device, margin=70): | |
# get params | |
desiredLeftEye = [float(alignment_params["desiredLeftEye"][0]), float(alignment_params["desiredLeftEye"][1])] | |
rotation_point = alignment_params["eyesCenter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reinsert_aligned_into_image(aligned_face_img, img, alignment_params, | |
keypoints, smoothEdge=20, margin=0, clean_merge=False): | |
""" | |
Reinserts aligned image into original image by inverting the affine transformation | |
:param margin: margin to add afterwards | |
:param smoothEdge: adding a gaussian blurred edge | |
:param alignment_params: dict with following content: angle, scale, shape, desiredLeftEye, eyesCenter | |
:param keypoints: keypoint dict from dlib | |
:param aligned_face_img: cv2 img | |
:param img: cv2 img original background image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this kills the gradient of fake_B_unaligned | |
# fake_B_unaligned should have the gradient of this alignement operation | |
# real_B_unaligned should have no gradient | |
def align_fake(self, margin=70): | |
# get params | |
desiredLeftEye = [float(self.alignment_params["desiredLeftEye"][0]), | |
float(self.alignment_params["desiredLeftEye"][1])] | |
rotation_point = self.alignment_params["eyesCenter"] | |
angle = -self.alignment_params["angle"] | |
h, w = self.fake_B.shape[2:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def clean_edges(img, pixels_to_ommit=4): | |
""" | |
This function removes the first #pixels_to_ommit pixels that are non black from every side of the image | |
(top, bottom, left, right) | |
:param img: image, cv2 | |
:param pixels_to_ommit: integer of how many pixels to remove | |
:return: image | |
""" | |
w, h, _ = img.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# clean artifacts on the edges | |
rewarped_img = clean_edges(rewarped_img) | |
# copy rewarped image into original image | |
y_size = rewarped_img.shape[1] | |
x_size = rewarped_img.shape[0] | |
for k in range(y_size): | |
for j in range(x_size): | |
y = y_start + k | |
x = x_start + j | |
if not (rewarped_img[k, j, :] == [0, 0, 0]).all(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kornia | |
import numpy as np | |
import torch.nn.functional as F | |
def get_M(): | |
''' | |
Provide mock trafo Matrix and original image sizes | |
:return: w_original, h_original, M | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_mask_from_points(img, keypoints, smooth=0, dilate_size=0): | |
""" | |
Returns image with TRANSPARENT (ALPHA CHANNEL = 0) face region | |
:param smooth: kernel size of gaussian blurring | |
:param dilate_size: % of image length to dilate mask with | |
:param img: Image | |
:param keypoints: dlib keypoints | |
:return: | |
""" | |
# create empty 2D mask filled with 0s |
OlderNewer