Skip to content

Commit

Permalink
feat: sync api (#161)
Browse files Browse the repository at this point in the history
* align + hd + dpi

* sync inference

* sync api

* docs

* Update README.md

* refactor api_CN

* docs update
  • Loading branch information
Zeyi-Lin authored Sep 23, 2024
1 parent 4c2ac52 commit fe8886f
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 1,720 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ test/temp/*

.python-version

# Ignore .png and .jpg files in the root directory
/*.png
/*.jpg
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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.09.22: 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增加**人脸旋转对齐**功能,自定义尺寸输入支持**毫米**单位
Expand Down Expand Up @@ -256,8 +256,6 @@ python deploy_api.py
详细请求方式请参考 [API 文档](docs/api_CN.md),包含以下请求示例:
- [cURL](docs/api_CN.md#curl-请求示例)
- [Python](docs/api_CN.md#python-请求示例)
- [Java](docs/api_CN.md#java-请求示例)
- [Javascript](docs/api_CN.md#javascript-请求示例)

<br>

Expand Down
4 changes: 1 addition & 3 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ English / [中文](README.md) / [日本語](README_JP.md) / [한국어](README_K

- Online Experience: [![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.09.22: Gradio Demo adds **Beast Mode** and **DPI** parameter
- 2024.09.18: Gradio Demo adds **Share Template Photos** feature and **American Style** background option
- 2024.09.17: Gradio Demo adds **Custom Background Color-HEX Input** feature | **(Community Contribution) C++ Version** - [HivisionIDPhotos-cpp](https://github.com/zjkhahah/HivisionIDPhotos-cpp) contributed by [zjkhahah](https://github.com/zjkhahah)
- 2024.09.16: Gradio Demo adds **Face Rotation Alignment** feature, custom size input supports **millimeters**
- 2024.09.14: Gradio Demo adds **Custom DPI** feature, adds Japanese and Korean support, adds **Adjust Brightness, Contrast, Sharpness** feature
- 2024.09.12: Gradio Demo adds **Whitening** feature | API interface adds **Watermark**, **Set Photo KB Size**, **ID Photo Cropping**
- 2024.09.11: Added **transparent image display and download** feature to Gradio Demo.
- 2024.09.10: Added a new **face detection model** Retinaface-resnet50, which offers higher detection accuracy at a slightly slower speed compared to mtcnn. Recommended for use.
- 2024.09.09: Added a new **Background Removal Model** [BiRefNet-v1-lite](https://github.com/ZhengPeng7/BiRefNet) | Gradio added **Advanced Parameter Settings** and **Watermark** tabs

<br>

Expand Down Expand Up @@ -252,8 +252,6 @@ python deploy_api.py
For detailed request methods, please refer to the [API Documentation](docs/api_EN.md), which includes the following request examples:
- [cURL](docs/api_EN.md#curl-request-examples)
- [Python](docs/api_EN.md#python-request-example)
- [Java](docs/api_EN.md#java-request-example)
- [Javascript](docs/api_EN.md#javascript-request-examples)

<br>

Expand Down
4 changes: 1 addition & 3 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@

- オンライン体験: [![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.09.22: Gradioデモに**ビーストモード****DPI**パラメータを追加
- 2024.09.18: Gradioデモに**テンプレート写真の共有**機能を追加、**米国式**背景オプションを追加
- 2024.09.17: Gradioデモに**カスタム底色-HEX入力**機能を追加 | **(コミュニティ貢献)C++バージョン** - [HivisionIDPhotos-cpp](https://github.com/zjkhahah/HivisionIDPhotos-cpp) 貢献 by [zjkhahah](https://github.com/zjkhahah)
- 2024.09.16: Gradioデモに**顔回転対応**機能を追加、カスタムサイズ入力に**ミリメートル**をサポート
- 2024.09.14: Gradioデモに**カスタムDPI**機能を追加、日本語と韓国語を追加,**明るさ、コントラスト、鮮明度の調整**機能を追加
- 2024.09.12: Gradioデモに**ホワイトニング**機能を追加 | APIインターフェースに**ウォーターマーク追加****写真のKBサイズ設定****証明写真のトリミング**を追加
- 2024.09.11: Gradioデモに**透過画像表示とダウンロード**機能を追加しました。
- 2024.09.09: 新しい**背景除去モデル** [BiRefNet-v1-lite](https://github.com/ZhengPeng7/BiRefNet) を追加 | Gradioに**高度なパラメータ設定**および**ウォーターマーク**タブを追加

<br>

Expand Down Expand Up @@ -248,8 +248,6 @@ python deploy_api.py
詳細なリクエスト方法は[APIドキュメント](docs/api_EN.md)を参照してください。以下のリクエスト例が含まれます:
- [cURL](docs/api_EN.md#curl-request-examples)
- [Python](docs/api_EN.md#python-request-example)
- [Java](docs/api_EN.md#java-request-example)
- [Javascript](docs/api_EN.md#javascript-request-examples)

<br>

Expand Down
2 changes: 1 addition & 1 deletion README_KO.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@

- 온라인 체험: [![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.09.22: Gradio Demo에 **버스트 모드****DPI** 매개변수 추가
- 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 데모에 **미백** 기능 추가 | API 인터페이스에 **워터마크 추가**, **사진 KB 크기 설정**, **증명사진 자르기** 추가
- 2024.09.11: Gradio Demo에 **투명 이미지 표시 및 다운로드** 기능 추가
- 2024.09.09: 새로운 **배경 제거 모델** [BiRefNet-v1-lite](https://github.com/ZhengPeng7/BiRefNet) 추가 | Gradio에 **고급 매개변수 설정****워터마크** 탭 추가

<br>

Expand Down
84 changes: 43 additions & 41 deletions deploy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
from hivision.creator.choose_handler import choose_handler
from hivision.utils import (
add_background,
resize_image_to_kb_base64,
resize_image_to_kb,
bytes_2_base64,
numpy_2_base64,
hex_to_rgb,
add_watermark,
save_image_dpi_to_bytes,
)
import base64
import numpy as np
Expand All @@ -32,23 +35,17 @@
)


# 将图像转换为Base64编码
def numpy_2_base64(img: np.ndarray):
retval, buffer = cv2.imencode(".png", img)
base64_image = base64.b64encode(buffer).decode("utf-8")

return "data:image/png;base64," + base64_image


# 证件照智能制作接口
@app.post("/idphoto")
async def idphoto_inference(
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
human_matting_model: str = Form("hivision_modnet"),
human_matting_model: str = Form("modnet_photographic_portrait_matting"),
face_detect_model: str = Form("mtcnn"),
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,
Expand All @@ -70,19 +67,23 @@ async def idphoto_inference(
head_measure_ratio=head_measure_ratio,
head_height_ratio=head_height_ratio,
head_top_range=(top_distance_max, top_distance_min),
face_alignment=face_align,
)
except FaceError:
result_message = {"status": False}
# 如果检测到人脸数量等于1, 则返回标准证和高清照结果(png 4通道图像)
else:
result_image_standard_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA), None, dpi)

result_message = {
"status": True,
"image_base64_standard": numpy_2_base64(result.standard),
"image_base64_standard": bytes_2_base64(result_image_standard_bytes),
}

# 如果hd为True, 则增加高清照结果(png 4通道图像)
if hd:
result_message["image_base64_hd"] = numpy_2_base64(result.hd)
result_image_hd_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.hd, cv2.COLOR_RGBA2BGRA), None, dpi)
result_message["image_base64_hd"] = bytes_2_base64(result_image_hd_bytes)

return result_message

Expand All @@ -92,6 +93,7 @@ async def idphoto_inference(
async def human_matting_inference(
input_image: UploadFile,
human_matting_model: str = Form("hivision_modnet"),
dpi: int = Form(300),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
Expand All @@ -109,9 +111,10 @@ async def human_matting_inference(
result_message = {"status": False}

else:
result_image_standard_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA), None, dpi)
result_message = {
"status": True,
"image_base64": numpy_2_base64(result.standard),
"image_base64": bytes_2_base64(result_image_standard_bytes),
}
return result_message

Expand All @@ -122,6 +125,7 @@ async def photo_add_background(
input_image: UploadFile,
color: str = Form("000000"),
kb: int = Form(None),
dpi: int = Form(300),
render: int = Form(0),
):
render_choice = ["pure_color", "updown_gradient", "center_gradient"]
Expand All @@ -139,25 +143,17 @@ async def photo_add_background(
mode=render_choice[render],
).astype(np.uint8)

result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
if kb:
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
result_image_bytes = resize_image_to_kb(result_image, None, int(kb), dpi=dpi)
else:
result_image_base64 = numpy_2_base64(result_image)
result_image_bytes = save_image_dpi_to_bytes(result_image, None, dpi=dpi)

# try:
result_messgae = {
"status": True,
"image_base64": result_image_base64,
"image_base64": bytes_2_base64(result_image_bytes),
}

# except Exception as e:
# print(e)
# result_messgae = {
# "status": False,
# "error": e
# }

return result_messgae


Expand All @@ -168,6 +164,7 @@ async def generate_layout_photos(
height: int = Form(413),
width: int = Form(295),
kb: int = Form(None),
dpi: int = Form(300),
):
# try:
image_bytes = await input_image.read()
Expand All @@ -184,24 +181,21 @@ async def generate_layout_photos(
img, typography_arr, typography_rotate, height=size[0], width=size[1]
).astype(np.uint8)

result_layout_image = cv2.cvtColor(result_layout_image, cv2.COLOR_RGB2BGR)
if kb:
result_layout_image = cv2.cvtColor(result_layout_image, cv2.COLOR_RGB2BGR)
result_layout_image_base64 = resize_image_to_kb_base64(
result_layout_image, int(kb)
result_layout_image_bytes = resize_image_to_kb(
result_layout_image, None, int(kb), dpi=dpi
)
else:
result_layout_image_base64 = numpy_2_base64(result_layout_image)
result_layout_image_bytes = save_image_dpi_to_bytes(result_layout_image, None, dpi=dpi)

result_layout_image_base64 = bytes_2_base64(result_layout_image_bytes)

result_messgae = {
"status": True,
"image_base64": result_layout_image_base64,
}

# except Exception as e:
# result_messgae = {
# "status": False,
# }

return result_messgae


Expand All @@ -216,6 +210,7 @@ async def watermark(
color: str = "#000000",
space: int = 25,
kb: int = Form(None),
dpi: int = Form(300),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
Expand All @@ -224,11 +219,12 @@ async def watermark(
try:
result_image = add_watermark(img, text, size, opacity, angle, color, space)

result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
if kb:
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
result_image_bytes = resize_image_to_kb(result_image, None, int(kb), dpi=dpi)
else:
result_image_base64 = numpy_2_base64(result_image)
result_image_bytes = save_image_dpi_to_bytes(result_image, None, dpi=dpi)
result_image_base64 = bytes_2_base64(result_image_bytes)

result_messgae = {
"status": True,
Expand All @@ -237,7 +233,7 @@ async def watermark(
except Exception as e:
result_messgae = {
"status": False,
"error": e,
"error": str(e),
}

return result_messgae
Expand All @@ -247,6 +243,7 @@ async def watermark(
@app.post("/set_kb")
async def set_kb(
input_image: UploadFile,
dpi: int = Form(300),
kb: int = Form(50),
):
image_bytes = await input_image.read()
Expand All @@ -255,7 +252,8 @@ async def set_kb(

try:
result_image = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
result_image_bytes = resize_image_to_kb(result_image, None, int(kb), dpi=dpi)
result_image_base64 = bytes_2_base64(result_image_bytes)

result_messgae = {
"status": True,
Expand All @@ -278,6 +276,7 @@ async def idphoto_crop_inference(
width: int = Form(295),
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,
Expand Down Expand Up @@ -305,14 +304,17 @@ async def idphoto_crop_inference(
result_message = {"status": False}
# 如果检测到人脸数量等于1, 则返回标准证和高清照结果(png 4通道图像)
else:
result_image_standard_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA), None, dpi)

result_message = {
"status": True,
"image_base64_standard": numpy_2_base64(result.standard),
"image_base64_standard": bytes_2_base64(result_image_standard_bytes),
}

# 如果hd为True, 则增加高清照结果(png 4通道图像)
if hd:
result_message["image_base64_hd"] = numpy_2_base64(result.hd)
result_image_hd_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.hd, cv2.COLOR_RGBA2BGRA), None, dpi)
result_message["image_base64_hd"] = bytes_2_base64(result_image_hd_bytes)

return result_message

Expand Down
Loading

0 comments on commit fe8886f

Please sign in to comment.