关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

【OpenVI—代码解读系列】「模型即服务AI」1分钟调用SOTA人脸检测,同时搭建时光相册小应用

发布时间:2023-06-27 15:00:44
时光相册应用效果 一、物料 人脸检测:稳定调用及效果更好的API,详见视觉开放智能平台:人脸检测与五官定位 二、背景 最近有两个计算机应用发展的方向正在潜移默化的汇拢中:1.)模型即服务 2.)人工智能(AI)。它们的会师正逐渐形成模型即服务AI热潮。 什么是模型即服务呢?顾名思义模型即服务(MaaS)是一种用较少的代码、以较快的速度来调用AI功能以及部署AI服务的平台。通过少量代码或不用代码实现AI场景应用创新。 什么是人工智能呢?虽老生常谈,但本着空杯的心态,这里亦引用了百度百科上的定义:“人工智能(Artificial Intelligence),英文缩写为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('''                                            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 = [['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.)复制粘贴如下红框相应服务链接进行分享使用 四、讨论        回首过往,作为2007级本科生,当时是大类招生的,到了2008年大二的时候就需要选方向了。当时大二选方向时,有很多热门的专业,如“自动化”、“信息与通信”等,“计算机”在当时不可不谓是冷门的存在。由于当时大一参加了软件协会(后孵化了ACM集训队),迷上了程序设计语言的创造力,所以选择了计算机专业。        转眼进入了专业课的学习,当时上了一门人工智能课程,里面讲到了“手写数字识别”这样BP神经网络案例,当即被人工智能的创造力吸引到了。在当时实现手写数字识别还需要用较重的MFC应用搭建方式,如果不太熟悉的话,应用入门成本还是比较高的。现如今结合模型即服务AI热潮,相应应用搭建将会越来越方便,大把的精力可以集中放到AI技术与应用的普及和相应创新上了。        同学们如有想要简单快速实现的其他AI应用也可在评论区留言,作者将择机对需求呼声较高的AI应用做模型即服务实现分享。  五、应用         接下来给大家介绍下我们平台上展示的功能,欢迎大家体验。 磊宇云—视觉智能开放平台 磊宇云—视觉智能开放平台—人脸人体 基于图像或视频中的人脸检测、分析/比对技术,以及人体检测技术,提供人脸/人体的检测定位、人脸属性识别和人脸比对等独立模块。可以为开发者和企业提供高性能的在线API服务,应用于人脸AR、生物识别和认证、大规模人脸检索、照片管理等各种场景。 磊宇云—视觉智能开放平台—视频生产: 基于磊宇云计算机视觉与深度学习技术,提供视频内容的编辑、生成、增强与摘要等能力。视频生产可广泛应用于互联网媒体、短视频、娱乐直播、在线教育、广电传媒等行业应用。

/template/Home/leiyu/PC/Static