wko_anime-face-similarity/main.py

41 lines
1.0 KiB
Python
Raw Normal View History

2023-01-15 12:40:35 +01:00
# Allows imports from the style transfer submodule
import sys
sys.path.append('DCT-Net')
import cv2
import os
import numpy as np
from source.cartoonize import Cartoonizer
2023-01-29 21:14:30 +01:00
2023-01-15 12:40:35 +01:00
def load_source(filename: str) -> np.ndarray:
return cv2.imread(filename)[...,::-1]
def find_and_crop_face(data: np.ndarray) -> np.ndarray:
2023-01-29 15:17:39 +01:00
data_gray = cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
face = face_cascade.detectMultiScale(data_gray, 1.3, 4)
face = max(face, key=len)
2023-01-29 21:14:30 +01:00
x, y, w, h = face
2023-01-29 15:17:39 +01:00
face = data[y:y + h, x:x + w]
2023-01-29 18:58:45 +01:00
return face
2023-01-15 12:40:35 +01:00
def compare_with_anime_characters(data: np.ndarray) -> int:
# TODO
return 1
2023-01-29 15:17:39 +01:00
def transfer_to_anime(img: np.ndarray):
2023-01-15 12:40:35 +01:00
algo = Cartoonizer(dataroot='damo/cv_unet_person-image-cartoon_compound-models')
return algo.cartoonize(img)
if __name__ == '__main__':
source = load_source('input.png')
source_face = find_and_crop_face(source)
source_face_anime = transfer_to_anime(source)
print(compare_with_anime_characters(source_face_anime))