提交 23c0a04e authored 作者: yangqi's avatar yangqi

回调

上级 14bcb67b
......@@ -3,13 +3,20 @@ package cn.lefull.api;
import cn.lefull.common.annotation.NoAuth;
import cn.lefull.common.response.ApiResponse;
import cn.lefull.service.ali.FaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @author 杨奇
......@@ -17,6 +24,9 @@ import java.util.Map;
@RestController
@RequestMapping("/face")
public class FaceController {
public static final String REAL_PATH = "/opt/server/iot/";
@Autowired
private FaceService faceService;
......@@ -68,4 +78,58 @@ public class FaceController {
faceService.searchFace(dbName, url);
return new ApiResponse<>();
}
@NoAuth
@RequestMapping("/uploads")
public String uploadFiles(HttpServletRequest request,
@RequestPart("faceImage") MultipartFile faceImage,
@RequestPart("backgroundImage") MultipartFile backgroundImage) {
Logger logger = LoggerFactory.getLogger(FaceController.class);
logger.info("upload....");
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
//获得路径
String format = sdf.format(new Date());
String path = REAL_PATH + format;
//创建文件夹
File fold = new File(path);
while (!fold.exists()) {fold.mkdirs();}
StringBuffer retsb = new StringBuffer();
MultipartFile[] uploadFile = new MultipartFile[] {faceImage, backgroundImage};
try {
for (int i = 0; i < uploadFile.length; i++) {
MultipartFile file = uploadFile[i];
String oldname = file.getOriginalFilename();
// 新文件名
String newname = UUID.randomUUID().toString() + "_" + file.getName() + ".jpg";
file.transferTo(new File(fold, newname));
String ssss = request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort() +
"/facebody/preview?path=" + format + newname;
logger.info(ssss);
retsb.append(String.format("<img src=\"%s\" /><br/>\n", ssss));
}
} catch (IOException e) {
e.printStackTrace();
}
return retsb.toString();
}
@NoAuth
@RequestMapping("/upload")
public void uploadFile() throws Exception {
String dbName = "faceDb_321";
String file = "D:/phpstudy/phpstudy_pro/WWW/wework/runtime/1.jpg";
faceService.searchFaces(dbName, file);
}
}
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);
}
}
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;
}
}
......@@ -2,14 +2,18 @@ package cn.lefull.common.sdk.ali.base;
import com.aliyun.com.viapi.FileUtils;
import com.aliyun.facebody20191230.models.*;
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 org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
@Component
......@@ -186,4 +190,38 @@ public class Face {
FileUtils fileUtils = FileUtils.getInstance(this.accessKeyId, this.accessKeySecret);
return fileUtils.upload(file);
}
public URL uploadImage(String fileName, File file) {
String objectName = (new StringBuilder()
.append(fileName))
.toString();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
PutObjectRequest request = new PutObjectRequest(bucketName, objectName, file);
ossClient.putObject(request);
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();
}
}
}
}
......@@ -45,4 +45,9 @@ public interface FaceService {
* 搜索人脸
*/
boolean searchFace(String dbName, String url) throws Exception;
/**
* 搜索人脸
*/
public void searchFaces(String dbName, String filePath) throws Exception;
}
......@@ -15,6 +15,8 @@ import com.aliyun.facebody20191230.models.SearchFaceResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.Map;
......@@ -157,4 +159,14 @@ public class FaceServiceImpl implements FaceService{
logMapper.addLog(log);
return true;
}
@Override
public void searchFaces(String dbName, String filePath) throws Exception {
Utils.checkParamsNotEmpty(dbName, "dbName");
Utils.checkParamsNotEmpty(filePath, "filePath");
File file = new File(filePath);
URL url = sdk.uploadImage(dbName, "1.jpg", file);
sdk.searchFace(dbName, url.toString());
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论