Spaces:
Runtime error
Runtime error
import gradio as gr | |
from PIL import Image, ImageFilter, ImageOps | |
import cv2 | |
import numpy as np | |
import os | |
from collections import defaultdict | |
from skimage.color import deltaE_ciede2000, rgb2lab | |
# XDoGフィルターを適用する関数 | |
def XDoG_filter(image, kernel_size=0, sigma=1.4, k_sigma=1.6, epsilon=0, phi=10, gamma=0.98): | |
epsilon /= 255 | |
g1 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma) | |
g2 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma * k_sigma) | |
dog = g1 - gamma * g2 | |
dog /= dog.max() | |
e = 1 + np.tanh(phi * (dog - epsilon)) | |
e[e >= 1] = 1 | |
return (e * 255).astype('uint8') | |
# 画像を二値化する関数 | |
def binarize_image(image): | |
_, binarized = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | |
return binarized | |
# XDoGフィルターを画像に適用し、その後二値化する関数 | |
def process_XDoG(image_path): | |
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) | |
xdog_image = XDoG_filter(image) | |
binarized_image = binarize_image(xdog_image) | |
return Image.fromarray(binarized_image) | |
# 主要な色を取得する関数 | |
def get_major_colors(image, threshold_percentage=0.01): | |
if image.mode != 'RGB': | |
image = image.convert('RGB') | |
color_count = defaultdict(int) | |
for pixel in image.getdata(): | |
color_count[pixel] += 1 | |
total_pixels = image.width * image.height | |
major_colors = [(color, count) for color, count in color_count.items() if (count / total_pixels) >= threshold_percentage] | |
return major_colors | |
# 色を統合する関数 | |
def consolidate_colors(major_colors, threshold): | |
colors_lab = [rgb2lab(np.array([[color]], dtype=np.float32)/255.0).reshape(3) for color, _ in major_colors] | |
i = 0 | |
while i < len(colors_lab): | |
j = i + 1 | |
while j < len(colors_lab): | |
if deltaE_ciede2000(colors_lab[i], colors_lab[j]) < threshold: | |
if major_colors[i][1] >= major_colors[j][1]: | |
major_colors[i] = (major_colors[i][0], major_colors[i][1] + major_colors[j][1]) | |
major_colors.pop(j) | |
colors_lab.pop(j) | |
else: | |
major_colors[j] = (major_colors[j][0], major_colors[j][1] + major_colors[i][1]) | |
major_colors.pop(i) | |
colors_lab.pop(i) | |
continue | |
j += 1 | |
i += 1 | |
return major_colors | |
# Gradioインターフェース用のメイン関数 | |
def gradio_interface(image): | |
image_path = 'temp_input_image.jpg' | |
image.save(image_path) | |
lineart = process_XDoG(image_path).convert('L') | |
processed_image = ImageOps.invert(lineart) | |
return processed_image | |
# Gradioアプリを設定し、起動する | |
iface = gr.Interface( | |
fn=gradio_interface, | |
inputs=gr.inputs.Image(type='pil', label="Original Image"), | |
outputs=gr.outputs.Image(type='pil', label="Processed Image"), | |
title="Line Art Removal", | |
description="画像をアップロードして線画を除去します。" | |
) | |
iface.launch() | |