Amazon web services 无法识别使用Amazon Rekognition AWS Java SDK的用户

Amazon web services 无法识别使用Amazon Rekognition AWS Java SDK的用户,amazon-web-services,amazon-rekognition,Amazon Web Services,Amazon Rekognition,大家好 我正试图在一张图像上运行人脸检测,这张图像是基于从少数人的肖像图像创建的集合。所采用的方法如下: 创建集合名称“数据库” 索引各个图片中的面,并将其存储在集合“数据库”中 在目标图像上运行索引面,并将所有面存储在单独的集合“toBeDetected”中 使用SearchFaces API调用根据数据库集合识别目标图像中的所有面 然而,当我尝试这样做时,我得到了无效的参数异常。我对这一点非常陌生,并试图找到解决问题的办法,但我还没有找到任何办法。请帮忙。我已附上代码如下 public cl

大家好

我正试图在一张图像上运行人脸检测,这张图像是基于从少数人的肖像图像创建的集合。所采用的方法如下:

  • 创建集合名称“数据库”
  • 索引各个图片中的面,并将其存储在集合“数据库”中
  • 在目标图像上运行索引面,并将所有面存储在单独的集合“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);
    }
    }
    
    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) 找不到面:1BEC904-b4b8-417a-92bf-7ade964838c0 在com.amazonaws.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.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699) 位于com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667) 在com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)上 在com.amazonaws.http.AmazonHttpClient.execute上(AmazonHttpClient.java:513) 在com.amazonaws.services.rekognition.AmazonRekognitionClient.doInvoke(AmazonRekognitionClient.java:1458) 位于com.amazonaws.services.rekognition.AmazonRekognitionClient.invoke(AmazonRekognitionClient.java:1434) 位于com.amazonaws.services.rekognition.AmazonRekognitionClient.executeSearchFaces(AmazonRekognitionClient.java:1309) 位于com.amazonaws.services.rekognition.AmazonRekognitionClient.searchFaces(AmazonRekognitionClient.java:1285) 位于com.siemens.aws.recognition.MatchAllFacesInCollection.matchAllFacesInTargetCollection(MatchAllFacesInCollection.java:23) 位于com.siemens.aws.recognition.FRInvoker.invokeSystem(FRInvoker.java:79) 位于com.siemens.aws.recognition.FRInvoker.main(FRInvoker.java:67)


    请帮忙。谢谢大家!

    我也遇到了同样的问题,我的解决方案是将目标图像中的索引面添加到与源面相同的集合中

    算法:

  • 将面从源图像(照片)索引到集合X,并为ExternalImage ID使用唯一标识符
  • 为目标图像(集体照片)中的面编制索引,将它们添加到集合X中,并为所有找到的面使用ExternalMageID的公共值(“fromtargetimage”)
  • 循环上一个操作的结果
  • 对循环中当前元素的请求使用SearchFaces操作,其中image=target image,CollectionId=collection X,faceId=faceId
  • 在上一个操作返回的面中,获取第一个没有ExternalId=“fromtargetimage”的面
  • 循环中当前元素的ExternalId具有标识此人的信息,将此人添加到找到的面列表中并继续循环
  • 完成此操作后,可以使用“删除面”操作删除ExternalId=“fromtargetimage”中的所有面,仅保留原始源图像面

    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
        }
    
    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());
                    }
                }
            }
        }
    
    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);
    }