Skip to content

Instantly share code, notes, and snippets.

View mowolf's full-sized avatar

Moritz Wolf mowolf

View GitHub Profile
@mowolf
mowolf / EU VAT ID Regex.md
Last active July 5, 2023 10:01
Regex to check European VAT ID

Regex

https://regex101.com/r/T81x4P/1

^((AT)U[0-9]{8}|(BE)[0-9]{10}|(BG)[0-9]{9,10}|(HR)[0-9]{11}|(CY)[0-9]{8}L|(CZ)[0-9]{8,10}|(DE)[0-9]{9}|(DK)[0-9]{8}|(EE)[0-9]{9}|(EL|GR)[0-9]{9}|(ES)[0-9A-Z][0-9]{7}[0-9A-Z]|(FI)[0-9]{8}|(FR)[0-9A-Z]{2}[0-9]{9}|(GB)([0-9]{9}([0-9]{3})?|[A-Z]{2}[0-9]{3})|(HU)[0-9]{8}|(IE)(([0-9]S[0-9]{5}L)|[0-9]{9}WI)|(IT)[0-9]{11}|(LT)([0-9]{9}|[0-9]{12})|(LU)[0-9]{8}|(LV)[0-9]{11}|(MT)[0-9]{8}|(NL)[0-9]{9}B[0-9]{2}|(PL)[0-9]{10}|(PT)[0-9]{9}|(RO)[0-9]{2,10}|(SE)[0-9]{12}|(SI)[0-9]{8}|(SK)[0-9]{10})$

Documentation

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
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
'''
# 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():
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 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:]
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
@mowolf
mowolf / reinsert_torch.py
Created April 13, 2020 16:07
reinsertion script in torch
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"]
@mowolf
mowolf / align_face.py
Created April 13, 2020 16:05
Alignement script for face preprocessing
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
"""
@mowolf
mowolf / backprop.py
Last active April 7, 2020 11:49
backprop.py
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"]