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.
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</code><code>from modelscope.utils.constant import Tasks</code><code>mog_face_detection_func = pipeline(Tasks.face_detection, 'damo/cv_resnet101_face-detection_cvpr22papermogface')</code><code>src_img_path = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/mog_face_detection.jpg'</code><code>raw_result = mog_face_detection_func(src_img_path)</code><code>print('face detection output: {}.'.format(raw_result))</code><code># visualize the result</code><code>from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result</code><code>from modelscope.preprocessors.image import LoadImage</code><code>import cv2, numpy as np</code><code>src_img = LoadImage.convert_to_ndarray(src_img_path)</code><code>src_img = cv2.cvtColor(np.asarray(src_img), cv2.COLOR_RGB2BGR)</code><code>cv2.imwrite('src_img.jpg', src_img)</code><code>dst_img = draw_face_detection_no_lm_result('src_img.jpg', raw_result)</code><code>cv2.imwrite('dst_img.jpg', dst_img)</code><code>import matplotlib.pyplot as plt</code><code>dst_img = cv2.cvtColor(np.asarray(dst_img), cv2.COLOR_BGR2RGB)</code><code>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</code><code>from modelscope.pipelines import pipeline</code><code>from modelscope.utils.constant import Tasks</code><code>from modelscope.utils.cv.image_utils import draw_face_detection_no_lm_result</code><code>from modelscope.preprocessors.image import LoadImage</code><code>from PIL import Image</code><code>import cv2, numpy as np</code><code>def inference(input_file):</code><code> mog_face_detection_func = pipeline(Tasks.face_detection, 'damo/cv_resnet101_face-detection_cvpr22papermogface')</code><code> src_img_path = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/mog_face_detection.jpg'</code><code> raw_result = mog_face_detection_func(src_img_path)</code><code> src_img = LoadImage.convert_to_ndarray(src_img_path)</code><code> src_img = cv2.cvtColor(np.asarray(src_img), cv2.COLOR_RGB2BGR)</code><code> cv2.imwrite('src_img.jpg', src_img)</code><code> dst_img = draw_face_detection_no_lm_result('src_img.jpg', raw_result)</code><code> dst_img = cv2.cvtColor(np.asarray(dst_img), cv2.COLOR_BGR2RGB)</code><code> return dst_img</code><code>css_style = "#fixed_size_img {height: 240px;} #overview {margin: auto;max-width: 600px; max-height: 400px;}"</code><code>title = "AI人脸检测应用"</code><code>with gr.Blocks(title=title, css=css_style) as demo:</code><code> gr.HTML('''<div style="text-align: center; max-width: 720px; margin: 0 auto;"><h1 style="font-family: PingFangSC; font-weight: 500; font-size: 32px; margin-bottom: 7px;">AI人脸检测应用</h1></div>''')</code><code> with gr.Row():</code><code> img_input = gr.Image(type="pil", elem_id="fixed_size_img")</code><code> img_output = gr.Image(type="pil", elem_id="fixed_size_img")</code><code> with gr.Row():</code><code> btn_submit = gr.Button(value="一键生成", elem_id="blue_btn")</code><code> examples = [["https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/mog_face_detection.jpg"]]</code><code> examples = gr.Examples(examples=examples, inputs=img_input, outputs=img_output, label="点击如下示例试玩", run_on_click=True)</code><code> btn_submit.click(inference, inputs=[img_input], outputs=img_output)</code><code>if __name__ == "__main__":</code><code> 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
