Spaces:
Runtime error
Runtime error
tori29umai
commited on
Commit
•
4d0b176
1
Parent(s):
db5d282
Update
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import numpy as np
|
|
5 |
import os
|
6 |
from collections import defaultdict
|
7 |
from skimage.color import deltaE_ciede2000, rgb2lab
|
|
|
8 |
|
9 |
def DoG_filter(image, kernel_size=0, sigma=1.0, k_sigma=2.0, gamma=1.5):
|
10 |
g1 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
|
@@ -23,7 +24,6 @@ def binarize_image(image):
|
|
23 |
_, binarized = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
24 |
return binarized
|
25 |
|
26 |
-
|
27 |
def process_XDoG(image_path):
|
28 |
kernel_size=0
|
29 |
sigma=1.4
|
@@ -38,12 +38,10 @@ def process_XDoG(image_path):
|
|
38 |
final_image = Image.fromarray(binarized_image)
|
39 |
return final_image
|
40 |
|
41 |
-
|
42 |
-
|
43 |
def replace_color(image, color_1, blur_radius=2):
|
44 |
data = np.array(image)
|
45 |
original_shape = data.shape
|
46 |
-
channels = original_shape[2] if len(original_shape) > 2 else 1
|
47 |
data = data.reshape(-1, channels)
|
48 |
color_1 = np.array(color_1)
|
49 |
matches = np.all(data[:, :3] == color_1, axis=1)
|
@@ -53,7 +51,7 @@ def replace_color(image, color_1, blur_radius=2):
|
|
53 |
while np.any(matches):
|
54 |
new_matches = np.zeros_like(matches)
|
55 |
match_num = np.sum(matches)
|
56 |
-
for i in range(len(data)):
|
57 |
if matches[i]:
|
58 |
x, y = divmod(i, original_shape[1])
|
59 |
neighbors = [
|
@@ -134,13 +132,9 @@ def line_color(image, mask, new_color):
|
|
134 |
data[mask, :3] = new_color
|
135 |
return Image.fromarray(data)
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
def main(image, lineart):
|
141 |
if image.mode != 'RGBA':
|
142 |
image = image.convert('RGBA')
|
143 |
-
|
144 |
lineart = lineart.point(lambda x: 0 if x < 200 else 255)
|
145 |
lineart = ImageOps.invert(lineart)
|
146 |
kernel = np.ones((3, 3), np.uint8)
|
@@ -154,47 +148,59 @@ def main(image, lineart):
|
|
154 |
replace_color_image = replace_color(filled_image, new_color_1, 2).convert('RGB')
|
155 |
return replace_color_image
|
156 |
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
)
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import os
|
6 |
from collections import defaultdict
|
7 |
from skimage.color import deltaE_ciede2000, rgb2lab
|
8 |
+
import zipfile
|
9 |
|
10 |
def DoG_filter(image, kernel_size=0, sigma=1.0, k_sigma=2.0, gamma=1.5):
|
11 |
g1 = cv2.GaussianBlur(image, (kernel_size, kernel_size), sigma)
|
|
|
24 |
_, binarized = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
25 |
return binarized
|
26 |
|
|
|
27 |
def process_XDoG(image_path):
|
28 |
kernel_size=0
|
29 |
sigma=1.4
|
|
|
38 |
final_image = Image.fromarray(binarized_image)
|
39 |
return final_image
|
40 |
|
|
|
|
|
41 |
def replace_color(image, color_1, blur_radius=2):
|
42 |
data = np.array(image)
|
43 |
original_shape = data.shape
|
44 |
+
channels = original_shape[2] if len(original_shape) > 2 else 1
|
45 |
data = data.reshape(-1, channels)
|
46 |
color_1 = np.array(color_1)
|
47 |
matches = np.all(data[:, :3] == color_1, axis=1)
|
|
|
51 |
while np.any(matches):
|
52 |
new_matches = np.zeros_like(matches)
|
53 |
match_num = np.sum(matches)
|
54 |
+
for i in range(len(data)):
|
55 |
if matches[i]:
|
56 |
x, y = divmod(i, original_shape[1])
|
57 |
neighbors = [
|
|
|
132 |
data[mask, :3] = new_color
|
133 |
return Image.fromarray(data)
|
134 |
|
135 |
+
def process_image(image, lineart):
|
|
|
|
|
|
|
136 |
if image.mode != 'RGBA':
|
137 |
image = image.convert('RGBA')
|
|
|
138 |
lineart = lineart.point(lambda x: 0 if x < 200 else 255)
|
139 |
lineart = ImageOps.invert(lineart)
|
140 |
kernel = np.ones((3, 3), np.uint8)
|
|
|
148 |
replace_color_image = replace_color(filled_image, new_color_1, 2).convert('RGB')
|
149 |
return replace_color_image
|
150 |
|
151 |
+
def zip_files(zip_files, zip_path):
|
152 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
153 |
+
for file_path in zip_files:
|
154 |
+
zipf.write(file_path, arcname=os.path.basename(file_path))
|
155 |
+
|
156 |
+
class webui:
|
157 |
+
def __init__(self):
|
158 |
+
self.demo = gr.Blocks()
|
159 |
+
|
160 |
+
def main(self, image_path):
|
161 |
+
image = Image.open(image_path).convert('RGBA')
|
162 |
+
image_name = os.path.basename(image_path)
|
163 |
+
alpha = image.getchannel('A') if image.mode == 'RGBA' else None
|
164 |
+
image.save(image_path)
|
165 |
+
image = Image.open(image_path).convert('RGBA')
|
166 |
+
rgb_image = image.convert('RGB')
|
167 |
+
lineart = process_XDoG(image_path).convert('L')
|
168 |
+
replace_color_image = process_image(rgb_image, lineart).convert('RGBA')
|
169 |
+
if alpha:
|
170 |
+
replace_color_image.putalpha(alpha)
|
171 |
+
replace_color_image_path = f"{image_name}_noline.png"
|
172 |
+
replace_color_image.save(replace_color_image_path)
|
173 |
+
lineart_image = lineart.convert('RGBA')
|
174 |
+
lineart_image.putalpha(255 - lineart_image.getchannel('A'))
|
175 |
+
lineart_image_path = f"{image_name}_lineart.png"
|
176 |
+
lineart_image.save(lineart_image_path)
|
177 |
+
|
178 |
+
zip_files_list = [replace_color_image_path, lineart_image_path]
|
179 |
+
zip_path = f"{image_name}.zip"
|
180 |
+
zip_files(zip_files_list, zip_path)
|
181 |
+
outputs = [replace_color_image, lineart_image]
|
182 |
+
return outputs, zip_path
|
183 |
+
|
184 |
+
def launch(self, share):
|
185 |
+
with self.demo:
|
186 |
+
with gr.Row():
|
187 |
+
with gr.Column():
|
188 |
+
input_image = gr.Image(type='filepath', image_mode="RGBA", label="Original Image")
|
189 |
+
submit = gr.Button(value="Start")
|
190 |
+
with gr.Row():
|
191 |
+
with gr.Column():
|
192 |
+
with gr.Tab("output"):
|
193 |
+
output_0 = gr.Gallery(format="png")
|
194 |
+
output_file = gr.File()
|
195 |
+
submit.click(
|
196 |
+
self.main,
|
197 |
+
inputs=[input_image],
|
198 |
+
outputs=[output_0, output_file]
|
199 |
+
)
|
200 |
+
|
201 |
+
self.demo.queue()
|
202 |
+
self.demo.launch(share=share)
|
203 |
+
|
204 |
+
if __name__ == "__main__":
|
205 |
+
ui = webui()
|
206 |
+
ui.launch(share=True)
|