public APIGatewayProxyResponseEvent handleRequest()

in java-app-backend/BackendFunction/src/main/java/com/webapp/function/RecognizeImageHandler.java [47:105]


    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        String image = input.getBody();

        Map<String, String> headers = new HashMap<>();
        headers.put("Access-Control-Allow-Origin", "*");
        headers.put("Access-Control-Allow-Methods", "POST,GET");
        APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent()
                .withHeaders(headers);

        if (image == null || image.isEmpty()) {
            LOG.debug("No Image found in payload");
            return apiGatewayProxyResponseEvent
                    .withStatusCode(400)
                    .withBody("{\n" +
                            "  \"message\": \"No image found in body. Pass base 64 encode image in the request body\"\n" +
                            "}");
        }


        byte[] decodedImage = Base64.getDecoder().decode(image);

        List<FaceMatch> faceMatches = faceSearch(decodedImage);

        if (faceMatches.size() > 0) {
            FaceMatch faceMatch = faceMatches.get(0);

            LOG.debug("Details of matched face: {}", faceMatch);

            Map<String, AttributeValue> keyMap = new HashMap<>();

            keyMap.put("RekognitionId", AttributeValue.builder()
                    .s(faceMatch.face().faceId())
                    .build());

            GetItemResponse faceDetails = query(keyMap);

            if (faceDetails.hasItem()) {
                String fullName = faceDetails.item().get("FullName").s();

                putAnnotation("FullName", fullName);
                putMetadata("Confidence", faceMatch.face().confidence());

                metricsLogger().putMetric("FaceSearchCount", 1, Unit.COUNT);

                return apiGatewayProxyResponseEvent
                        .withStatusCode(200)
                        .withBody("{\n" +
                                "  \"person_name\": \"" + fullName + "\"\n" +
                                "}");
            }
        }

        metricsLogger().putMetric("FaceSearchCount", 0, Unit.COUNT);
        return apiGatewayProxyResponseEvent
                .withStatusCode(200)
                .withBody("{\n" +
                        "  \"message\": \"No match found in the record\"\n" +
                        "}");
    }