本文介绍图像搜索服务Java SDK的使用方法及示例。
接口列表
接口名称 | 接口说明 |
---|---|
Add | 增加图片。 |
SearchImageByPic | 使用图片进行搜索。 |
SearchImageByName | 指定名称,使用已入库的图片进行搜索。 |
Delete | 删除图片。 |
SDK使用示例
准备工作
- 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见创建AccessKey。
- 将SDK添加到项目中
引入ImageSearch SDK依赖,通过Maven二方库依赖的方式将ImageSearch的SDK加入到自己的项目中。
<dependency> <groupId>com.aliyun</groupId> <artifactId>imagesearch20201214</artifactId> <version>3.1.1</version> </dependency>
Add接口
- 代码示例
import com.aliyun.imagesearch20201214.Client; import com.aliyun.imagesearch20201214.models.AddImageAdvanceRequest; import com.aliyun.imagesearch20201214.models.AddImageResponse; import com.aliyun.teaopenapi.models.Config; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; public class Add { public static void main(String[] args) throws Exception { Config authConfig = new Config(); authConfig.accessKeyId = "XXXXXX"; authConfig.accessKeySecret = "XXXXXXXXX"; authConfig.type = "access_key"; authConfig.endpoint = "imagesearch.cn-shanghai.aliyuncs.com"; authConfig.regionId = "cn-shanghai"; Client client = new Client(authConfig); AddImageAdvanceRequest request = new AddImageAdvanceRequest(); // 必填,图像搜索实例名称。 request.instanceName = "XXXXXXXXXXX"; // 必填,商品id,最多支持 512个字符。 // 一个商品可有多张图片。 request.productId = "test"; // 必填,图片名称,最多支持 512个字符。 // 1. ProductId + PicName唯一确定一张图片。 // 2. 如果多次添加图片具有相同的ProductId + PicName,以最后一次添加为准,前面添加的图片将被覆盖。 request.picName = "1000"; // 选填,图片类目。 // 1. 对于商品搜索:若设置类目,则以设置的为准;若不设置类目,将由系统进行类目预测,预测的类目结果可在Response中获取 。 // 2. 对于通用搜索:不论是否设置类目,系统会将类目设置为88888888。 request.categoryId = 3; // 选填,用户自定义的内容,最多支持4096个字符。 // 查询时会返回该字段。例如可添加图片的描述等文本。 request.customContent = "this is a simple test"; // 选填,整数类型属性,可用于查询时过滤,查询时会返回该字段。 // 例如不同的站点的图片/不同用户的图片,可以设置不同的IntAttr,查询时通过过滤来达到隔离的目的 request.intAttr = 56; // 选填,字符串类型属性,最多支持 128个字符。可用于查询时过滤,查询时会返回该字段。 request.strAttr = "test"; // 选填,是否需要进行主体识别,默认为true。 // 1.为true时,由系统进行主体识别,以识别的主体进行搜索,主体识别结果可在Response中获取。 // 2. 为false时,则不进行主体识别,以整张图进行搜索。 request.crop = true; // 选填,图片的主体区域,格式为 x1,x2,y1,y2, 其中 x1,y1 是左上角的点,x2,y2是右下角的点。设置的region 区域不要超过图片的边界。 // 若用户设置了Region,则不论Crop参数为何值,都将以用户输入Region进行搜索。 request.region = "167,467,221,407"; RuntimeOptions runtimeOptions = new RuntimeOptions(); String picName = "D:/123.jpg"; InputStream inputStream = new FileInputStream(picName); // 图片内容,最多支持 4MB大小图片以及5s的传输等待时间。当前仅支持PNG、JPG、JPEG、BMP、GIF、WEBP、TIFF、PPM格式图片; // 对于商品、商标、通用图片搜索,图片长和宽的像素必须都大于等于100且小于等于4096; // 对于布料搜索,图片长和宽的像素必须都大于等于448且小于等于4096; // 图像中不能带有旋转信息 request.picContentObject = inputStream; try { AddImageResponse response = client.addImageAdvance(request,runtimeOptions); System.out.println("success: " + response.getBody().success + ". message: " + response.getBody().message + ". categoryId: " + response.getBody().picInfo.categoryId + ". region:" + response.getBody().picInfo.region + ". requestId: " + response.getBody().requestId); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }
- 结果示例
success: true. message: success. categoryId: 9. region:383,681,291,549. requestId: 016D63E3-D0C5-42D3-8879-55E1A705FBA4
SearchImageByPic接口
- 代码示例
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.SearchImageByPicAdvanceRequest; import com.aliyun.imagesearch20201214.models.SearchImageByPicResponse; import com.aliyun.tea.TeaException; import com.aliyun.teautil.models.RuntimeOptions; import java.io.FileInputStream; import java.io.InputStream; import java.util.List; public class SearchImageByPic { public static void main(String[] args) throws Exception { Config authConfig = new Config(); authConfig.accessKeyId = "XXXXXXXX"; authConfig.accessKeySecret = "XXXXXXXXXX"; authConfig.type = "access_key"; authConfig.endpoint = "imagesearch.cn-shanghai.aliyuncs.com"; authConfig.regionId = "cn-shanghai"; Client client = new Client(authConfig); SearchImageByPicAdvanceRequest request = new SearchImageByPicAdvanceRequest(); // 必填,图像搜索实例名称。 request.instanceName = "XXXXXXXXX"; // 选填,商品类目。 // 1. 对于商品搜索:若设置类目,则以设置的为准;若不设置类目,将由系统进行类目预测,预测的类目结果可在Response中获取 。 // 2. 对于通用搜索:不论是否设置类目,系统会将类目设置为88888888。 request.categoryId = 3; // 选填,返回结果的数目。取值范围:1-100。默认值:10。 request.num = 10; // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。 request.start = 0; // 选填,是否需要进行主体识别,默认为true。 // 1.为true时,由系统进行主体识别,以识别的主体进行搜索,主体识别结果可在Response中获取。 // 2. 为false时,则不进行主体识别,以整张图进行搜索。 request.crop = true; // 选填,图片的主体区域,格式为 x1,x2,y1,y2, 其中 x1,y1 是左上角的点,x2,y2是右下角的点。设置的region 区域不要超过图片的边界。 // 若用户设置了Region,则不论Crop参数为何值,都将以用户输入Region进行搜索。 request.region="167,467,221,407"; // 选填,过滤条件 // int_attr支持的操作符有>、>=、<、<=、=,str_attr支持的操作符有=和!=,多个条件之支持AND和OR进行连接。 // 示例: // 1. 根据IntAttr过滤结果,int_attr>=100 // 2. 根据StrAttr过滤结果,str_attr!="value1" // 3. 根据IntAttr和StrAttr联合过滤结果,int_attr=1000 AND str_attr="value1" request.filter="int_attr=56 AND str_attr=\"test\""; InputStream inputStream = new FileInputStream("D:/123.jpg"); // 图片内容,最多支持 4MB大小图片以及5s的传输等待时间。当前仅支持PNG、JPG、JPEG、BMP、GIF、WEBP、TIFF、PPM格式图片; // 对于商品、商标、通用图片搜索,图片长和宽的像素必须都大于等于100且小于等于4096; // 对于布料搜索,图片长和宽的像素必须都大于等于448且小于等于4096; // 图像中不能带有旋转信息 request.picContentObject = inputStream; request.num = 10; request.start = 0; RuntimeOptions runtimeObject = new RuntimeOptions(); try { SearchImageByPicResponse response = client.searchImageByPicAdvance(request, runtimeObject); System.out.println(response.getBody().requestId); System.out.println(response.getBody().picInfo.categoryId); System.out.println(response.getBody().picInfo.region); List<SearchImageByPicResponseBody.SearchImageByPicResponseBodyAuctions> auctions = response.getBody().getAuctions(); for(SearchImageByPicResponseBody.SearchImageByPicResponseBodyAuctions auction:auctions) { System.out.println(auction.categoryId + " " + auction.picName + " " + auction.productId + " " + auction.customContent + " " + auction.score + " " + auction.strAttr + " " + auction.intAttr); } System.out.println("多主体信息"); SearchImageByPicResponseBody.SearchImageByPicResponseBodyPicInfo picInfo = response.getBody().getPicInfo(); for (SearchImageByPicResponseBody.SearchImageByPicResponseBodyPicInfoMultiRegion multiRegion : picInfo.getMultiRegion()) { System.out.println(multiRegion.region); } } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }
- 结果示例
09BE019A-AE3D-4D22-99C9-10533D8AC631 3 167,467,221,407 3 1000 test this is a simple test 1.0 test 56 多主体信息 112,440,76,387
SearchImageByName接口
- 代码示例
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.SearchImageByNameRequest; import com.aliyun.imagesearch20201214.models.SearchImageByNameResponse; import com.aliyun.tea.TeaException; import java.util.List; public class SearchImageByName{ public static void main(String[] args) throws Exception { //配置 Config authConfig = new Config(); authConfig.accessKeyId = "XXXXXX"; authConfig.accessKeySecret = "XXXXXXXXX"; authConfig.type = "access_key"; authConfig.endpoint = "imagesearch.cn-shanghai.aliyuncs.com"; authConfig.regionId = "cn-shanghai"; Client client = new Client(authConfig); SearchImageByNameRequest request = new SearchImageByNameRequest(); // 必填,图像搜索实例名称。 request.instanceName = "XXXXXXXXX"; // 必填,商品id,最多支持 512个字符。 // 一个商品可有多张图片。 request.productId = "test"; // 必填,图片名称,最多支持 512个字符。 // 1. ProductId + PicName唯一确定一张图片。 request.picName = "1000"; // 选填,商品类目。 // 1. 对于商品搜索:若设置类目,则以设置的为准;若不设置类目,将由系统进行类目预测,预测的类目结果可在Response中获取 。 // 2. 对于通用搜索:不论是否设置类目,系统会将类目设置为88888888。 request.categoryId = 3; // 选填,返回结果的数目。取值范围:1-100。默认值:10。 request.num =10; // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。 request.start =0; // 选填,过滤条件 // int_attr支持的操作符有>、>=、<、<=、=,str_attr支持的操作符有=和!=,多个条件之支持AND和OR进行连接。 // 示例: // 1. 根据IntAttr过滤结果,int_attr>=100 // 2. 根据StrAttr过滤结果,str_attr!="value1" // 3. 根据IntAttr和StrAttr联合过滤结果,int_attr=1000 AND str_attr="value1" request.filter = "int_attr=56 AND str_attr =\"test\""; try { SearchImageByNameResponse response = client.searchImageByName(request); System.out.println(response.getBody().requestId); System.out.println(response.getBody().picInfo.categoryId); System.out.println(response.getBody().picInfo.region); List<SearchImageByNameResponseBody.SearchImageByNameResponseBodyAuctions> auctions = response.getBody().getAuctions(); for(SearchImageByNameResponseBody.SearchImageByNameResponseBodyAuctions auction:auctions) { System.out.println(auction.categoryId + " " + auction.picName + " " + auction.productId + " " + auction.customContent + " " + auction.score + " " + auction.strAttr + " " + auction.intAttr); } } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }
- 结果示例
8F2441C8-EA05-461B-A4AC-5F5DE25FAC21 3 null 3 1000 test this is a simple test 1.0 test 56
Delete接口
- 代码示例
import com.aliyun.imagesearch20201214.Client; import com.aliyun.teaopenapi.models.Config; import com.aliyun.imagesearch20201214.models.DeleteImageRequest; import com.aliyun.imagesearch20201214.models.DeleteImageResponse; import com.aliyun.tea.TeaException; public class Delete { public static void main(String[] args) throws Exception { Config authConfig = new Config(); authConfig.accessKeyId = "XXXXXXXX"; authConfig.accessKeySecret = "XXXXXXXXXX"; authConfig.type = "access_key"; authConfig.endpoint = "imagesearch.cn-shanghai.aliyuncs.com"; authConfig.regionId = "cn-shanghai"; Client client = new Client(authConfig); DeleteImageRequest request = new DeleteImageRequest(); // 必填,图像搜索实例名称。 request.instanceName = "XXXXXXXXXXXX"; // 必填,商品id,最多支持 512个字符。 // 一个商品可有多张图片。 request.productId = "test"; // 选填,图片名称。若不指定本参数,则删除ProductId下所有图片;若指定本参数,则删除ProductId+PicName指定的图片。 request.picName = "1000"; try { DeleteImageResponse response = client.deleteImage(request); System.out.println("requestId: " + response.requestId + ". success: " + response.success + ". message: " + response.message); } catch (TeaException e) { System.out.println(e.getCode()); System.out.println(e.getData()); System.out.println(e.getMessage()); e.printStackTrace(); } } }
- 结果示例
requestId: 927AE217-90E1-4B48-9413-7508FCB4CE51. success: true. message: success