Artificial Intelligence 11 min read

Model-as-a-Service AI Face Detection and Gradio Application Development

This article explains how to use ModelScope's Model-as-a-Service for face detection, demonstrates the required Python code, and shows how to build and share a Gradio‑based AI application, including a more complex "Time Album" use case.

DataFunTalk
DataFunTalk
DataFunTalk
Model-as-a-Service AI Face Detection and Gradio Application Development

Materials

1. Face detection model: ModelScope face‑detection model

2. Time Album demo: Face album studio

Background

Model‑as‑a‑Service (MaaS) combines the rapid deployment of AI capabilities with minimal code, while Artificial Intelligence (AI) refers to technologies that simulate or extend human intelligence, covering areas such as computer vision, natural language processing, and robotics.

Method

1. Model‑as‑a‑Service AI function call

Open a ModelScope notebook, create an account, or install the Python SDK locally, then run the following example code to perform face detection:

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))
# visualize the result
from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result
from modelscope.preprocessors.image import LoadImage
import cv2, numpy as np
src_img = LoadImage.convert_to_ndarray(src_img_path)
src_img = cv2.cvtColor(np.asarray(src_img), cv2.COLOR_RGB2BGR)
cv2.imwrite('src_img.jpg', src_img)
dst_img = draw_face_detection_no_lm_result('src_img.jpg', raw_result)
cv2.imwrite('dst_img.jpg', dst_img)
import matplotlib.pyplot as plt
dst_img = cv2.cvtColor(np.asarray(dst_img), cv2.COLOR_BGR2RGB)
plt.imshow(dst_img)

This completes the MaaS AI function call and can be reused in any face‑detection scenario.

2. Model‑as‑a‑Service AI application building with Gradio

After the function works, create a Gradio demo to expose it as a web service. Install Gradio according to the quick‑start guide , then create app.py with the following code:

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, numpy as np
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)
src_img = LoadImage.convert_to_ndarray(src_img_path)
src_img = cv2.cvtColor(np.asarray(src_img), cv2.COLOR_RGB2BGR)
cv2.imwrite('src_img.jpg', src_img)
dst_img = draw_face_detection_no_lm_result('src_img.jpg', raw_result)
dst_img = cv2.cvtColor(np.asarray(dst_img), cv2.COLOR_BGR2RGB)
return dst_img
css_style = "#fixed_size_img {height: 240px;} #overview {margin: auto;max-width: 600px; max-height: 400px;}"
title = "AI人脸检测应用"
with gr.Blocks(title=title, css=css_style) as demo:
gr.HTML('''
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)
if __name__ == "__main__":
demo.launch(share=True)

Run python app.py to start the service, then share the generated link.

3. Complex application – "Time Album"

Building on the face‑detection demo, you can create a "Time Album" that aligns faces across a photo collection and generates age‑progression effects. The full code is available at the ModelScope studio . Execute python app.py and open the provided URL to try the demo.

Discussion

The author reflects on personal study of AI since 2007, noting how Model‑as‑a‑Service now lowers the barrier for building AI applications, and invites readers to suggest other quick AI projects.

Thank you for reading.

computer visionPythonAIface detectionModel-as-a-ServiceGradio
DataFunTalk
Written by

DataFunTalk

Dedicated to sharing and discussing big data and AI technology applications, aiming to empower a million data scientists. Regularly hosts live tech talks and curates articles on big data, recommendation/search algorithms, advertising algorithms, NLP, intelligent risk control, autonomous driving, and machine learning/deep learning.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.