Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,21 @@ import os
|
|
| 2 |
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 3 |
import gradio as gr
|
| 4 |
import onnxruntime as ort
|
|
|
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
import numpy as np
|
| 7 |
import cv2
|
| 8 |
import psutil
|
| 9 |
import time
|
|
|
|
| 10 |
|
| 11 |
token_ = os.environ.get("HF_TOKEN")
|
| 12 |
model_repo_id = "heyh97791/NCF"
|
| 13 |
model_filename = "ncf.onnx"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
I18N = {
|
| 16 |
"lang_btn": {
|
|
@@ -68,8 +74,8 @@ I18N = {
|
|
| 68 |
"zh": "色彩参考图"
|
| 69 |
},
|
| 70 |
"settings_header": {
|
| 71 |
-
"en": "⚙️ Settings
|
| 72 |
-
"zh": "⚙️ 设置
|
| 73 |
},
|
| 74 |
"res_label": {
|
| 75 |
"en": "Max Resolution Limit",
|
|
@@ -79,6 +85,10 @@ I18N = {
|
|
| 79 |
"en": "Limits the input image size to speed up inference. 'Original' keeps original size.",
|
| 80 |
"zh": "限制输入图像尺寸以加快推理速度。选择“Original”将保持原图尺寸。"
|
| 81 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
"btn_run": {
|
| 83 |
"en": "🚀 Start Color Transfer",
|
| 84 |
"zh": "🚀 开始追色"
|
|
@@ -115,6 +125,30 @@ except Exception as e:
|
|
| 115 |
print(f"{e}")
|
| 116 |
ort_session = None
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def get_memory_mb():
|
| 119 |
pid = os.getpid()
|
| 120 |
return psutil.Process(pid).memory_info().rss / 1024 / 1024
|
|
@@ -157,7 +191,26 @@ def postprocess_tensor(tensor):
|
|
| 157 |
img = (img * 255.0).astype(np.uint8)
|
| 158 |
return img
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
if ort_session is None:
|
| 162 |
raise gr.Error("model do not exists")
|
| 163 |
|
|
@@ -181,9 +234,14 @@ def inference(content_img, style_img, resolution_selection):
|
|
| 181 |
|
| 182 |
result_img = postprocess_tensor(final_output_tensor)
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
return result_img
|
| 189 |
|
|
@@ -206,6 +264,7 @@ def toggle_language(current_lang):
|
|
| 206 |
gr.update(label=I18N["label_result"][target_lang]), # Image Label
|
| 207 |
gr.update(label=I18N["settings_header"][target_lang]), # Accordion Settings
|
| 208 |
gr.update(label=I18N["res_label"][target_lang], info=I18N["res_info"][target_lang]), # Radio Label
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
|
|
@@ -250,20 +309,26 @@ with gr.Blocks(css=custom_css, title="Color-Transfer") as demo:
|
|
| 250 |
info=I18N["res_info"]["en"],
|
| 251 |
interactive=True
|
| 252 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
with gr.Row():
|
| 255 |
btn_run = gr.Button(I18N["btn_run"]["en"], variant="primary")
|
| 256 |
|
| 257 |
with gr.Row():
|
| 258 |
output_result = gr.Image(label=I18N["label_result"]["en"], type="numpy", interactive=False, height=450, format="png")
|
| 259 |
-
|
| 260 |
btn_run.click(
|
| 261 |
-
fn=lambda: None,
|
| 262 |
inputs=None,
|
| 263 |
outputs=output_result
|
| 264 |
-
).then(
|
| 265 |
fn=inference,
|
| 266 |
-
inputs=[input_content, input_style, radio_res],
|
| 267 |
outputs=output_result
|
| 268 |
)
|
| 269 |
|
|
@@ -292,9 +357,10 @@ with gr.Blocks(css=custom_css, title="Color-Transfer") as demo:
|
|
| 292 |
input_content,
|
| 293 |
input_style,
|
| 294 |
btn_run,
|
| 295 |
-
|
| 296 |
-
acc_settings,
|
| 297 |
radio_res,
|
|
|
|
|
|
|
| 298 |
]
|
| 299 |
)
|
| 300 |
|
|
|
|
| 2 |
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 3 |
import gradio as gr
|
| 4 |
import onnxruntime as ort
|
| 5 |
+
import importlib.util
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
import numpy as np
|
| 8 |
import cv2
|
| 9 |
import psutil
|
| 10 |
import time
|
| 11 |
+
import sys
|
| 12 |
|
| 13 |
token_ = os.environ.get("HF_TOKEN")
|
| 14 |
model_repo_id = "heyh97791/NCF"
|
| 15 |
model_filename = "ncf.onnx"
|
| 16 |
+
file_path = "skin_protection.py"
|
| 17 |
+
pretrained_name = "selfie_multiclass_256x256.tflite"
|
| 18 |
+
|
| 19 |
+
protector_instance = None
|
| 20 |
|
| 21 |
I18N = {
|
| 22 |
"lang_btn": {
|
|
|
|
| 74 |
"zh": "色彩参考图"
|
| 75 |
},
|
| 76 |
"settings_header": {
|
| 77 |
+
"en": "⚙️ Settings",
|
| 78 |
+
"zh": "⚙️ 设置"
|
| 79 |
},
|
| 80 |
"res_label": {
|
| 81 |
"en": "Max Resolution Limit",
|
|
|
|
| 85 |
"en": "Limits the input image size to speed up inference. 'Original' keeps original size.",
|
| 86 |
"zh": "限制输入图像尺寸以加快推理速度。选择“Original”将保持原图尺寸。"
|
| 87 |
},
|
| 88 |
+
"prot_label": {
|
| 89 |
+
"en": "🧪 Enable Skin Protection (Experimental)",
|
| 90 |
+
"zh": "🧪 肤色保护 (实验性功能)"
|
| 91 |
+
},
|
| 92 |
"btn_run": {
|
| 93 |
"en": "🚀 Start Color Transfer",
|
| 94 |
"zh": "🚀 开始追色"
|
|
|
|
| 125 |
print(f"{e}")
|
| 126 |
ort_session = None
|
| 127 |
|
| 128 |
+
try:
|
| 129 |
+
script_path = hf_hub_download(
|
| 130 |
+
repo_id=model_repo_id,
|
| 131 |
+
filename=file_path,
|
| 132 |
+
token=token_
|
| 133 |
+
)
|
| 134 |
+
skin_model_path = hf_hub_download(
|
| 135 |
+
repo_id=model_repo_id,
|
| 136 |
+
filename=pretrained_name,
|
| 137 |
+
token=token_
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
spec = importlib.util.spec_from_file_location("skin_protection", script_path)
|
| 141 |
+
secret_module = importlib.util.module_from_spec(spec)
|
| 142 |
+
sys.modules["skin_protection"] = secret_module
|
| 143 |
+
spec.loader.exec_module(secret_module)
|
| 144 |
+
|
| 145 |
+
protector_instance = secret_module.SkinProtector(skin_model_path)
|
| 146 |
+
print("✅ Skin protection module loaded successfully.")
|
| 147 |
+
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"⚠️ Skin protection not loaded (Running in standard mode): {e}")
|
| 150 |
+
protector_instance = None
|
| 151 |
+
|
| 152 |
def get_memory_mb():
|
| 153 |
pid = os.getpid()
|
| 154 |
return psutil.Process(pid).memory_info().rss / 1024 / 1024
|
|
|
|
| 191 |
img = (img * 255.0).astype(np.uint8)
|
| 192 |
return img
|
| 193 |
|
| 194 |
+
|
| 195 |
+
def run_inference_numpy(content_np, style_np):
|
| 196 |
+
if ort_session is None: return content_np
|
| 197 |
+
c_in = content_np.astype(np.float32) / 255.0
|
| 198 |
+
c_in = c_in.transpose(2, 0, 1)[np.newaxis, :, :, :]
|
| 199 |
+
|
| 200 |
+
s_in = style_np.astype(np.float32) / 255.0
|
| 201 |
+
s_in = s_in.transpose(2, 0, 1)[np.newaxis, :, :, :]
|
| 202 |
+
|
| 203 |
+
input_feed = {input_names[0]: c_in, input_names[1]: s_in}
|
| 204 |
+
outputs = ort_session.run(None, input_feed)
|
| 205 |
+
out_tensor = outputs[0]
|
| 206 |
+
out_img = np.squeeze(out_tensor, axis=0)
|
| 207 |
+
out_img = np.clip(out_img, 0, 1)
|
| 208 |
+
out_img = out_img.transpose(1, 2, 0)
|
| 209 |
+
out_img = (out_img * 255.0).astype(np.uint8)
|
| 210 |
+
|
| 211 |
+
return out_img
|
| 212 |
+
|
| 213 |
+
def inference(content_img, style_img, resolution_selection, enable_skin_protection):
|
| 214 |
if ort_session is None:
|
| 215 |
raise gr.Error("model do not exists")
|
| 216 |
|
|
|
|
| 234 |
|
| 235 |
result_img = postprocess_tensor(final_output_tensor)
|
| 236 |
|
| 237 |
+
if enable_skin_protection and protector_instance is not None:
|
| 238 |
+
# print("skin protection activated")
|
| 239 |
+
result_img = protector_instance.process(
|
| 240 |
+
content_img=content_img,
|
| 241 |
+
style_img=style_img,
|
| 242 |
+
global_result_img=result_img,
|
| 243 |
+
inference_callback=run_inference_numpy
|
| 244 |
+
)
|
| 245 |
|
| 246 |
return result_img
|
| 247 |
|
|
|
|
| 264 |
gr.update(label=I18N["label_result"][target_lang]), # Image Label
|
| 265 |
gr.update(label=I18N["settings_header"][target_lang]), # Accordion Settings
|
| 266 |
gr.update(label=I18N["res_label"][target_lang], info=I18N["res_info"][target_lang]), # Radio Label
|
| 267 |
+
gr.update(label=I18N["prot_label"][target_lang]),
|
| 268 |
)
|
| 269 |
|
| 270 |
|
|
|
|
| 309 |
info=I18N["res_info"]["en"],
|
| 310 |
interactive=True
|
| 311 |
)
|
| 312 |
+
with gr.Row():
|
| 313 |
+
chk_skin = gr.Checkbox(
|
| 314 |
+
label=I18N["prot_label"]["en"],
|
| 315 |
+
value=False,
|
| 316 |
+
interactive=True
|
| 317 |
+
)
|
| 318 |
|
| 319 |
with gr.Row():
|
| 320 |
btn_run = gr.Button(I18N["btn_run"]["en"], variant="primary")
|
| 321 |
|
| 322 |
with gr.Row():
|
| 323 |
output_result = gr.Image(label=I18N["label_result"]["en"], type="numpy", interactive=False, height=450, format="png")
|
| 324 |
+
|
| 325 |
btn_run.click(
|
| 326 |
+
fn=lambda: None,
|
| 327 |
inputs=None,
|
| 328 |
outputs=output_result
|
| 329 |
+
).then(
|
| 330 |
fn=inference,
|
| 331 |
+
inputs=[input_content, input_style, radio_res, chk_skin],
|
| 332 |
outputs=output_result
|
| 333 |
)
|
| 334 |
|
|
|
|
| 357 |
input_content,
|
| 358 |
input_style,
|
| 359 |
btn_run,
|
| 360 |
+
acc_settings,
|
|
|
|
| 361 |
radio_res,
|
| 362 |
+
output_result,
|
| 363 |
+
chk_skin,
|
| 364 |
]
|
| 365 |
)
|
| 366 |
|