Spaces:
Running
Running
Commit
·
b2cb163
1
Parent(s):
b2c5726
update
Browse files- demo/assets/color_list_CN.csv +2 -1
- demo/assets/color_list_EN.csv +2 -1
- demo/locals.py +24 -0
- demo/processor.py +9 -0
- demo/ui.py +41 -25
- hivision/creator/retinaface/inference.py +14 -34
demo/assets/color_list_CN.csv
CHANGED
|
@@ -3,4 +3,5 @@ Name,Hex
|
|
| 3 |
白色,ffffff
|
| 4 |
红色,d74532
|
| 5 |
黑色,000000
|
| 6 |
-
深蓝色,4b6190
|
|
|
|
|
|
| 3 |
白色,ffffff
|
| 4 |
红色,d74532
|
| 5 |
黑色,000000
|
| 6 |
+
深蓝色,4b6190
|
| 7 |
+
浅灰色,f2f0f0
|
demo/assets/color_list_EN.csv
CHANGED
|
@@ -3,4 +3,5 @@ Blue,628bce
|
|
| 3 |
White,ffffff
|
| 4 |
Red,d74532
|
| 5 |
Black,000000
|
| 6 |
-
Dark Blue,4b6190
|
|
|
|
|
|
| 3 |
White,ffffff
|
| 4 |
Red,d74532
|
| 5 |
Black,000000
|
| 6 |
+
Dark Blue,4b6190
|
| 7 |
+
Light Gray,f2f0f0
|
demo/locals.py
CHANGED
|
@@ -252,6 +252,22 @@ LOCALES = {
|
|
| 252 |
"label": "高清照",
|
| 253 |
},
|
| 254 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
"layout_photo": {
|
| 256 |
"en": {
|
| 257 |
"label": "Layout photo",
|
|
@@ -268,4 +284,12 @@ LOCALES = {
|
|
| 268 |
"label": "下载调整 KB 大小后的照片",
|
| 269 |
},
|
| 270 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
}
|
|
|
|
| 252 |
"label": "高清照",
|
| 253 |
},
|
| 254 |
},
|
| 255 |
+
"standard_photo_png": {
|
| 256 |
+
"en": {
|
| 257 |
+
"label": "Matting Standard photo",
|
| 258 |
+
},
|
| 259 |
+
"zh": {
|
| 260 |
+
"label": "透明标准照",
|
| 261 |
+
},
|
| 262 |
+
},
|
| 263 |
+
"hd_photo_png": {
|
| 264 |
+
"en": {
|
| 265 |
+
"label": "Matting HD photo",
|
| 266 |
+
},
|
| 267 |
+
"zh": {
|
| 268 |
+
"label": "透明高清照",
|
| 269 |
+
},
|
| 270 |
+
},
|
| 271 |
"layout_photo": {
|
| 272 |
"en": {
|
| 273 |
"label": "Layout photo",
|
|
|
|
| 284 |
"label": "下载调整 KB 大小后的照片",
|
| 285 |
},
|
| 286 |
},
|
| 287 |
+
"matting_image": {
|
| 288 |
+
"en": {
|
| 289 |
+
"label": "Matting image",
|
| 290 |
+
},
|
| 291 |
+
"zh": {
|
| 292 |
+
"label": "抠图图像",
|
| 293 |
+
},
|
| 294 |
+
},
|
| 295 |
}
|
demo/processor.py
CHANGED
|
@@ -137,6 +137,7 @@ class IDPhotoProcessor:
|
|
| 137 |
return [
|
| 138 |
gr.update(value=None), # img_output_standard
|
| 139 |
gr.update(value=None), # img_output_standard_hd
|
|
|
|
| 140 |
gr.update(visible=False), # img_output_layout
|
| 141 |
gr.update( # notification
|
| 142 |
value=LOCALES["notification"][language]["face_error"],
|
|
@@ -147,6 +148,10 @@ class IDPhotoProcessor:
|
|
| 147 |
|
| 148 |
else:
|
| 149 |
(result_image_standard, result_image_hd, _, _) = result
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
if (
|
| 151 |
idphoto_json["render_mode"]
|
| 152 |
== LOCALES["render_mode"][language]["choices"][0]
|
|
@@ -272,6 +277,8 @@ class IDPhotoProcessor:
|
|
| 272 |
return [
|
| 273 |
result_image_standard, # img_output_standard
|
| 274 |
result_image_hd, # img_output_standard_hd
|
|
|
|
|
|
|
| 275 |
result_layout_image, # img_output_layout
|
| 276 |
gr.update(visible=False), # notification
|
| 277 |
gr.update(visible=True, value=output_image_path), # file_download
|
|
@@ -280,6 +287,8 @@ class IDPhotoProcessor:
|
|
| 280 |
return [
|
| 281 |
result_image_standard, # img_output_standard
|
| 282 |
result_image_hd, # img_output_standard_hd
|
|
|
|
|
|
|
| 283 |
result_layout_image, # img_output_layout
|
| 284 |
gr.update(visible=False), # notification
|
| 285 |
gr.update(visible=False), # file_download
|
|
|
|
| 137 |
return [
|
| 138 |
gr.update(value=None), # img_output_standard
|
| 139 |
gr.update(value=None), # img_output_standard_hd
|
| 140 |
+
gr.update(value=None), # img_output_standard_hd_png
|
| 141 |
gr.update(visible=False), # img_output_layout
|
| 142 |
gr.update( # notification
|
| 143 |
value=LOCALES["notification"][language]["face_error"],
|
|
|
|
| 148 |
|
| 149 |
else:
|
| 150 |
(result_image_standard, result_image_hd, _, _) = result
|
| 151 |
+
|
| 152 |
+
result_image_standard_png = np.uint8(result_image_standard)
|
| 153 |
+
result_image_hd_png = np.uint8(result_image_hd)
|
| 154 |
+
|
| 155 |
if (
|
| 156 |
idphoto_json["render_mode"]
|
| 157 |
== LOCALES["render_mode"][language]["choices"][0]
|
|
|
|
| 277 |
return [
|
| 278 |
result_image_standard, # img_output_standard
|
| 279 |
result_image_hd, # img_output_standard_hd
|
| 280 |
+
result_image_standard_png, # img_output_standard_hd_png
|
| 281 |
+
result_image_hd_png, # img_output_standard_hd_png
|
| 282 |
result_layout_image, # img_output_layout
|
| 283 |
gr.update(visible=False), # notification
|
| 284 |
gr.update(visible=True, value=output_image_path), # file_download
|
|
|
|
| 287 |
return [
|
| 288 |
result_image_standard, # img_output_standard
|
| 289 |
result_image_hd, # img_output_standard_hd
|
| 290 |
+
result_image_standard_png, # img_output_standard_hd_png
|
| 291 |
+
result_image_hd_png, # img_output_standard_hd_png
|
| 292 |
result_layout_image, # img_output_layout
|
| 293 |
gr.update(visible=False), # notification
|
| 294 |
gr.update(visible=False), # file_download
|
demo/ui.py
CHANGED
|
@@ -16,35 +16,16 @@ def create_ui(
|
|
| 16 |
):
|
| 17 |
DEFAULT_LANG = "zh"
|
| 18 |
DEFAULT_HUMAN_MATTING_MODEL = "modnet_photographic_portrait_matting"
|
| 19 |
-
DEFAULT_FACE_DETECT_MODEL = "
|
| 20 |
|
| 21 |
if DEFAULT_HUMAN_MATTING_MODEL in human_matting_models:
|
| 22 |
human_matting_models.remove(DEFAULT_HUMAN_MATTING_MODEL)
|
| 23 |
human_matting_models.insert(0, DEFAULT_HUMAN_MATTING_MODEL)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
}
|
| 30 |
-
#col-mid {
|
| 31 |
-
margin: 0 auto;
|
| 32 |
-
max-width: 430px;
|
| 33 |
-
}
|
| 34 |
-
#col-right {
|
| 35 |
-
margin: 0 auto;
|
| 36 |
-
max-width: 430px;
|
| 37 |
-
}
|
| 38 |
-
#col-showcase {
|
| 39 |
-
margin: 0 auto;
|
| 40 |
-
max-width: 1100px;
|
| 41 |
-
}
|
| 42 |
-
#button {
|
| 43 |
-
color: blue;
|
| 44 |
-
}
|
| 45 |
-
"""
|
| 46 |
-
|
| 47 |
-
demo = gr.Blocks(title="HivisionIDPhotos", css=css)
|
| 48 |
|
| 49 |
with demo:
|
| 50 |
gr.HTML(load_description(os.path.join(root_dir, "assets/title.md")))
|
|
@@ -237,7 +218,9 @@ def create_ui(
|
|
| 237 |
],
|
| 238 |
)
|
| 239 |
|
| 240 |
-
img_but = gr.Button(
|
|
|
|
|
|
|
| 241 |
|
| 242 |
example_images = gr.Examples(
|
| 243 |
inputs=[img_input],
|
|
@@ -267,15 +250,34 @@ def create_ui(
|
|
| 267 |
height=350,
|
| 268 |
format="jpeg",
|
| 269 |
)
|
|
|
|
| 270 |
img_output_layout = gr.Image(
|
| 271 |
label=LOCALES["layout_photo"][DEFAULT_LANG]["label"],
|
| 272 |
height=350,
|
| 273 |
format="jpeg",
|
| 274 |
)
|
|
|
|
| 275 |
file_download = gr.File(
|
| 276 |
label=LOCALES["download"][DEFAULT_LANG]["label"], visible=False
|
| 277 |
)
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
# ---------------- 设置隐藏/显示组件 ----------------
|
| 280 |
def change_language(language):
|
| 281 |
return {
|
|
@@ -323,6 +325,12 @@ def create_ui(
|
|
| 323 |
img_output_standard_hd: gr.update(
|
| 324 |
label=LOCALES["hd_photo"][language]["label"]
|
| 325 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
img_output_layout: gr.update(
|
| 327 |
label=LOCALES["layout_photo"][language]["label"]
|
| 328 |
),
|
|
@@ -368,6 +376,9 @@ def create_ui(
|
|
| 368 |
choices=LOCALES["watermark_switch"][language]["choices"],
|
| 369 |
value=LOCALES["watermark_switch"][language]["choices"][0],
|
| 370 |
),
|
|
|
|
|
|
|
|
|
|
| 371 |
}
|
| 372 |
|
| 373 |
def change_color(colors):
|
|
@@ -422,6 +433,8 @@ def create_ui(
|
|
| 422 |
notification,
|
| 423 |
img_output_standard,
|
| 424 |
img_output_standard_hd,
|
|
|
|
|
|
|
| 425 |
img_output_layout,
|
| 426 |
file_download,
|
| 427 |
head_measure_ratio_option,
|
|
@@ -436,6 +449,7 @@ def create_ui(
|
|
| 436 |
watermark_text_angle,
|
| 437 |
watermark_text_space,
|
| 438 |
watermark_options,
|
|
|
|
| 439 |
],
|
| 440 |
)
|
| 441 |
|
|
@@ -484,6 +498,8 @@ def create_ui(
|
|
| 484 |
outputs=[
|
| 485 |
img_output_standard,
|
| 486 |
img_output_standard_hd,
|
|
|
|
|
|
|
| 487 |
img_output_layout,
|
| 488 |
notification,
|
| 489 |
file_download,
|
|
|
|
| 16 |
):
|
| 17 |
DEFAULT_LANG = "zh"
|
| 18 |
DEFAULT_HUMAN_MATTING_MODEL = "modnet_photographic_portrait_matting"
|
| 19 |
+
DEFAULT_FACE_DETECT_MODEL = "retinaface-resnet50"
|
| 20 |
|
| 21 |
if DEFAULT_HUMAN_MATTING_MODEL in human_matting_models:
|
| 22 |
human_matting_models.remove(DEFAULT_HUMAN_MATTING_MODEL)
|
| 23 |
human_matting_models.insert(0, DEFAULT_HUMAN_MATTING_MODEL)
|
| 24 |
|
| 25 |
+
if DEFAULT_FACE_DETECT_MODEL not in face_detect_models:
|
| 26 |
+
DEFAULT_FACE_DETECT_MODEL = "mtcnn"
|
| 27 |
+
|
| 28 |
+
demo = gr.Blocks(title="HivisionIDPhotos")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
with demo:
|
| 31 |
gr.HTML(load_description(os.path.join(root_dir, "assets/title.md")))
|
|
|
|
| 218 |
],
|
| 219 |
)
|
| 220 |
|
| 221 |
+
img_but = gr.Button(
|
| 222 |
+
LOCALES["button"][DEFAULT_LANG]["label"], elem_id="btn"
|
| 223 |
+
)
|
| 224 |
|
| 225 |
example_images = gr.Examples(
|
| 226 |
inputs=[img_input],
|
|
|
|
| 250 |
height=350,
|
| 251 |
format="jpeg",
|
| 252 |
)
|
| 253 |
+
|
| 254 |
img_output_layout = gr.Image(
|
| 255 |
label=LOCALES["layout_photo"][DEFAULT_LANG]["label"],
|
| 256 |
height=350,
|
| 257 |
format="jpeg",
|
| 258 |
)
|
| 259 |
+
|
| 260 |
file_download = gr.File(
|
| 261 |
label=LOCALES["download"][DEFAULT_LANG]["label"], visible=False
|
| 262 |
)
|
| 263 |
|
| 264 |
+
with gr.Accordion(
|
| 265 |
+
LOCALES["matting_image"][DEFAULT_LANG]["label"], open=False
|
| 266 |
+
) as matting_image_accordion:
|
| 267 |
+
with gr.Row():
|
| 268 |
+
img_output_standard_png = gr.Image(
|
| 269 |
+
label=LOCALES["standard_photo_png"][DEFAULT_LANG]["label"],
|
| 270 |
+
height=350,
|
| 271 |
+
format="png",
|
| 272 |
+
elem_id="standard_photo_png",
|
| 273 |
+
)
|
| 274 |
+
img_output_standard_hd_png = gr.Image(
|
| 275 |
+
label=LOCALES["hd_photo_png"][DEFAULT_LANG]["label"],
|
| 276 |
+
height=350,
|
| 277 |
+
format="png",
|
| 278 |
+
elem_id="hd_photo_png",
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
# ---------------- 设置隐藏/显示组件 ----------------
|
| 282 |
def change_language(language):
|
| 283 |
return {
|
|
|
|
| 325 |
img_output_standard_hd: gr.update(
|
| 326 |
label=LOCALES["hd_photo"][language]["label"]
|
| 327 |
),
|
| 328 |
+
img_output_standard_png: gr.update(
|
| 329 |
+
label=LOCALES["standard_photo_png"][language]["label"]
|
| 330 |
+
),
|
| 331 |
+
img_output_standard_hd_png: gr.update(
|
| 332 |
+
label=LOCALES["hd_photo_png"][language]["label"]
|
| 333 |
+
),
|
| 334 |
img_output_layout: gr.update(
|
| 335 |
label=LOCALES["layout_photo"][language]["label"]
|
| 336 |
),
|
|
|
|
| 376 |
choices=LOCALES["watermark_switch"][language]["choices"],
|
| 377 |
value=LOCALES["watermark_switch"][language]["choices"][0],
|
| 378 |
),
|
| 379 |
+
matting_image_accordion: gr.update(
|
| 380 |
+
label=LOCALES["matting_image"][language]["label"]
|
| 381 |
+
),
|
| 382 |
}
|
| 383 |
|
| 384 |
def change_color(colors):
|
|
|
|
| 433 |
notification,
|
| 434 |
img_output_standard,
|
| 435 |
img_output_standard_hd,
|
| 436 |
+
img_output_standard_png,
|
| 437 |
+
img_output_standard_hd_png,
|
| 438 |
img_output_layout,
|
| 439 |
file_download,
|
| 440 |
head_measure_ratio_option,
|
|
|
|
| 449 |
watermark_text_angle,
|
| 450 |
watermark_text_space,
|
| 451 |
watermark_options,
|
| 452 |
+
matting_image_accordion,
|
| 453 |
],
|
| 454 |
)
|
| 455 |
|
|
|
|
| 498 |
outputs=[
|
| 499 |
img_output_standard,
|
| 500 |
img_output_standard_hd,
|
| 501 |
+
img_output_standard_png,
|
| 502 |
+
img_output_standard_hd_png,
|
| 503 |
img_output_layout,
|
| 504 |
notification,
|
| 505 |
file_download,
|
hivision/creator/retinaface/inference.py
CHANGED
|
@@ -3,7 +3,6 @@ import cv2
|
|
| 3 |
import onnxruntime as ort
|
| 4 |
from hivision.creator.retinaface.box_utils import decode, decode_landm
|
| 5 |
from hivision.creator.retinaface.prior_box import PriorBox
|
| 6 |
-
import argparse
|
| 7 |
|
| 8 |
|
| 9 |
def py_cpu_nms(dets, thresh):
|
|
@@ -37,31 +36,15 @@ def py_cpu_nms(dets, thresh):
|
|
| 37 |
return keep
|
| 38 |
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"--confidence_threshold", default=0.8, type=float, help="confidence_threshold"
|
| 50 |
-
)
|
| 51 |
-
parser.add_argument("--top_k", default=5000, type=int, help="top_k")
|
| 52 |
-
parser.add_argument("--nms_threshold", default=0.2, type=float, help="nms_threshold")
|
| 53 |
-
parser.add_argument("--keep_top_k", default=750, type=int, help="keep_top_k")
|
| 54 |
-
parser.add_argument(
|
| 55 |
-
"-s",
|
| 56 |
-
"--save_image",
|
| 57 |
-
action="store_true",
|
| 58 |
-
default=True,
|
| 59 |
-
help="show detection results",
|
| 60 |
-
)
|
| 61 |
-
parser.add_argument(
|
| 62 |
-
"--vis_thres", default=0.6, type=float, help="visualization_threshold"
|
| 63 |
-
)
|
| 64 |
-
args = parser.parse_args()
|
| 65 |
|
| 66 |
|
| 67 |
def load_model_ort(model_path):
|
|
@@ -112,7 +95,6 @@ def retinaface_detect_faces(image, model_path: str, sess=None):
|
|
| 112 |
inputs = {"input": img}
|
| 113 |
loc, conf, landms = retinaface.run(None, inputs)
|
| 114 |
|
| 115 |
-
# tic = time.time()
|
| 116 |
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
|
| 117 |
priors = priorbox.forward()
|
| 118 |
|
|
@@ -141,30 +123,28 @@ def retinaface_detect_faces(image, model_path: str, sess=None):
|
|
| 141 |
landms = landms * scale1 / resize
|
| 142 |
|
| 143 |
# ignore low scores
|
| 144 |
-
inds = np.where(scores >
|
| 145 |
boxes = boxes[inds]
|
| 146 |
landms = landms[inds]
|
| 147 |
scores = scores[inds]
|
| 148 |
|
| 149 |
# keep top-K before NMS
|
| 150 |
-
order = scores.argsort()[::-1][:
|
| 151 |
boxes = boxes[order]
|
| 152 |
landms = landms[order]
|
| 153 |
scores = scores[order]
|
| 154 |
|
| 155 |
# do NMS
|
| 156 |
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
|
| 157 |
-
keep = py_cpu_nms(dets,
|
| 158 |
-
# keep = nms(dets, args.nms_threshold,force_cpu=args.cpu)
|
| 159 |
dets = dets[keep, :]
|
| 160 |
landms = landms[keep]
|
| 161 |
|
| 162 |
# keep top-K faster NMS
|
| 163 |
-
dets = dets[:
|
| 164 |
-
landms = landms[:
|
| 165 |
|
| 166 |
dets = np.concatenate((dets, landms), axis=1)
|
| 167 |
-
# print("post processing time: {:.4f}s".format(time.time() - tic))
|
| 168 |
|
| 169 |
return dets, retinaface
|
| 170 |
|
|
|
|
| 3 |
import onnxruntime as ort
|
| 4 |
from hivision.creator.retinaface.box_utils import decode, decode_landm
|
| 5 |
from hivision.creator.retinaface.prior_box import PriorBox
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
def py_cpu_nms(dets, thresh):
|
|
|
|
| 36 |
return keep
|
| 37 |
|
| 38 |
|
| 39 |
+
# 替换掉 argparse 的部分,直接使用普通变量
|
| 40 |
+
network = "resnet50"
|
| 41 |
+
use_cpu = False
|
| 42 |
+
confidence_threshold = 0.8
|
| 43 |
+
top_k = 5000
|
| 44 |
+
nms_threshold = 0.2
|
| 45 |
+
keep_top_k = 750
|
| 46 |
+
save_image = True
|
| 47 |
+
vis_thres = 0.6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def load_model_ort(model_path):
|
|
|
|
| 95 |
inputs = {"input": img}
|
| 96 |
loc, conf, landms = retinaface.run(None, inputs)
|
| 97 |
|
|
|
|
| 98 |
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
|
| 99 |
priors = priorbox.forward()
|
| 100 |
|
|
|
|
| 123 |
landms = landms * scale1 / resize
|
| 124 |
|
| 125 |
# ignore low scores
|
| 126 |
+
inds = np.where(scores > confidence_threshold)[0]
|
| 127 |
boxes = boxes[inds]
|
| 128 |
landms = landms[inds]
|
| 129 |
scores = scores[inds]
|
| 130 |
|
| 131 |
# keep top-K before NMS
|
| 132 |
+
order = scores.argsort()[::-1][:top_k]
|
| 133 |
boxes = boxes[order]
|
| 134 |
landms = landms[order]
|
| 135 |
scores = scores[order]
|
| 136 |
|
| 137 |
# do NMS
|
| 138 |
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
|
| 139 |
+
keep = py_cpu_nms(dets, nms_threshold)
|
|
|
|
| 140 |
dets = dets[keep, :]
|
| 141 |
landms = landms[keep]
|
| 142 |
|
| 143 |
# keep top-K faster NMS
|
| 144 |
+
dets = dets[:keep_top_k, :]
|
| 145 |
+
landms = landms[:keep_top_k, :]
|
| 146 |
|
| 147 |
dets = np.concatenate((dets, landms), axis=1)
|
|
|
|
| 148 |
|
| 149 |
return dets, retinaface
|
| 150 |
|