Skip to content

Commit

Permalink
Rekognition first commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws committed Jun 13, 2024
1 parent 672744a commit 5ea707f
Show file tree
Hide file tree
Showing 10 changed files with 616 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cpp/example_code/rekognition/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Set the minimum required version of CMake for this project.
cmake_minimum_required(VERSION 3.13)

set(SERVICE_NAME rekognition)
set(SERVICE_COMPONENTS rekognition)

# Set this project's name.
project("${SERVICE_NAME}-examples")

# Set the C++ standard to use to build this target.
set(CMAKE_CXX_STANDARD 11)

# Use the MSVC variable to determine if this is a Windows build.
set(WINDOWS_BUILD ${MSVC})

# Set the location of where Windows can find the installed libraries of the SDK.
if (WINDOWS_BUILD )
string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all")
list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH})
endif ()

# Find the AWS SDK for C++ package.
find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS})


if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS)
# Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging.

# set(BIN_SUB_DIR "/Debug") # If you are building from the command line, you may need to uncomment this
# and set the proper subdirectory to the executables' location.

AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR})
endif ()


# AWSDOC_SOURCE can be defined in the command line to limit the files in a build. For example,
# you can limit files to one action.
if (NOT DEFINED AWSDOC_SOURCE)
file(GLOB AWSDOC_SOURCE
"*.cpp"
)
endif ()

foreach (file ${AWSDOC_SOURCE})
get_filename_component(EXAMPLE ${file} NAME_WE)

# Build the code example executables.
set(EXAMPLE_EXE run_${EXAMPLE})

add_executable(${EXAMPLE_EXE} ${file}
hello_rekognition/hello_rekognition.cpp)

target_link_libraries(${EXAMPLE_EXE} ${AWSSDK_LINK_LIBRARIES}
${AWSSDK_PLATFORM_DEPS})

endforeach ()


if (BUILD_TESTS)
add_subdirectory(tests)
endif ()

105 changes: 105 additions & 0 deletions cpp/example_code/rekognition/detect_labels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/

#include <aws/core/Aws.h>
#include <aws/rekognition/RekognitionClient.h>
#include <aws/rekognition/model/DetectLabelsRequest.h>
#include <aws/rekognition/model/Image.h>
#include <iostream>
#include "rekognition_samples.h"

// snippet-start:[cpp.example_code.rekognition.DetectLabels]
//! Detect instances of real-world entities within an image by using Amazon Rekognition
/*!
\param imageBucket: The Amazon Simple Storage Service (Amazon S3) bucket containing an image.
\param imageKey: The Amazon S3 key of an image object.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc::Rekognition::detectLabels(const Aws::String &imageBucket,
const Aws::String &imageKey,
const Aws::Client::ClientConfiguration &clientConfiguration)
{
Aws::Rekognition::RekognitionClient rekognitionClient(clientConfiguration);

Aws::Rekognition::Model::DetectLabelsRequest request;
Aws::Rekognition::Model::S3Object s3Object;
s3Object.SetBucket(imageBucket);
s3Object.SetName(imageKey);

Aws::Rekognition::Model::Image image;
image.SetS3Object(s3Object);

request.SetImage(image);

const Aws::Rekognition::Model::DetectLabelsOutcome outcome = rekognitionClient.DetectLabels(request);

if (outcome.IsSuccess()) {
const Aws::Vector<Aws::Rekognition::Model::Label> &labels = outcome.GetResult().GetLabels();
if (labels.empty()) {
std::cout << "No labels detected" << std::endl;
}
else {
for (const Aws::Rekognition::Model::Label &label: labels) {
std::cout << label.GetName() << ": " << label.GetConfidence() << std::endl;
}
}
}
else {
std::cerr << "Error while detecting labels: '"
<< outcome.GetError().GetMessage()
<< "'" << std::endl;
}

return outcome.IsSuccess();
}

// snippet-end:[cpp.example_code.rekognition.DetectLabels]

/*
*
* main function
*
* Usage: 'run_detect_labels <bucket> <image_key>'
*
* Prerequisites: An S3 bucket with an image.
*
*
*/

#ifndef TESTING_BUILD

int main(int argc, char **argv) {
if (argc != 3) {
std::cout << "Usage: run_detect_labels <bucket> <image_key>" << std::endl;
return 1;
}
Aws::String bucket = argv[1];
Aws::String imageKey = argv[2];
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";

AwsDoc::Rekognition::detectLabels(bucket, imageKey, clientConfig);
}

Aws::ShutdownAPI(options);

return 0;
}

#endif // TESTING_BUILD
43 changes: 43 additions & 0 deletions cpp/example_code/rekognition/hello_rekognition/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# snippet-start:[cpp.example_code.rekognition.hello_rekognition.cmake]
# Set the minimum required version of CMake for this project.
cmake_minimum_required(VERSION 3.13)

# Set the AWS service components used by this project.
set(SERVICE_COMPONENTS rekognition)

# Set this project's name.
project("hello_rekognition")

# Set the C++ standard to use to build this target.
# At least C++ 11 is required for the AWS SDK for C++.
set(CMAKE_CXX_STANDARD 11)

# Use the MSVC variable to determine if this is a Windows build.
set(WINDOWS_BUILD ${MSVC})

if (WINDOWS_BUILD) # Set the location where CMake can find the installed libraries for the AWS SDK.
string(REPLACE ";" "/aws-cpp-sdk-all;" SYSTEM_MODULE_PATH "${CMAKE_SYSTEM_PREFIX_PATH}/aws-cpp-sdk-all")
list(APPEND CMAKE_PREFIX_PATH ${SYSTEM_MODULE_PATH})
endif ()

