From 71e05ba193cfb92bb6934d48800c3afb61de6673 Mon Sep 17 00:00:00 2001 From: Ze-Yi LIN <58305964+Zeyi-Lin@users.noreply.github.com> Date: Sat, 16 Nov 2024 20:24:17 +0800 Subject: [PATCH] feat: api add beauty (#209) * docs: title 1.3.0 * fix: demo face error * feat: api add beauty * docs: api * Update README.md --- README.md | 2 +- demo/assets/title.md | 2 +- demo/processor.py | 3 ++- deploy_api.py | 25 +++++++++++++-------- docs/api_CN.md | 53 ++++++++++++++++++++++++++------------------ docs/api_EN.md | 34 +++++++++++++++------------- 6 files changed, 70 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 0a2e0b57..20216af0 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ - 在线体验: [![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] +- 2024.11.16: API接口增加美颜参数 - 2024.09.25: 增加**五寸相纸**和**JPEG下载**选项|默认照片下载支持300DPI - 2024.09.24: API接口增加base64图像传入选项 | Gradio Demo增加**排版照裁剪线**功能 - 2024.09.22: Gradio Demo增加**野兽模式**,可设置内存加载策略 | API接口增加**dpi、face_alignment**参数 @@ -61,7 +62,6 @@ - 2024.09.17: Gradio Demo增加**自定义底色-HEX输入**功能 | **(社区贡献)C++版本** - [HivisionIDPhotos-cpp](https://github.com/zjkhahah/HivisionIDPhotos-cpp) 贡献 by [zjkhahah](https://github.com/zjkhahah) - 2024.09.16: Gradio Demo增加**人脸旋转对齐**功能,自定义尺寸输入支持**毫米**单位 - 2024.09.14: Gradio Demo增加**自定义DPI**功能,增加日语和韩语支持,增加**调整亮度、对比度、锐度**功能 -- 2024.09.12: Gradio Demo增加**美白**功能 | API接口增加**加水印**、**设置照片KB值大小**、**证件照裁切**
diff --git a/demo/assets/title.md b/demo/assets/title.md index 5c836537..d19d8671 100644 --- a/demo/assets/title.md +++ b/demo/assets/title.md @@ -1,7 +1,7 @@
HivisionIDPhotos - HivisionIDPhotos v1.2.8 + HivisionIDPhotos v1.3.0
Github   diff --git a/demo/processor.py b/demo/processor.py index bed27f62..fdebdf67 100644 --- a/demo/processor.py +++ b/demo/processor.py @@ -320,10 +320,11 @@ def _handle_photo_generation_error(self, language): """处理照片生成错误""" return [gr.update(value=None) for _ in range(4)] + [ gr.update(visible=False), + gr.update(value=None), + gr.update(value=None), gr.update( value=LOCALES["notification"][language]["face_error"], visible=True ), - None, ] # 处理生成的照片 diff --git a/deploy_api.py b/deploy_api.py index b01e2d73..4330dcf2 100644 --- a/deploy_api.py +++ b/deploy_api.py @@ -15,7 +15,6 @@ add_watermark, save_image_dpi_to_bytes, ) -import base64 import numpy as np import cv2 from starlette.middleware.cors import CORSMiddleware @@ -47,10 +46,14 @@ async def idphoto_inference( hd: bool = Form(True), dpi: int = Form(300), face_align: bool = Form(False), - head_measure_ratio: float = 0.2, - head_height_ratio: float = 0.45, - top_distance_max: float = 0.12, - top_distance_min: float = 0.10, + head_measure_ratio: float = Form(0.2), + head_height_ratio: float = Form(0.45), + top_distance_max: float = Form(0.12), + top_distance_min: float = Form(0.10), + brightness_strength: float = Form(0), + contrast_strength: float = Form(0), + sharpen_strength: float = Form(0), + saturation_strength: float = Form(0), ): # 如果传入了base64,则直接使用base64解码 if input_image_base64: @@ -74,6 +77,10 @@ async def idphoto_inference( head_height_ratio=head_height_ratio, head_top_range=(top_distance_max, top_distance_min), face_alignment=face_align, + brightness_strength=brightness_strength, + contrast_strength=contrast_strength, + sharpen_strength=sharpen_strength, + saturation_strength=saturation_strength, ) except FaceError: result_message = {"status": False} @@ -304,10 +311,10 @@ async def idphoto_crop_inference( face_detect_model: str = Form("mtcnn"), hd: bool = Form(True), dpi: int = Form(300), - head_measure_ratio: float = 0.2, - head_height_ratio: float = 0.45, - top_distance_max: float = 0.12, - top_distance_min: float = 0.10, + head_measure_ratio: float = Form(0.2), + head_height_ratio: float = Form(0.45), + top_distance_max: float = Form(0.12), + top_distance_min: float = Form(0.10), ): if input_image_base64: img = base64_2_numpy(input_image_base64) diff --git a/docs/api_CN.md b/docs/api_CN.md index 8af37cdc..d687edd5 100644 --- a/docs/api_CN.md +++ b/docs/api_CN.md @@ -60,7 +60,10 @@ python deploy_api.py | head_height_ratio | float | 否 | 面部中心与照片顶部的高度比例,默认为`0.45` | | top_distance_max | float | 否 | 头部与照片顶部距离的比例最大值,默认为`0.12` | | top_distance_min | float | 否 | 头部与照片顶部距离的比例最小值,默认为`0.1` | - +| brightness_strength | float | 否 | 亮度调整强度,默认为`0` | +| contrast_strength | float | 否 | 对比度调整强度,默认为`0` | +| sharpen_strength | float | 否 | 锐化调整强度,默认为`0` | +| saturation_strength | float | 否 | 饱和度调整强度,默认为`0` | **返回参数:** @@ -247,7 +250,15 @@ curl -X POST "http://127.0.0.1:8080/idphoto" \ -F "face_detect_model=mtcnn" \ -F "hd=true" \ -F "dpi=300" \ --F "face_alignment=true" +-F "face_alignment=true" \ +-F 'head_height_ratio=0.45' \ +-F 'head_measure_ratio=0.2' \ +-F 'top_distance_min=0.1' \ +-F 'top_distance_max=0.12' \ +-F 'sharpen_strength=0' \ +-F 'saturation_strength=0' \ +-F 'brightness_strength=10' \ +-F 'contrast_strength=0' ``` ### 2. 添加背景色 @@ -305,8 +316,7 @@ curl -X 'POST' \ ### 7. 证件照裁切 ```bash -curl -X 'POST' \ - 'http://127.0.0.1:8080/idphoto_crop?head_measure_ratio=0.2&head_height_ratio=0.45&top_distance_max=0.12&top_distance_min=0.1' \ +curl -X 'POST' 'http://127.0.0.1:8080/idphoto_crop' \ -H 'accept: application/json' \ -H 'Content-Type: multipart/form-data' \ -F 'input_image=@idphoto_matting.png;type=image/png' \ @@ -314,7 +324,11 @@ curl -X 'POST' \ -F 'width=295' \ -F 'face_detect_model=mtcnn' \ -F 'hd=true' \ - -F 'dpi=300' + -F 'dpi=300' \ + -F 'head_height_ratio=0.45' \ + -F 'head_measure_ratio=0.2' \ + -F 'top_distance_min=0.1' \ + -F 'top_distance_max=0.12' ```
@@ -328,13 +342,6 @@ import requests url = "http://127.0.0.1:8080/idphoto" input_image_path = "demo/images/test0.jpg" -# 设置请求参数 -params = { - "head_measure_ratio": 0.2, - "head_height_ratio": 0.45, - "top_distance_max": 0.12, - "top_distance_min": 0.1, -} files = {"input_image": open(input_image_path, "rb")} data = { "height": 413, @@ -344,6 +351,14 @@ data = { "hd": True, "dpi": 300, "face_alignment": True, + "head_measure_ratio": 0.2, + "head_height_ratio": 0.45, + "top_distance_max": 0.12, + "top_distance_min": 0.1, + "brightness_strength": 0, + "contrast_strength": 0, + "sharpen_strength": 0, + "saturation_strength": 0, } response = requests.post(url, params=params, files=files, data=data).json() @@ -481,14 +496,6 @@ import requests # 设置请求的 URL url = "http://127.0.0.1:8080/idphoto_crop" -# 设置请求参数 -params = { - "head_measure_ratio": 0.2, - "head_height_ratio": 0.45, - "top_distance_max": 0.12, - "top_distance_min": 0.1, -} - # 设置文件和其他表单数据 input_image_path = "idphoto_matting.png" files = {"input_image": ("idphoto_matting.png", open(input_image_path, "rb"), "image/png")} @@ -498,10 +505,14 @@ data = { "face_detect_model": "mtcnn", "hd": "true", "dpi": 300, + "head_measure_ratio": 0.2, + "head_height_ratio": 0.45, + "top_distance_max": 0.12, + "top_distance_min": 0.1, } # 发送 POST 请求 -response = requests.post(url, params=params, files=files, data=data) +response = requests.post(url, files=files, data=data) # 检查响应 if response.ok: diff --git a/docs/api_EN.md b/docs/api_EN.md index 56a4aab3..39d9e5d0 100644 --- a/docs/api_EN.md +++ b/docs/api_EN.md @@ -59,6 +59,10 @@ It is important to note that both generated photos are transparent (RGBA four-ch | head_height_ratio | float | No | The ratio of the face center to the top of the photo, with a default value of `0.45`. | | top_distance_max | float | No | The maximum ratio of the head to the top of the photo, with a default value of `0.12`. | | top_distance_min | float | No | The minimum ratio of the head to the top of the photo, with a default value of `0.1`. | +| brightness_strength | float | No | Brightness adjustment strength, default is `0` | +| contrast_strength | float | No | Contrast adjustment strength, default is `0` | +| sharpen_strength | float | No | Sharpening adjustment strength, default is `0` | +| saturation_strength | float | No | Saturation adjustment strength, default is `0` | **Return Parameters:** @@ -329,12 +333,6 @@ url = "http://127.0.0.1:8080/idphoto" input_image_path = "demo/images/test0.jpg" # Set request parameters -params = { - "head_measure_ratio": 0.2, - "head_height_ratio": 0.45, - "top_distance_max": 0.12, - "top_distance_min": 0.1, -} files = {"input_image": open(input_image_path, "rb")} data = { "height": 413, @@ -344,9 +342,17 @@ data = { "hd": True, "dpi": 300, "face_alignment": True, + "head_measure_ratio": 0.2, + "head_height_ratio": 0.45, + "top_distance_max": 0.12, + "top_distance_min": 0.1, + "brightness_strength": 0, + "contrast_strength": 0, + "sharpen_strength": 0, + "saturation_strength": 0, } -response = requests.post(url, params=params, files=files, data=data).json() +response = requests.post(url, files=files, data=data).json() # response is a JSON formatted dictionary containing status, image_base64_standard, and image_base64_hd print(response) @@ -481,14 +487,6 @@ import requests # Set the request URL url = "http://127.0.0.1:8080/idphoto_crop" -# Set request parameters -params = { - "head_measure_ratio": 0.2, - "head_height_ratio": 0.45, - "top_distance_max": 0.12, - "top_distance_min": 0.1, -} - # Set file and other form data input_image_path = "idphoto_matting.png" files = {"input_image": ("idphoto_matting.png", open(input_image_path, "rb"), "image/png")} @@ -498,10 +496,14 @@ data = { "face_detect_model": "mtcnn", "hd": "true", "dpi": 300, + "head_measure_ratio": 0.2, + "head_height_ratio": 0.45, + "top_distance_max": 0.12, + "top_distance_min": 0.1, } # Send POST request -response = requests.post(url, params=params, files=files, data=data) +response = requests.post(url, files=files, data=data) # Check response if response.ok: