Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyi-Lin committed Sep 14, 2024
2 parents d5a2728 + fbb0d0b commit 173682a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions deploy_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async def human_matting_inference(
async def photo_add_background(
input_image: UploadFile,
color: str = Form("000000"),
kb: int = Form(50),
kb: int = Form(None),
render: int = Form(0),
):
render_choice = ["pure_color", "updown_gradient", "center_gradient"]
Expand Down Expand Up @@ -167,7 +167,7 @@ async def generate_layout_photos(
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
kb: int = Form(50),
kb: int = Form(None),
):
# try:
image_bytes = await input_image.read()
Expand Down Expand Up @@ -205,7 +205,7 @@ async def generate_layout_photos(
return result_messgae


# 透明图像添加纯色背景接口
# 透明图像添加水印接口
@app.post("/watermark")
async def watermark(
input_image: UploadFile,
Expand All @@ -215,6 +215,7 @@ async def watermark(
angle: int = 30,
color: str = "#000000",
space: int = 25,
kb: int = Form(None),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
Expand All @@ -223,9 +224,15 @@ async def watermark(
try:
result_image = add_watermark(img, text, size, opacity, angle, color, space)

if kb:
result_image = cv2.cvtColor(result_image, cv2.COLOR_RGB2BGR)
result_image_base64 = resize_image_to_kb_base64(result_image, int(kb))
else:
result_image_base64 = numpy_2_base64(result_image)

result_messgae = {
"status": True,
"image_base64": numpy_2_base64(result_image),
"image_base64": result_image_base64,
}
except Exception as e:
result_messgae = {
Expand Down
4 changes: 2 additions & 2 deletions hivision/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ def resize_image_to_kb_base64(input_image, target_size_kb, mode="exact"):

# Encode the image data to base64
img_base64 = base64.b64encode(img_byte_arr.getvalue()).decode("utf-8")
return img_base64
return "data:image/png;base64," + img_base64


def numpy_2_base64(img: np.ndarray) -> str:
_, buffer = cv2.imencode(".png", img)
base64_image = base64.b64encode(buffer).decode("utf-8")

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


def base64_2_numpy(base64_image: str) -> np.ndarray:
Expand Down

0 comments on commit 173682a

Please sign in to comment.