Skip to content

Commit 4c2ac52

Browse files
authored
feat: beast mode (#160)
1 parent ecf028c commit 4c2ac52

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555

5656
- 在线体验: [![SwanHub Demo](https://img.shields.io/static/v1?label=Demo&message=SwanHub%20Demo&color=blue)](https://swanhub.co/ZeYiLin/HivisionIDPhotos/demo)[![Spaces](https://img.shields.io/badge/🤗-Open%20in%20Spaces-blue)](https://huggingface.co/spaces/TheEeeeLin/HivisionIDPhotos)[![][modelscope-shield]][modelscope-link]
5757

58+
- 2024.09.22: Gradio Demo增加**野兽模式**,可设置内存加载策略
5859
- 2024.09.18: Gradio Demo增加**分享模版照**功能、增加**美式证件照**背景选项
5960
- 2024.09.17: Gradio Demo增加**自定义底色-HEX输入**功能 | **(社区贡献)C++版本** - [HivisionIDPhotos-cpp](https://github.com/zjkhahah/HivisionIDPhotos-cpp) 贡献 by [zjkhahah](https://github.com/zjkhahah)
6061
- 2024.09.16: Gradio Demo增加**人脸旋转对齐**功能,自定义尺寸输入支持**毫米**单位
6162
- 2024.09.14: Gradio Demo增加**自定义DPI**功能,增加日语和韩语支持,增加**调整亮度、对比度、锐度**功能
6263
- 2024.09.12: Gradio Demo增加**美白**功能 | API接口增加**加水印****设置照片KB值大小****证件照裁切**
6364
- 2024.09.11: Gradio Demo增加**透明图显示与下载**功能
6465
- 2024.09.10: 增加新的**人脸检测模型** Retinaface-resnet50,以稍弱于mtcnn的速度换取更高的检测精度,推荐使用
65-
- 2024.09.09: 增加新的**抠图模型** [BiRefNet-v1-lite](https://github.com/ZhengPeng7/BiRefNet) | Gradio增加**高级参数设置****水印**选项卡
6666

6767
<br>
6868

@@ -319,13 +319,15 @@ docker compose up -d
319319
|--|--|--|--|
320320
| FACE_PLUS_API_KEY | 可选 | 这是你在 Face++ 控制台申请的 API 密钥 | `7-fZStDJ····` |
321321
| FACE_PLUS_API_SECRET | 可选 | Face++ API密钥对应的Secret | `VTee824E····` |
322+
| RUN_MODE | 可选 | 运行模式,可选值为`beast`(野兽模式)。野兽模式下人脸检测和抠图模型将不释放内存,从而获得更快的二次推理速度。建议内存16GB以上尝试。 | `beast` |
322323

323324
docker使用环境变量示例:
324325
```bash
325326
docker run -d -p 7860:7860 \
326327
-e FACE_PLUS_API_KEY=7-fZStDJ···· \
327328
-e FACE_PLUS_API_SECRET=VTee824E···· \
328-
linzeyi/hivision_idphotos
329+
-e RUN_MODE=beast \
330+
linzeyi/hivision_idphotos
329331
```
330332

331333
<br>

app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
FACE_DETECT_MODELS_CHOICE,
6666
LANGUAGE,
6767
)
68+
69+
# 如果RUN_MODE是Beast,打印已开启野兽模式
70+
if os.getenv("RUN_MODE") == "beast":
71+
print("[Beast mode activated.] 已开启野兽模式。")
72+
6873
demo.launch(
6974
server_name=args.host,
7075
server_port=args.port,

hivision/creator/face_detector.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,7 @@ def detect_face_retinaface(ctx: Context):
213213
dx = right_eye[0] - left_eye[0]
214214
roll_angle = np.degrees(np.arctan2(dy, dx))
215215
ctx.face["roll_angle"] = roll_angle
216+
217+
# 如果RUN_MODE不是野兽模式,则释放模型
218+
if os.getenv("RUN_MODE") == "beast":
219+
RETINAFCE_SESS = None

hivision/creator/human_matting.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def get_modnet_matting(input_image, checkpoint_path, ref_size=512):
201201
print(f"Checkpoint file not found: {checkpoint_path}")
202202
return None
203203

204+
# 如果RUN_MODE不是野兽模式,则不加载模型
204205
if HIVISION_MODNET_SESS is None:
205206
HIVISION_MODNET_SESS = load_onnx_model(checkpoint_path, set_cpu=True)
206207

@@ -216,6 +217,10 @@ def get_modnet_matting(input_image, checkpoint_path, ref_size=512):
216217
b, g, r = cv2.split(np.uint8(input_image))
217218

218219
output_image = cv2.merge((b, g, r, mask))
220+
221+
# 如果RUN_MODE不是野兽模式,则释放模型
222+
if os.getenv("RUN_MODE") != "beast":
223+
HIVISION_MODNET_SESS = None
219224

220225
return output_image
221226

@@ -229,6 +234,7 @@ def get_modnet_matting_photographic_portrait_matting(
229234
print(f"Checkpoint file not found: {checkpoint_path}")
230235
return None
231236

237+
# 如果RUN_MODE不是野兽模式,则不加载模型
232238
if MODNET_PHOTOGRAPHIC_PORTRAIT_MATTING_SESS is None:
233239
MODNET_PHOTOGRAPHIC_PORTRAIT_MATTING_SESS = load_onnx_model(
234240
checkpoint_path, set_cpu=True
@@ -248,6 +254,10 @@ def get_modnet_matting_photographic_portrait_matting(
248254
b, g, r = cv2.split(np.uint8(input_image))
249255

250256
output_image = cv2.merge((b, g, r, mask))
257+
258+
# 如果RUN_MODE不是野兽模式,则释放模型
259+
if os.getenv("RUN_MODE") != "beast":
260+
MODNET_PHOTOGRAPHIC_PORTRAIT_MATTING_SESS = None
251261

252262
return output_image
253263

@@ -297,6 +307,10 @@ def resize_rmbg_image(image):
297307
# Paste the mask on the original image
298308
new_im = Image.new("RGBA", orig_image.size, (0, 0, 0, 0))
299309
new_im.paste(orig_image, mask=pil_im)
310+
311+
# 如果RUN_MODE不是野兽模式,则释放模型
312+
if os.getenv("RUN_MODE") != "beast":
313+
RMBG_SESS = None
300314

301315
return np.array(new_im)
302316

@@ -362,8 +376,9 @@ def transform_image(image):
362376
# 记录加载onnx模型的开始时间
363377
load_start_time = time()
364378

379+
# 如果RUN_MODE不是野兽模式,则不加载模型
365380
if BIREFNET_V1_LITE_SESS is None:
366-
print("首次加载birefnet-v1-lite模型...")
381+
# print("首次加载birefnet-v1-lite模型...")
367382
if ONNX_DEVICE == "GPU":
368383
print("onnxruntime-gpu已安装,尝试使用CUDA加载模型")
369384
try:
@@ -405,5 +420,9 @@ def transform_image(image):
405420
# Paste the mask on the original image
406421
new_im = Image.new("RGBA", orig_image.size, (0, 0, 0, 0))
407422
new_im.paste(orig_image, mask=pil_im)
423+
424+
# 如果RUN_MODE不是野兽模式,则释放模型
425+
if os.getenv("RUN_MODE") != "beast":
426+
BIREFNET_V1_LITE_SESS = None
408427

409428
return np.array(new_im)

0 commit comments

Comments
 (0)