Skip to content

Commit

Permalink
feat: api add beauty (#209)
Browse files Browse the repository at this point in the history
* docs: title 1.3.0

* fix: demo face error

* feat: api add beauty

* docs: api

* Update README.md
  • Loading branch information
Zeyi-Lin authored Nov 16, 2024
1 parent 0942851 commit 71e05ba
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@

- 在线体验: [![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**参数
- 2024.09.18: Gradio Demo增加**分享模版照**功能、增加**美式证件照**背景选项
- 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值大小****证件照裁切**

<br>

Expand Down
2 changes: 1 addition & 1 deletion demo/assets/title.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; font-size: 40px;">
<div style="display: flex; align-items: center;">
<img src="https://swanhub.co/git/repo/ZeYiLin%2FHivisionIDPhotos/file/preview?ref=master&path=assets/hivision_logo.png" alt="HivisionIDPhotos" style="width: 65px; height: 65px; margin-right: 10px;" onerror="this.style.display='none';" loading="lazy">
<b style="color: #6e9abb;">HivisionIDPhotos</b><span style="font-size: 18px; color: #638fb3; margin-left: 10px;"> v1.2.8</span>
<b style="color: #6e9abb;">HivisionIDPhotos</b><span style="font-size: 18px; color: #638fb3; margin-left: 10px;"> v1.3.0</span>
</div>
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
<a href="https://github.com/xiaolin199912/HivisionIDPhotos"><img alt="Github" src="https://img.shields.io/static/v1?label=GitHub&message=GitHub&color=black" onerror="this.style.display='none';"></a> &ensp;
Expand Down
3 changes: 2 additions & 1 deletion demo/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]

# 处理生成的照片
Expand Down
25 changes: 16 additions & 9 deletions deploy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 32 additions & 21 deletions docs/api_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |

**返回参数:**

Expand Down Expand Up @@ -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. 添加背景色
Expand Down Expand Up @@ -305,16 +316,19 @@ 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' \
-F 'height=413' \
-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'
```

<br>
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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")}
Expand All @@ -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:
Expand Down
34 changes: 18 additions & 16 deletions docs/api_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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")}
Expand All @@ -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:
Expand Down

0 comments on commit 71e05ba

Please sign in to comment.