Práctica de segmentación del cuerpo humano en la nube de Alibaba

La última vez, utilicé la interfaz de segmentación del cuerpo humano de Baidu: Enseñanza de casos de segmentación de retratos en la nube inteligente de Baidu: Back-end Java realiza segmentación de retratos de tarjetas de identificación y color de fondo_Temporadas Renacen gradualmente Blog de C-CSDN Blog Esta vez usaré la segmentación del cuerpo humano de Alibaba Cloud.

Compara los dos:

① Para fotos de identificación normales, los efectos de los dos son básicamente los mismos. Para imágenes complejas, Baidu es un poco mejor. Sin embargo, la segmentación general de retratos también se usa para fotos de identificación.

② Para usuarios de prueba, Alibaba Cloud 2dps es gratis y Baidu llama 10,000 veces gratis. Personalmente, el código de llamada de Alibaba Cloud es relativamente simple, y la adquisición y el procesamiento de parámetros serán más simples.

La imagen del efecto es la siguiente: en comparación con la interfaz de Baidu Smart Cloud, es obvio que la sombra no se ha dividido.

Los documentos de enlace abierto, interfaces, etc. se pueden consultar en el sitio web oficial

Demostración de capacidad: plataforma abierta de inteligencia visual en la nube de Alibaba

Código de muestra

dependencias de importación 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());
        }
    }
}

Supongo que te gusta

Origin blog.csdn.net/qq_70143756/article/details/129518504
Recomendado
Clasificación