Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: api default kb #119

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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