Skip to content

Commit

Permalink
feat: api watermark (#105)
Browse files Browse the repository at this point in the history
* api add watermark

* del requests_api and update api docs

* Update deploy_api.py

* refactor

* Update api_EN.md

* Update api_EN.md
  • Loading branch information
Zeyi-Lin authored Sep 11, 2024
1 parent 9d414a2 commit a1ccb1a
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 522 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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)

- 2024.09.11: Gradio Demo增加**透明图显示与下载**功能
- 2024.09.11: Gradio Demo增加**透明图显示与下载**功能 | API接口增加**加水印**
- 2024.09.10: 增加新的**人脸检测模型** Retinaface-resnet50,以稍弱于mtcnn的速度换取更高的检测精度,推荐使用
- 2024.09.09: 增加新的**抠图模型** [BiRefNet-v1-lite](https://github.com/ZhengPeng7/BiRefNet) | Gradio增加**高级参数设置****水印**选项卡
- 2024.09.08: 增加新的**抠图模型** [RMBG-1.4](https://huggingface.co/briaai/RMBG-1.4) | **ComfyUI工作流** - [HivisionIDPhotos-ComfyUI](https://github.com/AIFSH/HivisionIDPhotos-ComfyUI) 贡献 by [AIFSH](https://github.com/AIFSH/HivisionIDPhotos-ComfyUI)
Expand Down
4 changes: 2 additions & 2 deletions demo/processor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import numpy as np
from hivision import IDCreator
from hivision.error import FaceError, APIError
from hivision.utils import add_background, resize_image_to_kb
from hivision.utils import add_background, resize_image_to_kb, add_watermark
from hivision.creator.layout_calculator import (
generate_layout_photo,
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
from demo.utils import add_watermark, range_check
from demo.utils import range_check
import gradio as gr
import os
import time
Expand Down
17 changes: 0 additions & 17 deletions demo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,3 @@ def csv_to_color_list(csv_file: str) -> dict:
def range_check(value, min_value=0, max_value=255):
value = int(value)
return max(min_value, min(value, max_value))


def add_watermark(
image, text, size=50, opacity=0.5, angle=45, color="#8B8B1B", space=75
):
image = Image.fromarray(image)
watermarker = Watermarker(
input_image=image,
text=text,
style=WatermarkerStyles.STRIPED,
angle=angle,
color=color,
opacity=opacity,
size=size,
space=space,
)
return np.array(watermarker.image.convert("RGB"))
54 changes: 45 additions & 9 deletions deploy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
from hivision.utils import add_background, resize_image_to_kb_base64, hex_to_rgb
from hivision.utils import (
add_background,
resize_image_to_kb_base64,
hex_to_rgb,
add_watermark,
)
import base64
import numpy as np
import cv2
Expand All @@ -27,14 +32,14 @@ def numpy_2_base64(img: np.ndarray):
@app.post("/idphoto")
async def idphoto_inference(
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
height: str = Form(...),
width: str = Form(...),
human_matting_model: str = Form("hivision_modnet"),
face_detect_model: str = Form("mtcnn"),
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=0.2,
head_height_ratio=0.45,
top_distance_max=0.12,
top_distance_min=0.10,
):

image_bytes = await input_image.read()
Expand Down Expand Up @@ -101,7 +106,7 @@ async def idphoto_inference(
async def photo_add_background(
input_image: UploadFile,
color: str = Form(...),
kb: int = Form(None),
kb: str = Form(None),
render: int = Form(0),
):
render_choice = ["pure_color", "updown_gradient", "center_gradient"]
Expand Down Expand Up @@ -147,7 +152,7 @@ async def generate_layout_photos(
input_image: UploadFile,
height: str = Form(...),
width: str = Form(...),
kb: int = Form(None),
kb: str = Form(None),
):
# try:
image_bytes = await input_image.read()
Expand Down Expand Up @@ -185,6 +190,37 @@ async def generate_layout_photos(
return result_messgae


# 透明图像添加纯色背景接口
@app.post("/watermark")
async def watermark(
input_image: UploadFile,
text: str = Form("Hello"),
size: int = 20,
opacity: float = 0.5,
angle: int = 30,
color: str = "#000000",
space: int = 25,
):
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)

result_messgae = {
"status": True,
"image_base64": numpy_2_base64(result_image),
}
except Exception as e:
result_messgae = {
"status": False,
"error": e,
}

return result_messgae


if __name__ == "__main__":
import uvicorn

Expand Down
Loading

0 comments on commit a1ccb1a

Please sign in to comment.