Issue #211 Because Reactor's Mask Correction is based on sd-face-editor masking script - we need another naming of active folders +VersionUP (0.5.1 beta3)
37 lines
991 B
Python
37 lines
991 B
Python
from abc import ABC, abstractmethod
|
|
from typing import Tuple
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
class MaskGenerator(ABC):
|
|
@abstractmethod
|
|
def name(self) -> str:
|
|
pass
|
|
|
|
@abstractmethod
|
|
def generate_mask(
|
|
self,
|
|
face_image: np.ndarray,
|
|
face_area_on_image: Tuple[int, int, int, int],
|
|
**kwargs,
|
|
) -> np.ndarray:
|
|
pass
|
|
|
|
@staticmethod
|
|
def mask_non_face_areas(image: np.ndarray, face_area_on_image: Tuple[int, int, int, int]) -> np.ndarray:
|
|
left, top, right, bottom = face_area_on_image
|
|
image = image.copy()
|
|
image[:top, :] = 0
|
|
image[bottom:, :] = 0
|
|
image[:, :left] = 0
|
|
image[:, right:] = 0
|
|
return image
|
|
|
|
@staticmethod
|
|
def calculate_mask_coverage(mask: np.ndarray):
|
|
gray_mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
|
|
non_black_pixels = np.count_nonzero(gray_mask)
|
|
total_pixels = gray_mask.size
|
|
return non_black_pixels / total_pixels
|