### [百度一键人像分割 API 试用](https://pmself.com/article/1200) **Published:** 2022-03-07T11:14:33 **Author:** pmself **Excerpt:** 近期百度上线了人像抠图 API - 人像分割 - 识别图像中的人体轮廓,与背景进行分离,其主要分析能力为: 识… 近期百度上线了人像抠图 API – [人像分割 – 识别图像中的人体轮廓,与背景进行分离](http://ai.baidu.com/tech/body/seg),其主要分析能力为: > 识别人体的轮廓范围,与背景进行分离,适用于拍照背景替换、照片合成、身体特效等场景; > > 输入正常人像图片,返回分割后的二值结果图,返回的二值图像需要进行二次处理才可查看分割效果. 技术和应用场景简单介绍参考 – [人像分割技术正式上线 一键抠像 欢迎免费使用](http://ai.baidu.com/forum/topic/show/892416). ## 1\. 百度人像分割技术简介 **核心能力** 精准识别图像中的人体轮廓边界,将人体轮廓与图像背景进行分离,实现像素级分割,一键抠像. **技术优势** - 适应性强:图像中有多个人体,或者背景比较复杂时,都不在话下,还能hold住人体位置重叠、遮挡、背面、侧面、大动作等各类场景. - 精准分割:人体轮廓边缘清晰可见,评测IoU 95%以上. ![](https://pmself.com/wp-content/uploads/2022/03/5ce8e00f25e02.jpg) 分割效果图 **应用场景** - **人像抠图** 将原始图片中的人像分离出来,选择新的背景图像进行替换、合成;同时可以对背景进行虚化处理,突出人像,实现大光圈人像拍照效果。手残党也能变成修图小能手~ - **人体特效** 视频直播过程中,识别用户的人体轮廓,为人像实时增加各种设定的背景特效、贴纸道具,拉轰、上档次,妥妥的! 示例: ![](https://pmself.com/wp-content/uploads/2022/03/5ce8e00f6bd4b.jpg) 图片去背景 ![](https://pmself.com/wp-content/uploads/2022/03/5ce8e00fae088.jpg) 照片背景替换 **典型案例** – 美啦相机APP 美啦相机是广州光锥元信息科技有限公司推出的美颜美体应用软件,为女性提供全能的修图服务. 今年8月接入百度人像分割技术,精确分离照片中的人像和背景,为用户提供换背景、给背景添加滤镜、景深模式等定制化个性服务. 一秒就变小仙女~ ![](https://pmself.com/wp-content/uploads/2022/03/5ce8e0102457f.jpg) **如何接入** 人像分割服务已正式上线,欢迎免费试用,请戳这里:[http://ai.baidu.com/tech/body/seg](http://ai.baidu.com/tech/body/seg) ![](https://pmself.com/wp-content/uploads/2022/03/5ce8e010763a5.jpg) ## 2\. Python 调用百度 API 测试 Demo 需要首先安装 `baidu-aip-2.2.8.0` 包 ``` sudo pip2 install baidu-aip # 或 sudo pip3 install baidu-aip ``` 百度 API 测试代码如下: ``` import cv2 import base64 import numpy as np from aip import AipBodyAnalysis # 在百度云中申请,每天各接口有 500 次调用限制. APP_ID = '****' API_KEY = '****' SECRET_KEY = '****' client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) imgfile = 'test.jpg' ori_img = cv2.imread(imgfile) height, width, _ = ori_img.shape with open(imgfile, 'rb') as fp: img_info = fp.read() seg_res = client.bodySeg(img_info) labelmap = base64.b64decode(seg_res['labelmap']) nparr = np.fromstring(labelmap, np.uint8) labelimg = cv2.imdecode(nparr,1) labelimg = cv2.resize(labelimg,(width,height), interpolation=cv2.INTER_NEAREST) new_img = np.where(labelimg==1, 255, labelimg) maskfile = imgfile.replace('.jpg', '_mask.png') cv2.imwrite(maskfile, new_img) res_imgfile = imgfile.replace('.jpg', '_res.jpg') result = cv2.bitwise_and(ori_img, new_img) cv2.imwrite(res_imgfile, result) print('Done.') ``` 随便从淘宝上找了几张模特图,测试结构如下: ![](https://pmself.com/wp-content/uploads/2022/03/5ce8e0115ab38.jpg) **总结:** 根据 API 输出结果解析出来的 shape 为 473x473x3,应该采用了类似于 PSPNet 的语义分割网络做的二类分割背景人体背景+人体. 测试结果感觉差强人意. **Tags:** 人像分割 **Categories:** 技术 ---