Team Ai
Apppublic

Shellbrady/LivePortrait5

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
common.py50 linesDownload Raw Back to app
1import numpy as np2from numpy.linalg import norm as l2norm3#from easydict import EasyDict4 5class Face(dict):6 7    def __init__(self, d=None, **kwargs):8        if d is None:9            d = {}10        if kwargs:11            d.update(**kwargs)12        for k, v in d.items():13            setattr(self, k, v)14        # Class attributes15        #for k in self.__class__.__dict__.keys():16        #    if not (k.startswith('__') and k.endswith('__')) and not k in ('update', 'pop'):17        #        setattr(self, k, getattr(self, k))18 19    def __setattr__(self, name, value):20        if isinstance(value, (list, tuple)):21            value = [self.__class__(x)22                    if isinstance(x, dict) else x for x in value]23        elif isinstance(value, dict) and not isinstance(value, self.__class__):24            value = self.__class__(value)25        super(Face, self).__setattr__(name, value)26        super(Face, self).__setitem__(name, value)27 28    __setitem__ = __setattr__29 30    def __getattr__(self, name):31        return None32 33    @property34    def embedding_norm(self):35        if self.embedding is None:36            return None37        return l2norm(self.embedding)38 39    @property 40    def normed_embedding(self):41        if self.embedding is None:42            return None43        return self.embedding / self.embedding_norm44 45    @property 46    def sex(self):47        if self.gender is None:48            return None49        return 'M' if self.gender==1 else 'F'50