feat: api add beauty (#209)

* docs: title 1.3.0

* fix: demo face error

* feat: api add beauty

* docs: api

* Update README.md
This commit is contained in:
Ze-Yi LIN 2024-11-16 20:24:17 +08:00 committed by GitHub
parent 0942851ec6
commit 71e05ba193
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 70 additions and 49 deletions

View File

@ -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值大小**、**证件照裁切**
<br>

View File

@ -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;

View File

@ -320,10 +320,11 @@ class IDPhotoProcessor:
"""处理照片生成错误"""
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,
]
# 处理生成的照片

View File

@ -15,7 +15,6 @@ from hivision.utils import (
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)

View File

@ -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'
```
<br>
@ -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:

View File

@ -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: