文档

交互式涂抹分割

交互式涂抹分割能力支持用户通过鼠标涂抹的方式选择目标元素,算法模型通过涂抹区域的位置和大小,自动对涂抹的目标进行分割。本文为您介绍交互式涂抹分割常用语言的示例代码。

说明
  • 您可以进入在线咨询获取在线人工帮助。

  • 阿里云视觉智能开放平台视觉AI能力API接入、接口使用或问题咨询等,请通过钉钉群(23109592)加入阿里云视觉智能开放平台咨询群联系我们。

能力介绍

关于交互式涂抹分割的功能介绍以及具体调用参数说明,请参见交互式涂抹分割

SDK包安装

常见语言的SDK依赖包信息,请参见SDK总览

配置环境变量

配置环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET

重要
  • 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维,具体操作,请参见创建RAM用户

  • 请不要将AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。

  • Linux和macOS系统配置方法

    1. 在IntelliJ IDEA中打开终端Terminal。

    2. 执行以下命令,配置环境变量。

      <access_key_id>需替换为您RAM用户的AccessKey ID,<access_key_secret>替换为您RAM用户的AccessKey Secret。如果后续需要进行更多权限相关的配置,具体操作请参见使用RAM Policy控制访问权限

      export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> 
      export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • Windows系统配置方法

    新建环境变量文件,添加环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET,并写入已准备好的AccessKey ID和AccessKey Secret。然后重启Windows系统。本操作以Windows 10为例进行说明。

    1. 打开文件资源管理器,在此电脑上右键单击属性。

    2. 在右侧导航栏,单击高级系统配置

    3. 系统属性对话框的高级页签下,单击环境变量

    4. 环境变量对话框中,单击新建(W)image.png

    5. 在弹出的新建系统变量对话框中,添加环境变量ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET,并写入已准备好的AccessKey ID和AccessKey Secret。

    6. 重启Windows系统,使配置生效。

示例代码

文件在同地域OSS

/*
引入依赖包
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aigen20240111</artifactId>
  <version>1.0.0</version>
</dependency>
*/

import com.aliyun.aigen20240111.models.InteractiveScribbleSegmentationResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;

public class InteractiveScribbleSegmentation {
    public static com.aliyun.aigen20240111.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "aigen.cn-shanghai.aliyuncs.com";
        return new com.aliyun.aigen20240111.Client(config);
    }
    public static void main(String[] args_) throws Exception {
        // 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
        // 如果您使用的是RAM用户的AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        com.aliyun.aigen20240111.Client client = InteractiveScribbleSegmentation.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        com.aliyun.aigen20240111.models.InteractiveScribbleSegmentationRequest scribbleSegmentationRequest = new com.aliyun.aigen20240111.models.InteractiveScribbleSegmentationRequest()
                .setImageUrl("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/imageseg/SegmentCommonImage/SegmentCommonImage1.jpg")
                .setIntegratedMaskUrl("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/imageseg/SegmentCommonImage/SegmentCommonImage9.jpg");
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            InteractiveScribbleSegmentationResponse interactiveScribbleSegmentationResponse = client.interactiveScribbleSegmentationWithOptions(scribbleSegmentationRequest,runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(interactiveScribbleSegmentationResponse.body)));
        }catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }
}
# -*- coding: utf-8 -*-
# 引入依赖包
# 最低SDK版本要求:aigen20240111的SDK版本需大于等于1.0.0。
# 可以在此仓库地址中引用最新版本SDK:https://pypi.org/project/alibabacloud-aigen20240111/
# pip install alibabacloud_aigen20240111

import os
from alibabacloud_aigen20240111.client import Client
from alibabacloud_aigen20240111.models import InteractiveScribbleSegmentationRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions

config = Config(
    # 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
    # 如果您用的是RAM用户的AccessKey,还需要为RAM用户授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html。
    # 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
    access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
    access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
    # 访问的域名
    endpoint='aigen.cn-shanghai.aliyuncs.com',
    # 访问的域名对应的region
    region_id='cn-shanghai'
)
interactive_scribble_segmentation_request = InteractiveScribbleSegmentationRequest(
    image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/aigen/InteractiveScribbleSegmentation/InteractiveScribbleSegmentation1.png'
)
runtime_option = RuntimeOptions()
try:
    # 初始化Client
    client = Client(config)
    response = client.interactive_scribble_segmentation_with_options(interactive_scribble_segmentation_request, runtime_option)
    # 获取整体结果
    print(response.body)
except Exception as error:
    # 获取整体报错信息
    print(error)
    # 获取单个字段
    print(error.code)

文件在本地或文件不在同一地域OSS

/*
引入依赖包
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aigen20240111</artifactId>
  <version>1.0.0</version>
</dependency>
*/

import com.aliyun.aigen20240111.models.InteractiveScribbleSegmentationResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class InteractiveScribbleSegmentationAdvance {
    public static com.aliyun.aigen20240111.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        /*
          初始化配置对象com.aliyun.teaopenapi.models.Config
          Config对象存放AccessKeyId、AccessKeySecret、endpoint等配置
         */
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "aigen.cn-shanghai.aliyuncs.com";
        return new com.aliyun.aigen20240111.Client(config);
    }
    public static void main(String[] args_) throws Exception {
        // 创建AccessKey ID和AccessKey Secret,请参考https://help.aliyun.com/document_detail/175144.html。
        // 如果您使用的是RAM用户的AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参考https://help.aliyun.com/document_detail/145025.html
        // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
        com.aliyun.aigen20240111.Client client = InteractiveScribbleSegmentationAdvance.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        // 场景一,使用本地文件
        InputStream imageUrlInputStream = new FileInputStream(new File("/tmp/InteractiveScribbleImagUrl.png"));
        InputStream integratedMaskUrlInputStream = new FileInputStream(new File("/tmp/maskurl.png"));
        // 场景二,使用任意可访问的url
        //URL imageUrl = new URL("https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/aigen/InteractiveScribbleSegmentation/InteractiveScribbleSegmentation.png");
        //InputStream imageUrlInputStream = faceUrl.openConnection().getInputStream();
        //URL integratedMaskUrl = new URL("https://viapi-test-bj.oss-cn-beijing.aliyuncs.com/viapi-3.0domepic/aigen/InteractiveScribbleSegmentation/InteractiveScribbleSegmentation.png");
        //InputStream integratedMaskUrlInputStream = templateUrl.openConnection().getInputStream();
        com.aliyun.aigen20240111.models.InteractiveScribbleSegmentationAdvanceRequest advanceRequest = new com.aliyun.aigen20240111.models.InteractiveScribbleSegmentationAdvanceRequest()
                .setImageUrlObject(imageUrlInputStream)
                .setIntegratedMaskUrlObject(integratedMaskUrlInputStream);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            InteractiveScribbleSegmentationResponse interactiveScribbleSegmentationResponse = client.interactiveScribbleSegmentationAdvance(advanceRequest,runtime);
            // 获取整体结果
            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(interactiveScribbleSegmentationResponse.body)));
        }catch (TeaException teaException) {
            // 获取整体报错信息
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段
            System.out.println(teaException.getCode());
        }
    }
}
  • 本页导读 (1)
文档反馈