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

通过api生成白底的证件照问题 #197

Open
ioer2008 opened this issue Oct 27, 2024 · 3 comments
Open

通过api生成白底的证件照问题 #197

ioer2008 opened this issue Oct 27, 2024 · 3 comments

Comments

@ioer2008
Copy link

通过api生成白底的证件照是不是需要调用2次api?

1.调用接口 生成证件照(底透明)

2.调用接口 添加白色背景色

我的问题是这个在网页上面这么操作的话无可厚非,但是通过接口的话,就要考虑保持接口1返回的值,再post到接口2中,不知道后面能否再增加一个新的接口,实现1+2的功能

@ioer2008
Copy link
Author

我用cursor生成了一个方法,本地测试可用,方便的话,可以加到api中

添加新的接口: 生成带背景图的证件照

@app.post("/idphoto_with_background")
async def idphoto_with_background_inference(
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"),
face_detect_model: str = Form("mtcnn"),
hd: bool = Form(True),
dpi: int = Form(300),
face_align: bool = Form(False),
head_measure_ratio: float = 0.2,
head_height_ratio: float = 0.45,
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
color: str = Form("000000"),
render: int = Form(0),
):
# 如果传入了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)

# 将字符串转为元组
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),
        face_alignment=face_align,
    )
except FaceError:
    result_message = {"status": False}
# 如果检测到人脸数量等于1, 则返回标准证和高清照结果(png 4通道图像)
else:
    # 将证件照结果转为4通道BGRA图像
    result_image_standard = cv2.cvtColor(result.standard, cv2.COLOR_RGBA2BGRA)
    if hd:
        result_image_hd = cv2.cvtColor(result.hd, cv2.COLOR_RGBA2BGRA)

    # 添加背景色
    render_choice = ["pure_color", "updown_gradient", "center_gradient"]
    color = hex_to_rgb(color)
    color = (color[2], color[1], color[0])

    result_image_standard = add_background(
        result_image_standard,
        bgr=color,
        mode=render_choice[render],
    ).astype(np.uint8)
    
    result_image_standard_bytes = save_image_dpi_to_bytes(result_image_standard, None, dpi)
    
    result_message = {
        "status": True,
        "image_base64_standard": bytes_2_base64(result_image_standard_bytes),
    }

    # 如果hd为True, 则增加高清照结果
    if hd:
        result_image_hd = add_background(
            result_image_hd,
            bgr=color,
            mode=render_choice[render],
        ).astype(np.uint8)
        result_image_hd_bytes = save_image_dpi_to_bytes(result_image_hd, None, dpi)
        result_message["image_base64_hd"] = bytes_2_base64(result_image_hd_bytes)

return result_message

@shen774411223d
Copy link
Contributor

是的,目前接口是解耦提供的。

其实不用这么麻烦,你可以

@app.post("/generate-idphoto-task")
async def generate_idphoto_reference(data):
    result = idphoto_inference(**data)
    result2 = photo_add_background(**result)

就像调用函数一样就可以了

@ioer2008
Copy link
Author

不好意思,python语言我不会,但是你的代码,我本地报错

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@ioer2008 @shen774411223d and others