前回はBaiduの人体セグメンテーションインターフェースを利用しました: Baidu Smart Cloud ポートレートセグメンテーション事例教示: バックエンドJavaでIDカードのポートレートセグメンテーションと背景色を実現_Seasons Gradually Reborn C's Blog - CSDN Blog 今回はAlibaba Cloudの人体セグメンテーションを利用します。
2 つを比較してください:
①通常の証明写真の場合、両者の効果は基本的に同じです。複雑な画像の場合は、Baidu の方がわずかに優れています。ただし、一般的なポートレートセグメンテーションは証明写真にも使用されます。
② トライアルユーザーの場合、Alibaba Cloud 2dps は無料、Baidu の通話は 10,000 回無料です。個人的には、Alibaba Cloudの呼び出しコードは比較的シンプルで、パラメータの取得や処理も簡単になると思います。
エフェクト画像は以下の通り、Baidu Smart Cloudのインターフェースと比較すると、影が分割されていないことが一目瞭然です。
オープンリンクのドキュメント、インターフェイスなどは公式 Web サイトで照会できます
機能デモンストレーション - Alibaba Cloud Visual Intelligence オープン プラットフォーム
サンプルコード
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());
}
}
}