Skip to content

Commit fe8886f

Browse files
authored
feat: sync api (#161)
* align + hd + dpi * sync inference * sync api * docs * Update README.md * refactor api_CN * docs update
1 parent 4c2ac52 commit fe8886f

12 files changed

+317
-1720
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ test/temp/*
2525

2626
.python-version
2727

28+
# Ignore .png and .jpg files in the root directory
29+
/*.png
30+
/*.jpg

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
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增加**野兽模式**,可设置内存加载策略
58+
- 2024.09.22: Gradio Demo增加**野兽模式**,可设置内存加载策略 | API接口增加**dpi、face_alignment**参数
5959
- 2024.09.18: Gradio Demo增加**分享模版照**功能、增加**美式证件照**背景选项
6060
- 2024.09.17: Gradio Demo增加**自定义底色-HEX输入**功能 | **(社区贡献)C++版本** - [HivisionIDPhotos-cpp](https://github.com/zjkhahah/HivisionIDPhotos-cpp) 贡献 by [zjkhahah](https://github.com/zjkhahah)
6161
- 2024.09.16: Gradio Demo增加**人脸旋转对齐**功能,自定义尺寸输入支持**毫米**单位
@@ -256,8 +256,6 @@ python deploy_api.py
256256
详细请求方式请参考 [API 文档](docs/api_CN.md),包含以下请求示例:
257257
- [cURL](docs/api_CN.md#curl-请求示例)
258258
- [Python](docs/api_CN.md#python-请求示例)
259-
- [Java](docs/api_CN.md#java-请求示例)
260-
- [Javascript](docs/api_CN.md#javascript-请求示例)
261259

262260
<br>
263261

README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ English / [中文](README.md) / [日本語](README_JP.md) / [한국어](README_K
5454

5555
- 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]
5656

57+
- 2024.09.22: Gradio Demo adds **Beast Mode** and **DPI** parameter
5758
- 2024.09.18: Gradio Demo adds **Share Template Photos** feature and **American Style** background option
5859
- 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)
5960
- 2024.09.16: Gradio Demo adds **Face Rotation Alignment** feature, custom size input supports **millimeters**
6061
- 2024.09.14: Gradio Demo adds **Custom DPI** feature, adds Japanese and Korean support, adds **Adjust Brightness, Contrast, Sharpness** feature
6162
- 2024.09.12: Gradio Demo adds **Whitening** feature | API interface adds **Watermark**, **Set Photo KB Size**, **ID Photo Cropping**
6263
- 2024.09.11: Added **transparent image display and download** feature to Gradio Demo.
6364
- 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.
64-
- 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
6565

6666
<br>
6767

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

258256
<br>
259257

README_JP.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151

5252
- オンライン体験: [![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]
5353

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

6262
<br>
6363

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

254252
<br>
255253

README_KO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151

5252
- 온라인 체험: [![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]
5353

54+
- 2024.09.22: Gradio Demo에 **버스트 모드****DPI** 매개변수 추가
5455
- 2024.09.18: Gradio Demo에 **템플릿 사진 공유** 기능 추가, **미국식** 배경 옵션 추가
5556
- 2024.09.17: Gradio Demo에 **커스텀 배경색-HEX 입력** 기능 추가 | **(커뮤니티 기여) C++ 버전** - [HivisionIDPhotos-cpp](https://github.com/zjkhahah/HivisionIDPhotos-cpp) 기여 by [zjkhahah](https://github.com/zjkhahah)
5657
- 2024.09.16: Gradio Demo에 **얼굴 회전 정렬** 기능 추가, 커스텀 사이즈 입력에 **밀리미터** 단위 추가
5758
- 2024.09.14: Gradio Demo에 **커스텀 DPI** 기능 추가, 일본어와 한국어 추가, **밝기, 대비, 선명도 조절** 기능 추가
5859
- 2024.09.12: Gradio 데모에 **미백** 기능 추가 | API 인터페이스에 **워터마크 추가**, **사진 KB 크기 설정**, **증명사진 자르기** 추가
5960
- 2024.09.11: Gradio Demo에 **투명 이미지 표시 및 다운로드** 기능 추가
60-
- 2024.09.09: 새로운 **배경 제거 모델** [BiRefNet-v1-lite](https://github.com/ZhengPeng7/BiRefNet) 추가 | Gradio에 **고급 매개변수 설정****워터마크** 탭 추가
6161

6262
<br>
6363

deploy_api.py

+43-41
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
from hivision.creator.choose_handler import choose_handler
99
from hivision.utils import (
1010
add_background,
11-
resize_image_to_kb_base64,
11+
resize_image_to_kb,
12+
bytes_2_base64,
13+
numpy_2_base64,
1214
hex_to_rgb,
1315
add_watermark,
16+
save_image_dpi_to_bytes,
1417
)
1518
import base64
1619
import numpy as np
@@ -32,23 +35,17 @@
3235
)
3336

3437

35-
# 将图像转换为Base64编码
36-
def numpy_2_base64(img: np.ndarray):
37-
retval, buffer = cv2.imencode(".png", img)
38-
base64_image = base64.b64encode(buffer).decode("utf-8")
39-
40-
return "data:image/png;base64," + base64_image
41-
42-
4338
# 证件照智能制作接口
4439
@app.post("/idphoto")
4540
async def idphoto_inference(
4641
input_image: UploadFile,
4742
height: int = Form(413),
4843
width: int = Form(295),
49-
human_matting_model: str = Form("hivision_modnet"),
44+
human_matting_model: str = Form("modnet_photographic_portrait_matting"),
5045
face_detect_model: str = Form("mtcnn"),
5146
hd: bool = Form(True),
47+
dpi: int = Form(300),
48+
face_align: bool = Form(False),
5249
head_measure_ratio: float = 0.2,
5350
head_height_ratio: float = 0.45,
5451
top_distance_max: float = 0.12,
@@ -70,19 +67,23 @@ async def idphoto_inference(
7067
head_measure_ratio=head_measure_ratio,
7168
head_height_ratio=head_height_ratio,
7269
head_top_range=(top_distance_max, top_distance_min),
70+
face_alignment=face_align,
7371
)
7472
except FaceError:
7573
result_message = {"status": False}
7674
# 如果检测到人脸数量等于1, 则返回标准证和高清照结果(png 4通道图像)
7775
else:
76+
result_image_standard_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA), None, dpi)
77+
7878
result_message = {
7979
"status": True,
80-
"image_base64_standard": numpy_2_base64(result.standard),
80+
"image_base64_standard": bytes_2_base64(result_image_standard_bytes),
8181
}
8282

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

8788
return result_message
8889

@@ -92,6 +93,7 @@ async def idphoto_inference(
9293
async def human_matting_inference(
9394
input_image: UploadFile,
9495
human_matting_model: str = Form("hivision_modnet"),
96+
dpi: int = Form(300),
9597
):
9698
image_bytes = await input_image.read()
9799
nparr = np.frombuffer(image_bytes, np.uint8)
@@ -109,9 +111,10 @@ async def human_matting_inference(
109111
result_message = {"status": False}
110112

111113
else:
114+
result_image_standard_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA), None, dpi)
112115
result_message = {
113116
"status": True,
114-
"image_base64": numpy_2_base64(result.standard),
117+
"image_base64": bytes_2_base64(result_image_standard_bytes),
115118
}
116119
return result_message
117120

@@ -122,6 +125,7 @@ async def photo_add_background(
122125
input_image: UploadFile,
123126
color: str = Form("000000"),
124127
kb: int = Form(None),
128+
dpi: int = Form(300),
125129
render: int = Form(0),
126130
):
127131
render_choice = ["pure_color", "updown_gradient", "center_gradient"]
@@ -139,25 +143,17 @@ async def photo_add_background(
139143
mode=render_choice[render],
140144
).astype(np.uint8)
141145

146+
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
142147
if kb:
143-
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
144-
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
148+
result_image_bytes = resize_image_to_kb(result_image, None, int(kb), dpi=dpi)
145149
else:
146-
result_image_base64 = numpy_2_base64(result_image)
150+
result_image_bytes = save_image_dpi_to_bytes(result_image, None, dpi=dpi)
147151

148-
# try:
149152
result_messgae = {
150153
"status": True,
151-
"image_base64": result_image_base64,
154+
"image_base64": bytes_2_base64(result_image_bytes),
152155
}
153156

154-
# except Exception as e:
155-
# print(e)
156-
# result_messgae = {
157-
# "status": False,
158-
# "error": e
159-
# }
160-
161157
return result_messgae
162158

163159

@@ -168,6 +164,7 @@ async def generate_layout_photos(
168164
height: int = Form(413),
169165
width: int = Form(295),
170166
kb: int = Form(None),
167+
dpi: int = Form(300),
171168
):
172169
# try:
173170
image_bytes = await input_image.read()
@@ -184,24 +181,21 @@ async def generate_layout_photos(
184181
img, typography_arr, typography_rotate, height=size[0], width=size[1]
185182
).astype(np.uint8)
186183

184+
result_layout_image = cv2.cvtColor(result_layout_image, cv2.COLOR_RGB2BGR)
187185
if kb:
188-
result_layout_image = cv2.cvtColor(result_layout_image, cv2.COLOR_RGB2BGR)
189-
result_layout_image_base64 = resize_image_to_kb_base64(
190-
result_layout_image, int(kb)
186+
result_layout_image_bytes = resize_image_to_kb(
187+
result_layout_image, None, int(kb), dpi=dpi
191188
)
192189
else:
193-
result_layout_image_base64 = numpy_2_base64(result_layout_image)
190+
result_layout_image_bytes = save_image_dpi_to_bytes(result_layout_image, None, dpi=dpi)
191+
192+
result_layout_image_base64 = bytes_2_base64(result_layout_image_bytes)
194193

195194
result_messgae = {
196195
"status": True,
197196
"image_base64": result_layout_image_base64,
198197
}
199198

200-
# except Exception as e:
201-
# result_messgae = {
202-
# "status": False,
203-
# }
204-
205199
return result_messgae
206200

207201

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

222+
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
227223
if kb:
228-
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
229-
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
224+
result_image_bytes = resize_image_to_kb(result_image, None, int(kb), dpi=dpi)
230225
else:
231-
result_image_base64 = numpy_2_base64(result_image)
226+
result_image_bytes = save_image_dpi_to_bytes(result_image, None, dpi=dpi)
227+
result_image_base64 = bytes_2_base64(result_image_bytes)
232228

233229
result_messgae = {
234230
"status": True,
@@ -237,7 +233,7 @@ async def watermark(
237233
except Exception as e:
238234
result_messgae = {
239235
"status": False,
240-
"error": e,
236+
"error": str(e),
241237
}
242238

243239
return result_messgae
@@ -247,6 +243,7 @@ async def watermark(
247243
@app.post("/set_kb")
248244
async def set_kb(
249245
input_image: UploadFile,
246+
dpi: int = Form(300),
250247
kb: int = Form(50),
251248
):
252249
image_bytes = await input_image.read()
@@ -255,7 +252,8 @@ async def set_kb(
255252

256253
try:
257254
result_image = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
258-
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
255+
result_image_bytes = resize_image_to_kb(result_image, None, int(kb), dpi=dpi)
256+
result_image_base64 = bytes_2_base64(result_image_bytes)
259257

260258
result_messgae = {
261259
"status": True,
@@ -278,6 +276,7 @@ async def idphoto_crop_inference(
278276
width: int = Form(295),
279277
face_detect_model: str = Form("mtcnn"),
280278
hd: bool = Form(True),
279+
dpi: int = Form(300),
281280
head_measure_ratio: float = 0.2,
282281
head_height_ratio: float = 0.45,
283282
top_distance_max: float = 0.12,
@@ -305,14 +304,17 @@ async def idphoto_crop_inference(
305304
result_message = {"status": False}
306305
# 如果检测到人脸数量等于1, 则返回标准证和高清照结果(png 4通道图像)
307306
else:
307+
result_image_standard_bytes = save_image_dpi_to_bytes(cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA), None, dpi)
308+
308309
result_message = {
309310
"status": True,
310-
"image_base64_standard": numpy_2_base64(result.standard),
311+
"image_base64_standard": bytes_2_base64(result_image_standard_bytes),
311312
}
312313

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

317319
return result_message
318320

0 commit comments

Comments
 (0)