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

时间:2022-12-07 15:53:27
「模型即服务AI」1分钟调用SOTA人脸检测,同时搭建时光相册小应用
时光相册应用效果

一、物料

人脸检测:https://modelscope.cn/models/damo/cv_resnet101_face-detection_cvpr22papermogface/summary

时光相册: https://modelscope.cn/studios/damo/face_album/summary

二、背景

        最近有两个计算机应用发展的方向正在潜移默化的汇拢中:1.)模型即服务 2.)人工智能(AI)。它们的会师正逐渐形成模型即服务AI热潮,开源典型的代表有国外的huggingface以及国内的modelscope。

        什么是模型即服务呢?顾名思义模型即服务(MaaS)是一种用较少的代码、以较快的速度来调用AI功能以及部署AI服务的平台。通过少量代码或不用代码实现AI场景应用创新。

        什么是人工智能呢?虽老生常谈,但本着空杯的心态,这里亦引用了百度百科上的定义:“人工智能(Artificial Intelligence),英文缩写为AI。它是研究开发用于模拟延伸和扩展人的智能的理论、方法、技术及应用系统的一门新的技术科学。该领域的研究包括机器人、语言识别、图像识别、自然语言处理和专家系统等。”

        近几年模型即服务一直被人津津乐道,这是提升AI编程效率、加速AI创新应用的大趋势。人工智能领域近几年非常火热,基于AI的行业创新应用层出不穷,尤其今年的AI绘画又大有元年之势,相应介绍可查阅《人工智能内容生成元年—AI绘画原理解析》。如下章节将重点介绍如何通过模型即服务来完成AI功能调用以及相应AI应用搭建。

三、方法

1.)模型即服务AI功能调用:

        首先打开notebook,可以通过示例右上角创建账号申领。亦或通过本地python环境安装直接调用(若本地环境暂无pip,则查看文档中“Python环境配置”部分)。等到环境ready后,试跑如下示例代码:

from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

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))

# if you want to show the result, you can run
from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result
from modelscope.preprocessors.image import LoadImage
import cv2
import numpy as np

# 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)
# save dst image as bgr order to local
cv2.imwrite('dst_img.jpg', dst_img)
# show dst image by rgb order
import matplotlib.pyplot as plt
dst_img  = cv2.cvtColor(np.asarray(dst_img), cv2.COLOR_BGR2RGB)
plt.imshow(dst_img)

这样就完成了模型即服务的AI功能调用了。可以将这快速应用在平时需要调用人脸检测的场景中了。

2.)模型即服务AI应用搭建:

        在完成1.)步骤之后,可能有人会有疑问:“1.)步骤中的代码只适合自己调用,如何形成模型即服务的AI应用服务给更多人分享使用呢?”。本节重点介绍如何基于1.)进而完成相应模型即服务的样例AI应用。该应用的搭建将基于gradio实现,具体步骤如下:

a.)按照手册完成gradio环境配置。

b.)建立app.py文件并填充如下代码:

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('''
      <div style="text-align: center; max-width: 720px; margin: 0 auto;">
                  <div
                    style="
                      display: inline-flex;
                      align-items: center;
                      gap: 0.8rem;
                      font-size: 1.75rem;
                    "
                  >
                    <h1 style="font-family:  PingFangSC; font-weight: 500; font-size: 32px; margin-bottom: 7px;">
                      AI人脸检测应用
                    </h1>
      ''')

    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 = [['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)

c.)在本地或者线上环境执行如下指令:python app.py

d.)复制粘贴如下红框相应服务链接进行分享使用

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

3.)模型即服务AI复杂应用:

        在完成2.)步骤之后,相信很多同学对模型即服务AI功能调用、AI应用搭建有了很好的初体验,可能有些同学还是意犹未尽。本节重点介绍如何基于人脸检测搭建“时光相册”, 随意上传您心仪的照片合集(宝宝成长相册、同学毕业相册),通过人脸五官对齐,一键生成样貌变化效果图,点点鼠标即可下载分享给亲朋好友。效果图如下:

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

代码可直接拷贝这里(所有文件.py文件以及images文件都下载下来)。然后在本地或者线上环境执行如下指令:

python app.py

命令行log信息如下所示,将红色框的链接粘贴至浏览器即可体验该应用:

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

 效果与官网应用“时光相册”类似。 

四、讨论

        回首过往,作为2007级本科生,当时是大类招生的,到了2008年大二的时候就需要选方向了。当时大二选方向时,有很多热门的专业,如“自动化”、“信息与通信”等,“计算机”在当时不可不谓是冷门的存在。由于当时大一参加了软件协会(后孵化了ACM集训队),迷上了程序设计语言的创造力,所以选择了计算机专业。

        转眼进入了专业课的学习,当时上了一门人工智能课程,里面讲到了“手写数字识别”这样BP神经网络案例,当即被人工智能的创造力吸引到了。在当时实现手写数字识别还需要用较重的MFC应用搭建方式,如果不太熟悉的话,应用入门成本还是比较高的。现如今结合模型即服务AI热潮,相应应用搭建将会越来越方便,大把的精力可以集中放到AI技术与应用的普及和相应创新上了。

        同学们如有想要简单快速实现的其他AI应用也可在评论区留言,作者将择机对需求呼声较高的AI应用做模型即服务实现分享。