Skip to content

Commit

Permalink
feat: api image base64 (#166)
Browse files Browse the repository at this point in the history
* update base64

* update api docs
  • Loading branch information
Zeyi-Lin authored Sep 23, 2024
1 parent 553a6fd commit 24f7b6e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 48 deletions.
92 changes: 61 additions & 31 deletions deploy_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import FastAPI, UploadFile, Form
from fastapi import FastAPI, UploadFile, Form, File
from hivision import IDCreator
from hivision.error import FaceError
from hivision.creator.layout_calculator import (
Expand All @@ -10,7 +10,7 @@
add_background,
resize_image_to_kb,
bytes_2_base64,
numpy_2_base64,
base64_2_numpy,
hex_to_rgb,
add_watermark,
save_image_dpi_to_bytes,
Expand Down Expand Up @@ -38,7 +38,8 @@
# 证件照智能制作接口
@app.post("/idphoto")
async def idphoto_inference(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
height: int = Form(413),
width: int = Form(295),
human_matting_model: str = Form("modnet_photographic_portrait_matting"),
Expand All @@ -50,10 +51,15 @@ async def idphoto_inference(
head_height_ratio: float = 0.45,
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
):
# 如果传入了base64,则直接使用base64解码
if input_image_base64:
img = base64_2_numpy(input_image_base64)
# 否则使用上传的图片
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

# ------------------- 选择抠图与人脸检测模型 -------------------
choose_handler(creator, human_matting_model, face_detect_model)
Expand Down Expand Up @@ -91,13 +97,17 @@ async def idphoto_inference(
# 人像抠图接口
@app.post("/human_matting")
async def human_matting_inference(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
human_matting_model: str = Form("hivision_modnet"),
dpi: int = Form(300),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if input_image_base64:
img = base64_2_numpy(input_image_base64)
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

# ------------------- 选择抠图与人脸检测模型 -------------------
choose_handler(creator, human_matting_model, None)
Expand All @@ -122,17 +132,21 @@ async def human_matting_inference(
# 透明图像添加纯色背景接口
@app.post("/add_background")
async def photo_add_background(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
color: str = Form("000000"),
kb: int = Form(None),
dpi: int = Form(300),
render: int = Form(0),
):
render_choice = ["pure_color", "updown_gradient", "center_gradient"]

image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)
if input_image_base64:
img = base64_2_numpy(input_image_base64)
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED)

color = hex_to_rgb(color)
color = (color[2], color[1], color[0])
Expand Down Expand Up @@ -160,16 +174,20 @@ async def photo_add_background(
# 六寸排版照生成接口
@app.post("/generate_layout_photos")
async def generate_layout_photos(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
height: int = Form(413),
width: int = Form(295),
kb: int = Form(None),
dpi: int = Form(300),
):
# try:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if input_image_base64:
img = base64_2_numpy(input_image_base64)
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

size = (int(height), int(width))

Expand Down Expand Up @@ -202,7 +220,8 @@ async def generate_layout_photos(
# 透明图像添加水印接口
@app.post("/watermark")
async def watermark(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
text: str = Form("Hello"),
size: int = 20,
opacity: float = 0.5,
Expand All @@ -212,9 +231,12 @@ async def watermark(
kb: int = Form(None),
dpi: int = Form(300),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if input_image_base64:
img = base64_2_numpy(input_image_base64)
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

try:
result_image = add_watermark(img, text, size, opacity, angle, color, space)
Expand Down Expand Up @@ -242,13 +264,17 @@ async def watermark(
# 设置照片KB值接口(RGB图)
@app.post("/set_kb")
async def set_kb(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
dpi: int = Form(300),
kb: int = Form(50),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if input_image_base64:
img = base64_2_numpy(input_image_base64)
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

try:
result_image = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
Expand All @@ -271,7 +297,8 @@ async def set_kb(
# 证件照智能裁剪接口
@app.post("/idphoto_crop")
async def idphoto_crop_inference(
input_image: UploadFile,
input_image: UploadFile = File(None),
input_image_base64: str = Form(None),
height: int = Form(413),
width: int = Form(295),
face_detect_model: str = Form("mtcnn"),
Expand All @@ -282,9 +309,12 @@ async def idphoto_crop_inference(
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED) # 读取图像(4通道)
if input_image_base64:
img = base64_2_numpy(input_image_base64)
else:
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED) # 读取图像(4通道)

# ------------------- 选择抠图与人脸检测模型 -------------------
choose_handler(creator, face_detect_option=face_detect_model)
Expand Down
36 changes: 29 additions & 7 deletions docs/api_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

- [开始之前:开启后端服务](#开始之前开启后端服务)
- [接口功能说明](#接口功能说明)
- [1.生成证件照(底透明)](#1生成证件照底透明)
- [2.添加背景色](#2添加背景色)
- [3.生成六寸排版照](#3生成六寸排版照)
- [4.人像抠图](#4人像抠图)
- [5.图像加水印](#5图像加水印)
- [6.设置图像KB大小](#6设置图像KB大小)
- [7.证件照裁切](#7证件照裁切)
- [cURL 请求示例](#curl-请求示例)
- [Python 请求示例](#python-请求示例)

Expand Down Expand Up @@ -40,7 +47,8 @@ python deploy_api.py

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| input_image | file || 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image | file |`input_image_base64`二选一 | 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image_base64 | str |`input_image`二选一 | 传入的图像文件的base64编码,图像文件为需为RGB三通道图像。 |
| height | int || 标准证件照高度,默认为`413` |
| width | int || 标准证件照宽度,默认为`295` |
| human_matting_model | str || 人像分割模型,默认为`modnet_photographic_portrait_matting`。可选值为`modnet_photographic_portrait_matting``hivision_modnet``rmbg-1.4``birefnet-v1-lite` |
Expand All @@ -62,6 +70,8 @@ python deploy_api.py
| image_base64_standard | str | 标准证件照的base64编码 |
| image_base64_hd | str | 高清证件照的base64编码。如`hd`参数为`false`,则不返回该参数 |

<br>

### 2.添加背景色

接口名:`add_background`
Expand All @@ -72,7 +82,8 @@ python deploy_api.py

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| input_image | file || 传入的图像文件,图像文件为需为RGBA四通道图像。 |
| input_image | file |`input_image_base64`二选一 | 传入的图像文件,图像文件为需为RGBA四通道图像。 |
| input_image_base64 | str |`input_image`二选一 | 传入的图像文件的base64编码,图像文件为需为RGBA四通道图像。 |
| color | str || 背景色HEX值,默认为`000000` |
| kb | int || 输出照片的 KB 值,默认为`None`,即不对图像进行KB调整。|
| render | int || 渲染模式,默认为`0`。可选值为`0``1``2`,分别对应`纯色``上下渐变``中心渐变`|
Expand All @@ -85,6 +96,8 @@ python deploy_api.py
| status | int | 状态码,`true`表示成功 |
| image_base64 | str | 添加背景色之后的图像的base64编码 |

<br>

### 3.生成六寸排版照

接口名:`generate_layout_photos`
Expand All @@ -95,7 +108,8 @@ python deploy_api.py

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| input_image | file || 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image | file |`input_image_base64`二选一 | 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image_base64 | str |`input_image`二选一 | 传入的图像文件的base64编码,图像文件为需为RGB三通道图像。 |
| height | int || 输入图像的高度,默认为`413` |
| width | int || 输入图像的宽度,默认为`295` |
| kb | int || 输出照片的 KB 值,默认为`None`,即不对图像进行KB调整。|
Expand All @@ -108,6 +122,8 @@ python deploy_api.py
| status | int | 状态码,`true`表示成功 |
| image_base64 | str | 六寸排版照的base64编码 |

<br>

### 4.人像抠图

接口名:`human_matting`
Expand All @@ -129,6 +145,7 @@ python deploy_api.py
| status | int | 状态码,`true`表示成功 |
| image_base64 | str | 抠图人像照的base64编码 |

<br>

### 5.图像加水印

Expand All @@ -140,7 +157,8 @@ python deploy_api.py

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| input_image | file || 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image | file |`input_image_base64`二选一 | 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image_base64 | str |`input_image`二选一 | 传入的图像文件的base64编码,图像文件为需为RGB三通道图像。 |
| text | str || 水印文本,默认为`Hello` |
| size | int || 水印字体大小,默认为`20` |
| opacity | float || 水印透明度,默认为`0.5` |
Expand All @@ -156,6 +174,8 @@ python deploy_api.py
| status | int | 状态码,`true`表示成功 |
| image_base64 | str | 添加水印之后的图像的base64编码 |

<br>

### 6.设置图像KB大小

接口名:`set_kb`
Expand All @@ -166,7 +186,8 @@ python deploy_api.py

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| input_image | file || 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image | file |`input_image_base64`二选一 | 传入的图像文件,图像文件为需为RGB三通道图像。 |
| input_image_base64 | str |`input_image`二选一 | 传入的图像文件的base64编码,图像文件为需为RGB三通道图像。 |
| kb | int || 输出照片的 KB 值,默认为`None`,即不对图像进行KB调整。|
| dpi | int || 图像分辨率,默认为`300` |

Expand All @@ -177,7 +198,7 @@ python deploy_api.py
| status | int | 状态码,`true`表示成功 |
| image_base64 | str | 设置KB大小之后的图像的base64编码 |


<br>

### 7.证件照裁切

Expand All @@ -189,7 +210,8 @@ python deploy_api.py

| 参数名 | 类型 | 必填 | 说明 |
| :--- | :--- | :--- | :--- |
| input_image | file || 传入的图像文件,图像文件为需为RGBA四通道图像。 |
| input_image | file |`input_image_base64`二选一 | 传入的图像文件,图像文件为需为RGBA四通道图像。 |
| input_image_base64 | str |`input_image`二选一 | 传入的图像文件的base64编码,图像文件为需为RGBA四通道图像。 |
| height | int || 标准证件照高度,默认为`413` |
| width | int || 标准证件照宽度,默认为`295` |
| face_detect_model | str || 人脸检测模型,默认为`mtcnn`。可选值为`mtcnn``face_plusplus``retinaface-resnet50` |
Expand Down
Loading

0 comments on commit 24f7b6e

Please sign in to comment.