tori29umai commited on
Commit
ee14fdf
1 Parent(s): 3ea2029

Add application file

Browse files
Files changed (1) hide show
  1. app.py +78 -4
app.py CHANGED
@@ -1,7 +1,81 @@
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from PIL import Image, ImageFilter, ImageOps
3
+ import cv2
4
+ import numpy as np
5
+ import os
6
+ from collections import defaultdict
7
+ from skimage.color import deltaE_ciede2000, rgb2lab
8
 
9
+ # XDoGフィルターを適用する関数
10
+ def XDoG_filter(image, kernel_size=0, sigma=1.4, k_sigma=1.6, epsilon=0, phi=10, gamma=0.98):
11
+ epsilon /= 255
12
+ g1 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
13
+ g2 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma * k_sigma)
14
+ dog = g1 - gamma * g2
15
+ dog /= dog.max()
16
+ e = 1 + np.tanh(phi * (dog - epsilon))
17
+ e[e >= 1] = 1
18
+ return (e * 255).astype('uint8')
19
 
20
+ # 画像を二値化する関数
21
+ def binarize_image(image):
22
+ _, binarized = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
23
+ return binarized
24
+
25
+ # XDoGフィルターを画像に適用し、その後二値化する関数
26
+ def process_XDoG(image_path):
27
+ image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
28
+ xdog_image = XDoG_filter(image)
29
+ binarized_image = binarize_image(xdog_image)
30
+ return Image.fromarray(binarized_image)
31
+
32
+ # 主要な色を取得する関数
33
+ def get_major_colors(image, threshold_percentage=0.01):
34
+ if image.mode != 'RGB':
35
+ image = image.convert('RGB')
36
+ color_count = defaultdict(int)
37
+ for pixel in image.getdata():
38
+ color_count[pixel] += 1
39
+ total_pixels = image.width * image.height
40
+ major_colors = [(color, count) for color, count in color_count.items() if (count / total_pixels) >= threshold_percentage]
41
+ return major_colors
42
+
43
+ # 色を統合する関数
44
+ def consolidate_colors(major_colors, threshold):
45
+ colors_lab = [rgb2lab(np.array([[color]], dtype=np.float32)/255.0).reshape(3) for color, _ in major_colors]
46
+ i = 0
47
+ while i < len(colors_lab):
48
+ j = i + 1
49
+ while j < len(colors_lab):
50
+ if deltaE_ciede2000(colors_lab[i], colors_lab[j]) < threshold:
51
+ if major_colors[i][1] >= major_colors[j][1]:
52
+ major_colors[i] = (major_colors[i][0], major_colors[i][1] + major_colors[j][1])
53
+ major_colors.pop(j)
54
+ colors_lab.pop(j)
55
+ else:
56
+ major_colors[j] = (major_colors[j][0], major_colors[j][1] + major_colors[i][1])
57
+ major_colors.pop(i)
58
+ colors_lab.pop(i)
59
+ continue
60
+ j += 1
61
+ i += 1
62
+ return major_colors
63
+
64
+ # Gradioインターフェース用のメイン関数
65
+ def gradio_interface(image):
66
+ image_path = 'temp_input_image.jpg'
67
+ image.save(image_path)
68
+ lineart = process_XDoG(image_path).convert('L')
69
+ processed_image = ImageOps.invert(lineart)
70
+ return processed_image
71
+
72
+ # Gradioアプリを設定し、起動する
73
+ iface = gr.Interface(
74
+ fn=gradio_interface,
75
+ inputs=gr.inputs.Image(type='pil', label="Original Image"),
76
+ outputs=gr.outputs.Image(type='pil', label="Processed Image"),
77
+ title="Line Art Removal",
78
+ description="画像をアップロードして線画を除去します。"
79
+ )
80
+
81
+ iface.launch()