# Find the AWS SDK for C++ package.
find_package(AWSSDK REQUIRED COMPONENTS ${SERVICE_COMPONENTS})

if (WINDOWS_BUILD AND AWSSDK_INSTALL_AS_SHARED_LIBS)
# Copy relevant AWS SDK for C++ libraries into the current binary directory for running and debugging.

# set(BIN_SUB_DIR "/Debug") # If you are building from the command line, you may need to uncomment this
# and set the proper subdirectory to the executables' location.

AWSSDK_CPY_DYN_LIBS(SERVICE_COMPONENTS "" ${CMAKE_CURRENT_BINARY_DIR}${BIN_SUB_DIR})
endif ()

add_executable(${PROJECT_NAME}
hello_rekognition.cpp)

target_link_libraries(${PROJECT_NAME}
${AWSSDK_LINK_LIBRARIES})
# snippet-end:[cpp.example_code.rekognition.hello_rekognition.cmake]
61 changes: 61 additions & 0 deletions cpp/example_code/rekognition/hello_rekognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Hello Rekognition for the SDK for C++

## Overview

This folder provides a CMake "Hello Rekognition" project that uses the AWS SDK for C++ to call Amazon Rekognition.

## ⚠ Important

* Running this code might result in charges to your AWS account.
* Running the tests might result in charges to your AWS account.
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

## Run the Hello Rekognition app

### Prerequisites

Before using this example, first complete the installation and setup steps
for [Getting started](https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html) in the AWS SDK for
C++ Developer Guide.

### Instructions

This example uses the CMake build system. For information about the CMake build system, see https://cmake.org/.

Many Integrated Development Environments (IDEs) support CMake. If your preferred IDE supports CMake, follow the IDEs instructions to open this CMake project.

You can also build this project from a command line interface using the following commands.

```sh
mkdir build
cd build
cmake --build ..
```

The built executable is named `hello_rekognition`.

TODO (Add instructions about permissions)
You can run this example with the AWS managed policy "TODO: add policy".

You can also use CMake to generate the input files for your native build system.
For more information, see https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html.

The [CMakeLists.txt](CMakeLists.txt) file contains the build settings. If your build is failing (particularly on Windows), you might need to modify this file.

The [hello_rekognition.cpp](hello_rekognition.cpp) file contains the C++ source code, including a "main" function.



## Additional resources

* [Amazon Rekognition Developer Guide](https://docs.aws.amazon.com/amazonsevice_stub/latest/developerguide/Introduction.html)
* [Amazon Rekognition API Reference](https://docs.aws.amazon.com/amazonrekognition/latest/APIReference/Welcome.html)
* [Amazon Rekognition C++ APIs](https://sdk.amazonaws.com/cpp/api/LATEST/aws-cpp-sdk-rekognition/html/annotated.html)
* [AWS SDK for C++ Developer Guide](https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/welcome.html)

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
**/

// snippet-start:[cpp.example_code.rekognition.hello_rekognition]
#include <aws/core/Aws.h>
#include <aws/rekognition/RekognitionClient.h>
#include <aws/rekognition/model/ListCollectionsRequest.h>
#include <iostream>

/*
* A "Hello Rekognition" starter application which initializes an Amazon Rekognition client and
* lists the Amazon Rekognition collections in the current account and region.
*
* main function
*
* Usage: 'hello_rekognition'
*
*/

int main(int argc, char **argv) {
Aws::SDKOptions options;
// Optional: change the log level for debugging.
// options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug;
Aws::InitAPI(options); // Should only be called once.
{
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";

Aws::Rekognition::RekognitionClient rekognitionClient(clientConfig);
Aws::Rekognition::Model::ListCollectionsRequest request;
Aws::Rekognition::Model::ListCollectionsOutcome outcome =
rekognitionClient.ListCollections(request);

if (outcome.IsSuccess()) {
const Aws::Vector<Aws::String>& collectionsIds = outcome.GetResult().GetCollectionIds();
if (!collectionsIds.empty()) {
std::cout << "collectionsIds: " << std::endl;
for (auto &collectionId : collectionsIds) {
std::cout << "- " << collectionId << std::endl;
}
} else {
std::cout << "No collections found" << std::endl;
}
} else {
std::cerr << "Error with ListCollections: " << outcome.GetError()
<< std::endl;
}
}


Aws::ShutdownAPI(options); // Should only be called once.
return 0;
}
// snippet-end:[cpp.example_code.rekognition.hello_rekognition]
26 changes: 26 additions & 0 deletions cpp/example_code/rekognition/rekognition_samples.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Created by Meyer, Steve on 6/11/24.
//

#ifndef EXAMPLES_REKOGNITION_SAMPLES_H
#define EXAMPLES_REKOGNITION_SAMPLES_H

#include <aws/core/client/ClientConfiguration.h>

namespace AwsDoc {
namespace Rekognition {
//! Detect instances of real-world entities within an image by using Amazon Rekognition
/*!
\param image: The
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
*/

bool detectLabels(const Aws::String &imageBucket,
const Aws::String &imageKey,
const Aws::Client::ClientConfiguration &clientConfiguration);
} // Rekognition

}// AwsDoc

#endif //EXAMPLES_REKOGNITION_SAMPLES_H
Loading

0 comments on commit 5ea707f

Please sign in to comment.