Updated on 2024-12-05 GMT+08:00
Demo Project of Image Tagging
This section describes how to use the Image Tagging SDK by AK/SK authentication.
- Replace the AK/SK in the sample code with the actual AK/SK. You can use either the url or image parameter in the initialized ImageTaggingReq to configure image information. This section uses url as an example.
- endpoint indicates the regions and endpoints for Huawei Cloud services. For details, see Regions and Endpoints.
- The sample code for calling Image Tagging is as follows:
#include <cstdlib> #include <iostream> #include <string> #include <memory> #include <huaweicloud/core/exception/Exceptions.h> #include <huaweicloud/core/Client.h> #include <huaweicloud/image/v2/ImageClient.h> using namespace HuaweiCloud::Sdk::Image::V2; using namespace HuaweiCloud::Sdk::Image::V2::Model; using namespace HuaweiCloud::Sdk::Core; using namespace HuaweiCloud::Sdk::Core::Exception; using namespace std; int main(void) { // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables. // In this example, the AK and SK are stored in environment variables for identity authentication. Before running this example, configure environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. string ak = getenv("CLOUD_SDK_AK"); string sk = getenv("CLOUD_SDK_SK"); string projectId = ""; //Replace image.xx-xxx-xx.myhuaweicloud.com with the endpoint of the region where the service is enabled. string endpoint = "https://image.xx-xxx-xx.myhuaweicloud.com"; auto auth = std::make_unique<BasicCredentials>(); auth->withAk(ak) .withSk(sk) .withProjectId(projectId); HttpConfig httpConfig = HttpConfig(); auto client = ImageClient::newBuilder() .withCredentials(std::unique_ptr<Credentials>(auth.release())) .withHttpConfig(httpConfig) .withEndPoint(endpoint) .build(); RunImageTaggingRequest request; ImageTaggingReq body; body.setLimit(10); body.setThreshold(60); body.setLanguage("zh"); body.setImage("Input image encoded using Base64"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto response = client->runImageTagging(request); std::cout << response->getHttpBody() << std::endl; } catch (HostUnreachableException& e) { std::cout << "host unreachable:" << e.what() << std::endl; } catch (SslHandShakeException& e) { std::cout << "ssl handshake error:" << e.what() << std::endl; } catch (RetryOutageException& e) { std::cout << "retryoutage error:" << e.what() << std::endl; } catch (CallTimeoutException& e) { std::cout << "call timeout:" << e.what() << std::endl; } catch (ServiceResponseException& e) { std::cout << "http status code:" << e.getStatusCode() << std::endl; std::cout << "error code:" << e.getErrorCode() << std::endl; std::cout << "error msg:" << e.getErrorMsg() << std::endl; std::cout << "RequestId:" << e.getRequestId() << std::endl; } catch (exception& e) { std:cout << "unknown exception:" << e.what() << std::endl; } std::cout << "------request finished--------" << std::endl; return 0; }
- If the recognition result is displayed on the console, the execution is successful. For details about related parameters, see Image Tagging APIs.
1 2
RunImageTaggingResponse {"result": {"tags": [{"confidence": "98.01", "type": "Tree", "tag": "Tree", "i18n_tag": {"zh": "Chinese characters for tree", "en": "Tree"}, "i18n_type": {"zh": "Chinese characters for tree", "en": "Tree"}, "instances": []}]}} Process exiting with code: 0
Parent topic: Using the C++ SDK
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot