提交 90f19761 authored 作者: yangqi's avatar yangqi

1

上级 5e63ac25
......@@ -110,6 +110,12 @@
<artifactId>aliyun-java-sdk-viapiutils</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>cn.lefull</groupId>
<artifactId>lefull-lot-common</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
package cn.lefull.api.test;
import cn.lefull.common.annotation.NoAuth;
import cn.lefull.common.sdk.ali.HikvisionService;
import cn.lefull.repository.model.test.Test;
import cn.lefull.service.test.TestService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -14,12 +15,15 @@ public class TestController {
@Autowired
private TestService testService;
@Autowired
private HikvisionService hikvision;
@NoAuth
@GetMapping("/test")
public void test() {
Test t = this.testService.getTestById(3);
t.setUserName("测试修改数据库resource");
this.testService.updateById(t);
HikvisionService.test();
//Test t = this.testService.getTestById(3);
//t.setUserName("测试修改数据库resource");
//this.testService.updateById(t);
}
}
......@@ -12,6 +12,26 @@
<artifactId>lefull-lot-common</artifactId>
<version>${project.build.version}</version>
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>facebody20191230</artifactId>
<version>2.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>viapi-utils</artifactId>
<version>1.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<packaging>jar</packaging>
......
package cn.lefull.common.sdk.ali;
import com.aliyun.com.viapi.FileUtils;
import com.aliyun.facebody20191230.models.*;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyun.tea.TeaException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class HikvisionService {
static String endpoint = "oss-cn-shanghai.aliyuncs.com";
static String accessKeyId = "LTAI5t7ip3G6Wr5mMsC9UUfb";
static String accessKeySecret = "f03aZwjNUQb3rQMZmc4aey3bDWgfLc";
static String bucketName = "lefull-facebody-sh";
public static void test() {
System.out.println("tttttttttttttttttttttttttt");
}
/**
* 使用AK&SK初始化账号Client
* @return Client
* @throws Exception
*/
public static com.aliyun.facebody20191230.Client createClient() {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 您的 AccessKey ID
.setAccessKeyId("LTAI5tAFLtDk3SLdGEzpA3TY")
// 您的 AccessKey Secret
.setAccessKeySecret("1Mfr9POAjZcviHPkdAjhZ47ZYuyIiY");
// 访问的域名
config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
try {
return new com.aliyun.facebody20191230.Client(config);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 创建人脸库
* @link {https://help.aliyun.com/document_detail/159141.html}
*/
public static void createFaceDb(String dbName) throws Exception {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.CreateFaceDbRequest request = new com.aliyun.facebody20191230.models.CreateFaceDbRequest();
request.setName(dbName);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
client.createFaceDbWithOptions(request, runtime);
}
/**
* 查询人脸数据库列表
* @link {https://help.aliyun.com/document_detail/159142.html}
*/
public static List<String> getFaceDbList() throws Exception {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.ListFaceDbsRequest request = new com.aliyun.facebody20191230.models.ListFaceDbsRequest();
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
ListFaceDbsResponse res = client.listFaceDbsWithOptions(request, runtime);
List<ListFaceDbsResponseBody.ListFaceDbsResponseBodyDataDbList> dbList = res.getBody().data.dbList;
if (CollectionUtils.isEmpty(dbList)){
return new ArrayList<>();
}
return dbList.stream()
.map(ListFaceDbsResponseBody.ListFaceDbsResponseBodyDataDbList::getName)
.collect(Collectors.toList());
}
/**
* @link {https://help.aliyun.com/document_detail/161144.html}
*/
public static void addFace(String dbName, String entityId, String labels) throws Exception {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.AddFaceEntityRequest request = new com.aliyun.facebody20191230.models.AddFaceEntityRequest();
request.setDbName(dbName);
request.setEntityId(entityId);
if(!labels.isEmpty()){
request.setLabels(labels);
}
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
client.addFaceEntityWithOptions(request, runtime);
}
/**
* @link {https://help.aliyun.com/document_detail/161147.html}
*/
public static void updateFace(String dbName, String entityId, String labels) throws Exception {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.UpdateFaceEntityRequest request = new com.aliyun.facebody20191230.models.UpdateFaceEntityRequest();
request.setDbName(dbName);
request.setEntityId(entityId);
if(!labels.isEmpty()){
request.setLabels(labels);
}
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
client.updateFaceEntityWithOptions(request, runtime);
}
/**
* @link {https://help.aliyun.com/document_detail/161145.html}
*/
public static Map<String, Object> getFace(String dbName, String entityId) throws Exception {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.GetFaceEntityRequest request = new com.aliyun.facebody20191230.models.GetFaceEntityRequest();
request.setDbName(dbName);
request.setEntityId(entityId);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
GetFaceEntityResponse res = client.getFaceEntityWithOptions(request, runtime);
GetFaceEntityResponseBody.GetFaceEntityResponseBodyData data = res.getBody().data;
Map<String, Object> map = new HashMap<>();
map.put("labels", data.getLabels());
map.put("faces", data.getFaces());
return map;
}
/**
* @link {https://help.aliyun.com/document_detail/159216.html}
*/
public static String addFaceImage(String dbName, String entityId, String url) throws Exception {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.AddFaceRequest request = new com.aliyun.facebody20191230.models.AddFaceRequest();
request.setDbName(dbName);
request.setEntityId(entityId);
request.setImageUrl(url);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
AddFaceResponse res = client.addFaceWithOptions(request, runtime);
return res.getBody().data.faceId;
}
public static void searchFace(String daName, String imageUrl) {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.SearchFaceRequest searchFaceRequest = new com.aliyun.facebody20191230.models.SearchFaceRequest();
searchFaceRequest.setImageUrl(imageUrl);
searchFaceRequest.setLimit(3);
searchFaceRequest.setDbName(daName);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
SearchFaceResponse searchFaceResponse = client.searchFaceWithOptions(searchFaceRequest, runtime);
Map body = (Map) searchFaceResponse.toMap().get("body");
Map data = (Map) body.get("Data");
ArrayList list = (ArrayList) data.get("MatchList");
System.out.println(list);
for (int i = 0; i < list.size(); i++) {
Map faceList = (Map) list.get(i);
ArrayList items = (ArrayList) faceList.get("FaceItems");
for (int j = 0; j < items.size(); j++) {
Map item = (Map) items.get(j);
String entityId = (String) item.get("EntityId");
String faceId = (String) item.get("FaceId");
Float score = (Float) item.get("Score");
String dbName = (String) item.get("DbName");
System.out.println(
"[\n\tentityId=" + entityId + "\n" +
"\tfaceId=" + faceId + "\n" +
"\tscore=" + score + "\n" +
"\tdbName=" + dbName + "\n]\n"
);
}
}
} catch (TeaException error) {
// 如有需要,请打印 error
System.err.println("TeaException:" + error.message);
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
com.aliyun.teautil.Common.assertAsString(error.message);
System.err.println("Exception:" + error.message);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
public static URL uploadImage(String dbName, String entityId, String fileName, File file) {
String objectName = (new StringBuilder()
.append(dbName)
.append("/")
.append(entityId)
.append("/")
.append(fileName))
.toString();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
System.out.println(url);
return url;
} catch (OSSException oe) {
System.err.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.err.println("Error Message:" + oe.getErrorMessage());
System.err.println("Error Code:" + oe.getErrorCode());
System.err.println("Request ID:" + oe.getRequestId());
System.err.println("Host ID:" + oe.getHostId());
return null;
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
public static URL getImageUrl(String objectName) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// 设置签名URL过期时间,单位为毫秒。
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
System.out.println(url);
return url;
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
return null;
}
public static URL uploadImage(String dbName, String entityId, String fileName, String filePath) {
return uploadImage(dbName, entityId, fileName, new File(filePath));
}
public static String uploadImageUrl(String file) throws com.aliyuncs.exceptions.ClientException, IOException {
FileUtils fileUtils = FileUtils.getInstance(accessKeyId, accessKeySecret);
return fileUtils.upload(file);
}
public static ArrayList getFaceDb() {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.ListFaceDbsRequest listFaceDbsRequest = new com.aliyun.facebody20191230.models.ListFaceDbsRequest();
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
ListFaceDbsResponse listFaceDbsResponse = client.listFaceDbsWithOptions(listFaceDbsRequest, runtime);
Map body = (Map) listFaceDbsResponse.toMap().get("body");
Map data = (Map) body.get("Data");
ArrayList list = (ArrayList) data.get("DbList");
return list;
} catch (TeaException error) {
// 如有需要,请打印 error
System.err.println("Exception:" + error.message);
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.err.println("Exception:" + error.message);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
return null;
}
public static ArrayList getEntity(String dbName) {
com.aliyun.facebody20191230.Client client = createClient();
com.aliyun.facebody20191230.models.ListFaceEntitiesRequest listFaceEntitiesRequest = new com.aliyun.facebody20191230.models.ListFaceEntitiesRequest();
listFaceEntitiesRequest.setDbName(dbName);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
ListFaceEntitiesResponse listFaceEntitiesResponse = client.listFaceEntitiesWithOptions(listFaceEntitiesRequest, runtime);
Map body = (Map) listFaceEntitiesResponse.toMap().get("body");
Map data = (Map) body.get("Data");
ArrayList<Map> entities = (ArrayList<Map>) data.get("Entities");
for (int i = 0; i < entities.size(); i++) {
Map e = (Map) entities.get(i);
System.out.println("DbName:" + e.get("DbName"));
System.out.println("EntityId:" + e.get("EntityId"));
System.out.println("Labels:" + e.get("Labels"));
System.out.println("FaceCount:" + e.get("FaceCount"));
System.out.println("-----------------");
}
return entities;
} catch (TeaException error) {
// 如有需要,请打印 error
System.err.println("Exception:" + error.message);
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.err.println("Exception:" + error.message);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
return null;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论