阿里云人体分割实践

上回使用了百度的人体分割接口:百度智能云人像分割案例教学:后端java实现身份证人像分割、换底色_季节渐次重生C的博客-CSDN博客这回使用一下阿里云的人体分割。

对比二者:

①对于正常的证件照,二者效果基本一致。对于复杂图片,百度略胜一筹。不过一般人像分割也都是用于证件照的。

②对于试用者来说,阿里云2dps是免费的,百度是免费调用10000次。对我个人而言,阿里云调用代码比较简单,参数获取和处理也会简单点。

效果图如下,对比百度智能云的接口,很明显影子没有被分割掉。

开通链接 文档、接口等都可以在官方网站查询

能力展示-阿里云视觉智能开放平台

示例代码

pom.xml导入依赖

<!-- https://mvnrepository.com/artifact/com.aliyun/imageseg20191230 -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>imageseg20191230</artifactId>
            <version>1.0.5</version>
        </dependency>
import com.aliyun.imageseg20191230.models.SegmentBodyResponse;
import com.aliyun.tea.TeaException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class SegmentBody {
    public static com.aliyun.imageseg20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "imageseg.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageseg20191230.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        // "YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET" 的生成请参考https://help.aliyun.com/document_detail/175144.html
        // 如果您是用的子账号AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
        com.aliyun.imageseg20191230.Client client = SegmentBody.createClient("YOUR_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_SECRET");
        // 场景一,使用本地文件
         InputStream inputStream = new FileInputStream(new File("d:/AAfile/t1.jpg"));
        // 场景二,使用任意可访问的url
//        URL url = new URL("d:/AAfile/t1.jpg");
//        InputStream inputStream = url.openConnection().getInputStream();
        com.aliyun.imageseg20191230.models.SegmentBodyAdvanceRequest segmentBodyAdvanceRequest = new com.aliyun.imageseg20191230.models.SegmentBodyAdvanceRequest()
                .setImageURLObject(inputStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            SegmentBodyResponse segmentBodyResponse = client.segmentBodyAdvance(segmentBodyAdvanceRequest, runtime);
            /**  获取url文件到本地 */
            URL url = new URL(segmentBodyResponse.getBody().getData().getImageURL());
            inputStream = url.openStream();

            FileOutputStream outputStream = new FileOutputStream("d:/AAfile/image.jpg");

            byte[] buffer = new byte[2048];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }

            // Close streams
            inputStream.close();
            outputStream.close();

            System.out.println("Image downloaded successfully.");
        } catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_70143756/article/details/129518504