文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用适用于 C++ 的 SDK 的亚马逊 Rekognition 示例
以下代码示例向您展示了如何使用 AWS SDK for C++ 与 Amazon Rekognition 配合使用来执行操作和实现常见场景。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
场景是向您演示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
开始使用
以下代码示例展示了如何开始使用 Amazon Rekognition。
- SDK for C++
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 CMakeLists.txt 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})
hello_rekognition.cpp 源文件的代码。
#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; }
-
有关 API 的详细信息,请参阅 AWS SDK for C++ API 参考ListCollections中的。
-
操作
以下代码示例演示如何使用 DetectLabels
。
有关更多信息,请参阅检测图像中的标签。
- SDK for C++
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 //! 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(); }
-
有关 API 的详细信息,请参阅 AWS SDK for C++ API 参考DetectLabels中的。
-
场景
以下代码示例演示如何创建无服务器应用程序,让用户能够使用标签管理照片。