大家好,
我正在尝试根据从少数人的肖像图像创建的集合对一张图像进行人脸检测。使用的方法如下:
- 创建集合名称“DATABASE”
- 从单个图片中索引人脸并将它们存储在集合“数据库”中。
- 在目标图像上运行索引面孔并将所有面孔存储在单独的集合“toBeDetected”中。
- 使用 SearchFaces API 调用根据数据库集合识别目标图像中的所有人脸。
但是,当我尝试这样做时,我得到了无效的参数异常。我对此很陌生,并试图找到问题的解决方案,但我还没有。请帮忙。我附上了如下代码。
public class FRInvoker {
public static final String COLLECTION_ID_DATABASE = "collectionDatabase";
// public static final String COLLECTION_ID_TARGET = "toBeDetected";
public static Map<String, String> names = new HashMap<>();
private static AmazonRekognition amazonRekognition;
//Configure Credentials
public FRInvoker() {
AWSCredentials credentials;
try {
credentials = new BasicAWSCredentials("XXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
} catch (Exception e) {
throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
+ "Please make sure that your credentials file is at the correct "
+ "location (/Users/userid/.aws/credentials), and is in a valid format.", e);
}
amazonRekognition = AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_WEST_2)
.withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
}
public static void main(String[] args) {
FRInvoker invoker = new FRInvoker();
invoker.invokeSystem();
}
private void invokeSystem(){
AddFacesToRekognitionCollection faceRecognition = new AddFacesToRekognitionCollection(amazonRekognition);
faceRecognition.addFacesToRecognition(amazonRekognition);
DetectMultipleFaceHelper detectMultipleFaceHelper = new DetectMultipleFaceHelper();
detectMultipleFaceHelper.detectAllPossibleFaces(amazonRekognition);
MatchAllFacesInCollection matchFacesInCollection = new MatchAllFacesInCollection();
matchFacesInCollection.matchAllFacesInTargetCollection(amazonRekognition);
}
}
RekognitionCollectionCreateHelper
public class RekognitionCollectionCreateHelper {
public void createCollections(AmazonRekognition amazonRekognition, String collectionName) {
DeleteCollectionRequest request = new DeleteCollectionRequest().withCollectionId(collectionName);
amazonRekognition.deleteCollection(request);
try {
amazonRekognition.createCollection(new CreateCollectionRequest().withCollectionId(collectionName));
} catch (com.amazonaws.services.rekognition.model.ResourceAlreadyExistsException e) {
System.out.println(collectionName + "Already Exists");
System.out.println("Listing Existing Collections : \n");
this.printCollectionList(amazonRekognition);
}
}
private ListCollectionsResult callListCollections(String paginationToken, int limit,
AmazonRekognition amazonRekognition) {
ListCollectionsRequest listCollectionsRequest = new ListCollectionsRequest().withMaxResults(limit)
.withNextToken(paginationToken);
return amazonRekognition.listCollections(listCollectionsRequest);
}
private void printCollectionList(AmazonRekognition amazonRekognition){
int limit = 1;
ListCollectionsResult listCollectionsResult = null;
String paginationToken = null;
do {
if (listCollectionsResult != null) {
paginationToken = listCollectionsResult.getNextToken();
}
listCollectionsResult = callListCollections(paginationToken, limit, amazonRekognition);
List<String> collectionIds = listCollectionsResult.getCollectionIds();
for (String resultId : collectionIds) {
System.out.println(resultId);
}
} while (listCollectionsResult != null && listCollectionsResult.getNextToken() != null);
}
public void printContentOfCollection(AmazonRekognition amazonRekognition, String collectionName){
ObjectMapper objectMapper = new ObjectMapper();
ListFacesResult listFacesResult = null;
System.out.println("Faces in collection " + collectionName);
String paginationToken = null;
do {
if (listFacesResult != null) {
paginationToken = listFacesResult.getNextToken();
}
ListFacesRequest listFacesRequest = new ListFacesRequest()
.withCollectionId(collectionName)
.withMaxResults(1)
.withNextToken(paginationToken);
listFacesResult = amazonRekognition.listFaces(listFacesRequest);
List<Face> faces = listFacesResult.getFaces();
for (Face face: faces) {
try {
System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(face));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while (listFacesResult != null && listFacesResult.getNextToken() !=
null);
}
}
AddFacesToRekognitionCollection
public AddFacesToRekognitionCollection(AmazonRekognition amazonRekognition) {
RekognitionCollectionCreateHelper newCollectionCreator = new
RekognitionCollectionCreateHelper();
// newCollectionCreator.deleteAllAwsCollections(amazonRekognition);
newCollectionCreator.createCollections(amazonRekognition, FRInvoker.COLLECTION_ID_DATABASE);
}
public void addFacesToRecognition(AmazonRekognition amazonRekognition) {
File[] files = getAllImageFiles();
for (int i = 0; i < files.length; i++) {
Image image = new
Image().withBytes(AddFacesToRekognitionCollection.getImageBytes(files[i]));
String externalImageId = files[i].getName();
IndexFacesResult indexFacesResult = callIndexFaces(FRInvoker.COLLECTION_ID_DATABASE, externalImageId, "ALL", image,
amazonRekognition);
List<FaceRecord> faceRecords = indexFacesResult.getFaceRecords();
for (FaceRecord faceRecord : faceRecords) {
System.out.println("Image name: " + files[i].getName() + " ::::::::: Faceid is " + faceRecord.getFace().getFaceId());
FRInvoker.names.put(faceRecord.getFace().getFaceId(), files[i].getName());
}
}
}
//Private Helper Methods
public static ByteBuffer getImageBytes(File file) {
ByteBuffer imageBytes = null;
try (InputStream inputStream = new FileInputStream(file)) {
imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
} catch (IOException e) {
e.printStackTrace();
}
return imageBytes;
}
private IndexFacesResult callIndexFaces(String collectionId, String externalImageId, String attributes, Image image,
AmazonRekognition amazonRekognition) {
IndexFacesRequest indexFacesRequest = new IndexFacesRequest().withImage(image).withCollectionId(collectionId);
return amazonRekognition.indexFaces(indexFacesRequest);
}
public static File[] getAllImageFiles() {
File dir = new File(System.getProperty("user.dir") + "/imageDatabase/");
System.out.println(dir.getAbsolutePath());
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png");
}
});
return files;
}
//Private Helper Methods
}
MatchAllFacesInCollection
public class MatchAllFacesInCollection {
public void matchAllFacesInTargetCollection(AmazonRekognition amazonRekognition) {
ListFacesRequest request = new ListFacesRequest().withCollectionId(FRInvoker.COLLECTION_ID_TARGET)
.withMaxResults(50);
ListFacesResult response = amazonRekognition.listFaces(request);
for (Face face : response.getFaces()) {
SearchFacesRequest searchFaceRequest = new SearchFacesRequest()
.withCollectionId(FRInvoker.COLLECTION_ID_DATABASE).withFaceId(face.getFaceId())
.withMaxFaces(1).withFaceMatchThreshold(90f);
SearchFacesResult searchFaceResponse = null;
try{
searchFaceResponse = amazonRekognition.searchFaces(searchFaceRequest);
System.out.println(searchFaceResponse.getFaceMatches().get(0).getFace().getFaceId() + " matches best with Highest Matching rate of" +
searchFaceResponse.getFaceMatches().get(0).getSimilarity());
}catch(com.amazonaws.services.rekognition.model.InvalidParameterException e){
e.printStackTrace();
System.out.println("Face Not Found :::::: " + face.getFaceId());
}
}
}
}
DetectMultipleFaceHelper
public class DetectMultipleFaceHelper {
public void detectAllPossibleFaces(AmazonRekognition amazonRekognition) {
RekognitionCollectionCreateHelper collectionCreaterHelper = new RekognitionCollectionCreateHelper();
collectionCreaterHelper.createCollections(amazonRekognition, FRInvoker.COLLECTION_ID_TARGET);
IndexFacesRequest request = new IndexFacesRequest().withCollectionId(FRInvoker.COLLECTION_ID_TARGET)
.withImage(new Image().withBytes(AddFacesToRekognitionCollection.getImageBytes(new File(System.getProperty("user.dir") + "/ImageToRekognize/target.jpg"))));
amazonRekognition.indexFaces(request);
}
}
com.amazonaws.services.rekognition.model.InvalidParameterException:在集合中找不到 faceId。(服务:AmazonRekognition;状态代码:400;错误代码:InvalidParameterException;请求 ID:e28de8f9-d5b4-11e7-b9db-4fe55f28a54b)在 com.amazonaws 上找不到人脸 :::::: 1becc904-b4b8-417a-92bf-7ade964838c0 .http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1638) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1303) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java :1055) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743) 在 com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717) 在 com.amazonaws.http。
请帮忙。谢谢!