Step By Step
1、服務開通,參考鏈接:阿里雲視覺智能開放平臺使用簡明教程
2、目前提供圖像搜索相關的7個API能力;
3、操作流程
4、Code Sample
- 4.1 pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
- 4.2 Java Code
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
public class DemoSearch {
static IAcsClient client = null;
public static void main(String[] args) {
// ccess Key ID、Access Key Secret 獲取參考:https://yq.aliyun.com/articles/693979
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai", //默認
"LTAI9r2Z********", //您的Access Key ID
"W4SPz7p4McBhhc****************"); //您的Access Key Secret
client = new DefaultAcsClient(profile);
String dbName = "searchdemo";
// createImageDb(client,dbName);
// deleteImageDb(client, dbName);
// deleteImage(client, dbName, "1");
// deleteImage(client, dbName, "2");
// deleteImage(client, dbName, "3");
// listImageDbs(client);
String imageUrl1 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/searchpictest/changcheng.jpg";
String imageUrl2 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/searchpictest/erfeiertieta.jpg";
String imageUrl3 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/searchpictest/jingtian2.jpeg";
// addImage(client,dbName,"1","圖搜圖片1",imageUrl1);
// addImage(client,dbName,"2","圖搜圖片2",imageUrl2);
// addImage(client,dbName,"3","圖搜圖片3",imageUrl3);
// searchImage(client,imageUrl1,2,dbName);
listImages(client,dbName);
}
/**
* 創建圖片數據庫
* @param client client
* @param name 數據庫名稱
*/
public static void createImageDb(IAcsClient client, String name)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysAction("CreateImageDb");
commonRequest.putBodyParameter("Name",name);
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
commonRequest.setSysVersion("2020-03-20");
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("創建圖片數據庫:");
System.out.println(response.getData());
}
/**
* 查看數據庫的列表
* @param client
*/
public static void listImageDbs(IAcsClient client)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysVersion("2020-03-20");
commonRequest.setSysAction("ListImageDbs");
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("查看數據庫的列表:");
System.out.println(response.getData());
}
/**
* 為指定數據庫添加圖片數據
* @param client
* @param dbName 數據庫名稱
* @param entityId 實體ID,可以作為數據分組的ID
* @param extraData 自定義數據
* @param imageUrl 圖片地址,必須是同Region的OSS圖片地址
*/
public static void addImage(IAcsClient client, String dbName, String entityId, String extraData, String imageUrl)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysVersion("2020-03-20");
commonRequest.setSysAction("AddImage");
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
commonRequest.putBodyParameter("DbName", dbName);
commonRequest.putBodyParameter("EntityId", entityId);
commonRequest.putBodyParameter("ExtraData", extraData);
commonRequest.putBodyParameter("ImageUrl", imageUrl);
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("為指定數據庫添加圖片數據:");
System.out.println(response.getData());
}
/**
* 數據庫中搜索相似的圖片
* @param client
* @param imageUrl 圖片地址,必須是同Region的OSS的圖片地址
* @param limit 獲取結果數量上限,取值範圍1~1000
* @param dbName 數據庫名稱
*/
public static void searchImage(IAcsClient client, String imageUrl, Integer limit, String dbName)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysVersion("2020-03-20");
commonRequest.setSysAction("SearchImage");
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
commonRequest.putBodyParameter("Limit", limit);
commonRequest.putBodyParameter("ImageUrl", imageUrl);
commonRequest.putBodyParameter("DbName", dbName);
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("搜索圖片:");
System.out.println(response.getData());
}
/**
* 刪除指定數據庫
* @param client
* @param dbName 數據庫名稱
*/
public static void deleteImageDb(IAcsClient client, String dbName)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysVersion("2020-03-20");
commonRequest.setSysAction("DeleteImageDb");
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
commonRequest.putBodyParameter("Name", dbName);
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("刪除指定數據庫:");
System.out.println(response.getData());
}
/**
* 刪除指定數據庫中的圖片
* @param client
* @param dbName 數據庫名稱
* @param entityId 待刪除數據的實體ID
*/
public static void deleteImage(IAcsClient client, String dbName, String entityId)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysVersion("2020-03-20");
commonRequest.setSysAction("DeleteImage");
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
commonRequest.putBodyParameter("DbName", dbName);
commonRequest.putBodyParameter("EntityId", entityId);
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("刪除指定數據庫中的圖片:");
System.out.println(response.getData());
}
/**
* 查看指定數據庫中的圖片數據列表
* @param client
* @param dbName 數據庫名稱
* 更多參數的支持可以參考API說明:https://help.aliyun.com/document_detail/159128.html?spm=a2c4g.11186623.6.715.2c697c1edNt7uW 通過方法重載實現
*/
public static void listImages(IAcsClient client, String dbName)
{
CommonRequest commonRequest = new CommonRequest();
commonRequest.setSysVersion("2020-03-20");
commonRequest.setSysAction("ListImages");
commonRequest.setSysMethod(MethodType.POST);
commonRequest.setSysDomain("imgsearch.cn-shanghai.aliyuncs.com");
commonRequest.putBodyParameter("DbName", dbName);
CommonResponse response = null;
try {
response = client.getCommonResponse(commonRequest);
} catch (ClientException e) {
e.printStackTrace();
}
System.out.println("查看指定數據庫中的圖片數據列表:");
System.out.println(response.getData());
}
}