Spaces:
Runtime error
Runtime error
Hanxiao Xiang
commited on
Commit
·
542017d
1
Parent(s):
c9968b1
complete all TODOs
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ import cv2
|
|
| 15 |
from typing import Tuple
|
| 16 |
from PIL import Image
|
| 17 |
from io import BytesIO
|
| 18 |
-
|
| 19 |
import os
|
| 20 |
|
| 21 |
from Model.Model6.model6_inference import main as model6_inferencer
|
|
@@ -200,16 +200,25 @@ def model3_ext(x: np.ndarray, num_clusters=12):
|
|
| 200 |
"""主色调提取
|
| 201 |
:param x: 前端传入的图片
|
| 202 |
:param num_clusters: 聚类的数量
|
| 203 |
-
:return:
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
:param
|
| 210 |
:return:返回颜色名称
|
| 211 |
"""
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
def _cluster(img, NUM_CLUSTERS):
|
| 215 |
"""K-means 聚类提取主色调
|
|
@@ -238,25 +247,38 @@ def model3_ext(x: np.ndarray, num_clusters=12):
|
|
| 238 |
center = np.int32(center)
|
| 239 |
x_offset = 0
|
| 240 |
card = np.zeros((50, w, 3), dtype=np.uint8)
|
|
|
|
| 241 |
for c in np.argsort(clusters)[::-1]:
|
| 242 |
dx = int(clusters[c] * w)
|
| 243 |
-
|
| 244 |
g = center[c][1]
|
| 245 |
-
|
| 246 |
cv2.rectangle(card, (x_offset, 0), (x_offset + dx, 50),
|
| 247 |
-
(int(
|
|
|
|
| 248 |
x_offset += dx
|
| 249 |
|
| 250 |
-
return card, resized_f
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
resized_x, resized_f = resize_image(x)
|
| 253 |
-
card, resized_f = _cluster(resized_x, num_clusters)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
if resized_f:
|
| 255 |
resized_f = "图片尺寸已被修改至合适大小"
|
| 256 |
else:
|
| 257 |
resized_f = "图片尺寸无需修改"
|
| 258 |
|
| 259 |
-
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
def model4_clo(x_path: str):
|
|
@@ -306,12 +328,14 @@ with gr.Blocks() as demo:
|
|
| 306 |
with gr.Tab('主色调提取'):
|
| 307 |
with gr.Row():
|
| 308 |
with gr.Column():
|
| 309 |
-
# TODO: 参照“蒙娜丽莎”尝试修改前端界面[not important]
|
| 310 |
-
# TODO: 修改布局,使其更美观[moderately important]
|
| 311 |
model3_input = gr.Image(height=400, image_mode='RGBA')
|
| 312 |
model3_slider = gr.Slider(minimum=1, maximum=20, step=1, value=12,
|
| 313 |
min_width=400, label="聚类数量")
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
model3_button = gr.Button("开始提取")
|
| 316 |
with gr.Tab("廓形识别"):
|
| 317 |
with gr.Row():
|
|
@@ -326,6 +350,8 @@ with gr.Blocks() as demo:
|
|
| 326 |
|
| 327 |
model1_button.click(model1_det, inputs=model1_input, outputs=[model1_output_img, running_info])
|
| 328 |
model2_button.click(model2_rem, inputs=model2_input, outputs=[model2_output_img, running_info])
|
| 329 |
-
model3_button.click(model3_ext,
|
|
|
|
|
|
|
| 330 |
model4_button.click(model4_clo, inputs=model4_input, outputs=[model4_output_img, model4_output_df, running_info])
|
| 331 |
demo.launch()
|
|
|
|
| 15 |
from typing import Tuple
|
| 16 |
from PIL import Image
|
| 17 |
from io import BytesIO
|
| 18 |
+
from skimage import color
|
| 19 |
import os
|
| 20 |
|
| 21 |
from Model.Model6.model6_inference import main as model6_inferencer
|
|
|
|
| 200 |
"""主色调提取
|
| 201 |
:param x: 前端传入的图片
|
| 202 |
:param num_clusters: 聚类的数量
|
| 203 |
+
:return: 返回处理后的图片,是否进行了resize的标志,颜色列表"""
|
| 204 |
|
| 205 |
+
def _find_name(r, g, b):
|
| 206 |
+
"""根据颜色值查找颜色名称。
|
| 207 |
+
:param r: 红色值
|
| 208 |
+
:param g: 绿色值
|
| 209 |
+
:param b: 蓝色值
|
| 210 |
:return:返回颜色名称
|
| 211 |
"""
|
| 212 |
+
# turn RGB to Lab
|
| 213 |
+
lab = color.rgb2lab([[r / 255, g / 255, b / 255]])[0]
|
| 214 |
+
for i in range(len(df)):
|
| 215 |
+
# culcuate the minimum chromatic distance
|
| 216 |
+
df['distance'] = np.sqrt((df['L'] - lab[0]) ** 2 + (df['a'] - lab[1]) ** 2 + (df['b'] - lab[2]) ** 2)
|
| 217 |
+
# find the color name, whose chromatic distance is the minimum, and the corresponding distance
|
| 218 |
+
name = df[df['distance'] == df['distance'].min()]['name'].values[0]
|
| 219 |
+
distance = df[df['distance'] == df['distance'].min()]['distance'].values[0]
|
| 220 |
+
|
| 221 |
+
return name, distance
|
| 222 |
|
| 223 |
def _cluster(img, NUM_CLUSTERS):
|
| 224 |
"""K-means 聚类提取主色调
|
|
|
|
| 247 |
center = np.int32(center)
|
| 248 |
x_offset = 0
|
| 249 |
card = np.zeros((50, w, 3), dtype=np.uint8)
|
| 250 |
+
color_list = []
|
| 251 |
for c in np.argsort(clusters)[::-1]:
|
| 252 |
dx = int(clusters[c] * w)
|
| 253 |
+
r = center[c][0]
|
| 254 |
g = center[c][1]
|
| 255 |
+
b = center[c][2]
|
| 256 |
cv2.rectangle(card, (x_offset, 0), (x_offset + dx, 50),
|
| 257 |
+
(int(r), int(g), int(b)), -1)
|
| 258 |
+
color_list.append([r, g, b, str(round(clusters[c]*100, 2)) + '%'])
|
| 259 |
x_offset += dx
|
| 260 |
|
| 261 |
+
return card, resized_f, color_list
|
| 262 |
+
|
| 263 |
+
file = '中国传统色_集合.xlsx'
|
| 264 |
+
df = pd.read_excel(file, sheet_name='Sheet1')[['name', 'L', 'a', 'b']]
|
| 265 |
|
| 266 |
resized_x, resized_f = resize_image(x)
|
| 267 |
+
card, resized_f, c_list = _cluster(resized_x, num_clusters)
|
| 268 |
+
|
| 269 |
+
for c in c_list:
|
| 270 |
+
c_name, c_distance = _find_name(c[0], c[1], c[2])
|
| 271 |
+
c.append(c_name)
|
| 272 |
+
c.append(c_distance)
|
| 273 |
+
|
| 274 |
if resized_f:
|
| 275 |
resized_f = "图片尺寸已被修改至合适大小"
|
| 276 |
else:
|
| 277 |
resized_f = "图片尺寸无需修改"
|
| 278 |
|
| 279 |
+
c_df = pd.DataFrame(c_list, columns=['R', 'G', 'B', '比例', '颜色名称', '色差ΔE'])
|
| 280 |
+
|
| 281 |
+
return card, resized_f, c_df
|
| 282 |
|
| 283 |
|
| 284 |
def model4_clo(x_path: str):
|
|
|
|
| 328 |
with gr.Tab('主色调提取'):
|
| 329 |
with gr.Row():
|
| 330 |
with gr.Column():
|
|
|
|
|
|
|
| 331 |
model3_input = gr.Image(height=400, image_mode='RGBA')
|
| 332 |
model3_slider = gr.Slider(minimum=1, maximum=20, step=1, value=12,
|
| 333 |
min_width=400, label="聚类数量")
|
| 334 |
+
with gr.Column():
|
| 335 |
+
model3_output_img = gr.Image(height=100)
|
| 336 |
+
model3_output_df = gr.DataFrame(headers=['R', 'G', 'B', '比例', '颜色名称', '色差ΔE'],
|
| 337 |
+
datatype=['number', 'number', 'number', 'str', 'str', 'number'])
|
| 338 |
+
|
| 339 |
model3_button = gr.Button("开始提取")
|
| 340 |
with gr.Tab("廓形识别"):
|
| 341 |
with gr.Row():
|
|
|
|
| 350 |
|
| 351 |
model1_button.click(model1_det, inputs=model1_input, outputs=[model1_output_img, running_info])
|
| 352 |
model2_button.click(model2_rem, inputs=model2_input, outputs=[model2_output_img, running_info])
|
| 353 |
+
model3_button.click(model3_ext,
|
| 354 |
+
inputs=[model3_input, model3_slider],
|
| 355 |
+
outputs=[model3_output_img, running_info, model3_output_df])
|
| 356 |
model4_button.click(model4_clo, inputs=model4_input, outputs=[model4_output_img, model4_output_df, running_info])
|
| 357 |
demo.launch()
|