文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 SDK for C++ 的 Amazon Rekognition 範例
下列程式碼範例示範如何搭配 Amazon Rekognition AWS SDK for C++ 使用 來執行動作並實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
案例是程式碼範例,示範如何透過呼叫服務內的多個函數或與其他函數結合來完成特定任務 AWS 服務。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
開始使用
下列程式碼範例說明如何開始使用 Amazon Rekognition。
- C++ 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 ListCollections AWS SDK for C++ 參考中的 API。
-
動作
下列程式碼範例示範如何使用 DetectLabels
。
如需詳細資訊,請參閱偵測映像中的標籤。
- C++ 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 DetectLabels AWS SDK for C++ 參考中的 API。
-
案例
下列程式碼範例示範如何建立無伺服器應用程式,讓使用者以標籤管理相片。