「模型即服务AI」1分钟调用SOTA人脸检测,同时搭建时光相册小应用

520阅读 0评论2022-12-28 专注的阿熊
分类:Python/Ruby

import gradio as gr

from modelscope.pipelines import pipeline

from modelscope.utils.constant import Tasks

from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result

from modelscope.preprocessors.image import LoadImage

from PIL import Image

import cv2

import numpy as np

###########################################

# gradio demo app 推断入口

###########################################

# gradio app demo 算法运行函数

def inference(input_file):

    mog_face_detection_func = pipeline(Tasks.face_detection, 'damo/cv_resnet101_face-detection_cvpr22papermogface')

    src_img_path = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/mog_face_detection.jpg'

    raw_result = mog_face_detection_func(src_img_path)

    print('face detection output: {}.'.format(raw_result))

    # load image from url as rgb order

    src_img = LoadImage.convert_to_ndarray(src_img_path)

    # save src image as bgr order to local

    src_img  = cv2.cvtColor(np.asarray(src_img), cv2.COLOR_RGB2BGR)

    cv2.imwrite('src_img.jpg', src_img)

    # draw dst image from local src image as bgr order

    dst_img = draw_face_detection_no_lm_result('src_img.jpg', raw_result)

    # convert to rgb order

    dst_img  = cv2.cvtColor(np.asarray(dst_img), cv2.COLOR_BGR2RGB)

    return dst_img

# gradio app 环境参数

css_style = "#fixed_size_img {height: 240px;} " \

            "#overview {margin: auto;max-width: 600px; max-height: 400px;}"

title = "AI人脸检测应用"

###########################################

# gradio demo app

###########################################

with gr.Blocks(title=title, css=css_style) as demo:

    gr.HTML('''

      

                  

                    style="

                      display: inline-flex;

                      align-items: center;

                      gap: 0.8rem;

                      font-size: 1.75rem;

                    "

                  >

                    

                      AI人脸检测应用

                    

      ''')

    with gr.Row():

        img_input = gr.Image(type="pil", elem_id="fixed_size_img")

        img_output = gr.Image(type="pil", elem_id="fixed_size_img")

    with gr.Row():

        btn_submit = gr.Button(value="一键生成", elem_id="blue_btn")

    examples =跟单网gendan5.com [['https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/mog_face_detection.jpg']]

    examples = gr.Examples(examples=examples, inputs=img_input, outputs=img_output, label="点击如下示例试玩", run_on_click=True)

    btn_submit.click(inference, inputs=[img_input], outputs=img_output)

    # btn_clear清除画布

if __name__ == "__main__":

    demo.launch(share=True)

上一篇:Python写了个疫情信息快速查看工具
下一篇:【python】词云图制作