mirror of
https://github.com/Zeyi-Lin/HivisionIDPhotos.git
synced 2025-09-15 14:58:34 +08:00
parent
0712b789cb
commit
530481b137
13
README.md
13
README.md
@ -191,7 +191,7 @@ python app.py
|
|||||||
输入 1 张照片,获得 1 张标准证件照和 1 张高清证件照的 4 通道透明 png
|
输入 1 张照片,获得 1 张标准证件照和 1 张高清证件照的 4 通道透明 png
|
||||||
|
|
||||||
```python
|
```python
|
||||||
python inference.py -i demo/images/test.jpg -o ./idphoto.png --height 413 --width 295
|
python inference.py -i demo/images/test0.jpg -o ./idphoto.png --height 413 --width 295
|
||||||
```
|
```
|
||||||
|
|
||||||
## 2. 人像抠图
|
## 2. 人像抠图
|
||||||
@ -199,7 +199,7 @@ python inference.py -i demo/images/test.jpg -o ./idphoto.png --height 413 --widt
|
|||||||
输入 1 张照片,获得 1张 4 通道透明 png
|
输入 1 张照片,获得 1张 4 通道透明 png
|
||||||
|
|
||||||
```python
|
```python
|
||||||
python inference.py -t human_matting -i demo/images/test.jpg -o ./idphoto_matting.png --matting_model hivision_modnet
|
python inference.py -t human_matting -i demo/images/test0.jpg -o ./idphoto_matting.png --matting_model hivision_modnet
|
||||||
```
|
```
|
||||||
|
|
||||||
## 3. 透明图增加底色
|
## 3. 透明图增加底色
|
||||||
@ -218,6 +218,15 @@ python inference.py -t add_background -i ./idphoto.png -o ./idphoto_ab.jpg -c 4
|
|||||||
python inference.py -t generate_layout_photos -i ./idphoto_ab.jpg -o ./idphoto_layout.jpg --height 413 --width 295 -k 200
|
python inference.py -t generate_layout_photos -i ./idphoto_ab.jpg -o ./idphoto_layout.jpg --height 413 --width 295 -k 200
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 5. 证件照裁剪
|
||||||
|
|
||||||
|
输入 1 张 4 通道照片(抠图好的图像),获得 1 张标准证件照和 1 张高清证件照的 4 通道透明 png
|
||||||
|
|
||||||
|
```python
|
||||||
|
python inference.py -t idphoto_crop -i ./idphoto_matting.png -o ./idphoto_crop.png --height 413 --width 295
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
# ⚡️ 部署 API 服务
|
# ⚡️ 部署 API 服务
|
||||||
|
|||||||
@ -252,6 +252,54 @@ async def set_kb(
|
|||||||
return result_messgae
|
return result_messgae
|
||||||
|
|
||||||
|
|
||||||
|
# 证件照智能裁剪接口
|
||||||
|
@app.post("/idphoto_crop")
|
||||||
|
async def idphoto_crop_inference(
|
||||||
|
input_image: UploadFile,
|
||||||
|
height: int = Form(413),
|
||||||
|
width: int = Form(295),
|
||||||
|
face_detect_model: str = Form("mtcnn"),
|
||||||
|
hd: bool = Form(True),
|
||||||
|
head_measure_ratio: float = 0.2,
|
||||||
|
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_UNCHANGED) # 读取图像(4通道)
|
||||||
|
|
||||||
|
# ------------------- 选择抠图与人脸检测模型 -------------------
|
||||||
|
choose_handler(creator, face_detect_option=face_detect_model)
|
||||||
|
|
||||||
|
# 将字符串转为元组
|
||||||
|
size = (int(height), int(width))
|
||||||
|
try:
|
||||||
|
result = creator(
|
||||||
|
img,
|
||||||
|
size=size,
|
||||||
|
head_measure_ratio=head_measure_ratio,
|
||||||
|
head_height_ratio=head_height_ratio,
|
||||||
|
head_top_range=(top_distance_max, top_distance_min),
|
||||||
|
crop_only=True,
|
||||||
|
)
|
||||||
|
except FaceError:
|
||||||
|
result_message = {"status": False}
|
||||||
|
# 如果检测到人脸数量等于1, 则返回标准证和高清照结果(png 4通道图像)
|
||||||
|
else:
|
||||||
|
result_message = {
|
||||||
|
"status": True,
|
||||||
|
"image_base64_standard": numpy_2_base64(result.standard),
|
||||||
|
}
|
||||||
|
|
||||||
|
# 如果hd为True, 则增加高清照结果(png 4通道图像)
|
||||||
|
if hd:
|
||||||
|
result_message["image_base64_hd"] = numpy_2_base64(result.hd)
|
||||||
|
|
||||||
|
return result_message
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
|
|||||||
@ -42,19 +42,19 @@ python deploy_api.py
|
|||||||
|
|
||||||
接口名:`add_background`
|
接口名:`add_background`
|
||||||
|
|
||||||
`添加背景色`接口的逻辑是发送一张 RGBA 图像,根据`color`添加背景色,合成一张 JPG 图像。
|
`添加背景色`接口的逻辑是接收一张 RGBA 图像(透明图),根据`color`添加背景色,合成一张 JPG 图像。
|
||||||
|
|
||||||
### 3.生成六寸排版照
|
### 3.生成六寸排版照
|
||||||
|
|
||||||
接口名:`generate_layout_photos`
|
接口名:`generate_layout_photos`
|
||||||
|
|
||||||
`生成六寸排版照`接口的逻辑是发送一张 RGB 图像(一般为添加背景色之后的证件照),根据`size`进行照片排布,然后生成一张六寸排版照。
|
`生成六寸排版照`接口的逻辑是接收一张 RGB 图像(一般为添加背景色之后的证件照),根据`size`进行照片排布,然后生成一张六寸排版照。
|
||||||
|
|
||||||
### 4.人像抠图
|
### 4.人像抠图
|
||||||
|
|
||||||
接口名:`human_matting`
|
接口名:`human_matting`
|
||||||
|
|
||||||
`人像抠图`接口的逻辑是发送一张 RGB 图像,输出一张标准抠图人像照和高清抠图人像照(无任何背景填充)。
|
`人像抠图`接口的逻辑是接收一张 RGB 图像,输出一张标准抠图人像照和高清抠图人像照(无任何背景填充)。
|
||||||
|
|
||||||
### 5.图像加水印
|
### 5.图像加水印
|
||||||
|
|
||||||
@ -69,6 +69,12 @@ python deploy_api.py
|
|||||||
|
|
||||||
`设置图像KB大小`接口的功能是接收一张图像和目标文件大小(以KB为单位),如果设置的KB值小于原文件,则调整压缩率;如果设置的KB值大于源文件,则通过给文件头添加信息的方式调大KB值,目标是让图像的最终大小与设置的KB值一致。
|
`设置图像KB大小`接口的功能是接收一张图像和目标文件大小(以KB为单位),如果设置的KB值小于原文件,则调整压缩率;如果设置的KB值大于源文件,则通过给文件头添加信息的方式调大KB值,目标是让图像的最终大小与设置的KB值一致。
|
||||||
|
|
||||||
|
### 7.证件照裁切
|
||||||
|
|
||||||
|
接口名:`idphoto_crop`
|
||||||
|
|
||||||
|
`证件照裁切`接口的功能是接收一张 RBGA 图像(透明图),输出一张标准证件照和一张高清证件照。
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
## cURL 请求示例
|
## cURL 请求示例
|
||||||
@ -135,6 +141,18 @@ curl -X 'POST' \
|
|||||||
-F 'kb=50'
|
-F 'kb=50'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 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' \
|
||||||
|
-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'
|
||||||
|
```
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
@ -236,7 +254,8 @@ else:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### 6. 设置图像KB大小
|
### 6. 设置图像KB大小
|
||||||
```bash
|
|
||||||
|
```python
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# 设置请求的 URL
|
# 设置请求的 URL
|
||||||
@ -259,6 +278,44 @@ else:
|
|||||||
print(f"Request failed with status code {response.status_code}: {response.text}")
|
print(f"Request failed with status code {response.status_code}: {response.text}")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 7. 证件照裁切
|
||||||
|
|
||||||
|
```python
|
||||||
|
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")}
|
||||||
|
data = {
|
||||||
|
"height": 413,
|
||||||
|
"width": 295,
|
||||||
|
"face_detect_model": "mtcnn",
|
||||||
|
"hd": "true"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 发送 POST 请求
|
||||||
|
response = requests.post(url, params=params, files=files, data=data)
|
||||||
|
|
||||||
|
# 检查响应
|
||||||
|
if response.ok:
|
||||||
|
# 输出响应内容
|
||||||
|
print(response.json())
|
||||||
|
else:
|
||||||
|
# 输出错误信息
|
||||||
|
print(f"Request failed with status code {response.status_code}: {response.text}")
|
||||||
|
```
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
## Java 请求示例
|
## Java 请求示例
|
||||||
|
|||||||
18
inference.py
18
inference.py
@ -19,6 +19,7 @@ INFERENCE_TYPE = [
|
|||||||
"add_background",
|
"add_background",
|
||||||
"generate_layout_photos",
|
"generate_layout_photos",
|
||||||
"watermark",
|
"watermark",
|
||||||
|
"idphoto_crop",
|
||||||
]
|
]
|
||||||
MATTING_MODEL = [
|
MATTING_MODEL = [
|
||||||
"hivision_modnet",
|
"hivision_modnet",
|
||||||
@ -149,3 +150,20 @@ elif args.type == "generate_layout_photos":
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
cv2.imwrite(args.output_image_dir, result_layout_image)
|
cv2.imwrite(args.output_image_dir, result_layout_image)
|
||||||
|
|
||||||
|
# 如果模式是证件照裁切
|
||||||
|
elif args.type == "idphoto_crop":
|
||||||
|
# 将字符串转为元组
|
||||||
|
size = (int(args.height), int(args.width))
|
||||||
|
try:
|
||||||
|
result = creator(input_image, size=size, crop_only=True)
|
||||||
|
except FaceError:
|
||||||
|
print("人脸数量不等于 1,请上传单张人脸的图像。")
|
||||||
|
else:
|
||||||
|
# 保存标准照
|
||||||
|
cv2.imwrite(args.output_image_dir, result.standard)
|
||||||
|
|
||||||
|
# 保存高清照
|
||||||
|
file_name, file_extension = os.path.splitext(args.output_image_dir)
|
||||||
|
new_file_name = file_name + "_hd" + file_extension
|
||||||
|
cv2.imwrite(new_file_name, result.hd)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user