This document explains how to install and use the Node.js SDK for Alibaba Cloud Vision Intelligence Platform services and provides code examples.
For questions about SDK integration, API usage, or the visual AI capabilities of the Alibaba Cloud Vision Intelligence Platform, contact us through our DingTalk group (ID: 23109592).
This SDK requires Node.js 8.x or later. If your environment does not meet this requirement, upgrade Node.js.
Prerequisites
Before using an Alibaba Cloud SDK, you must have an Alibaba Cloud account and an AccessKey. For more information, see Create an AccessKey.
Run the following commands to install the SDK packages for the AI categories you need.
Face and Body:
npm install @alicloud/facebody20191230Optical Character Recognition:
npm install @alicloud/ocr20191230Product Understanding:
npm install @alicloud/goodstech20191230Content Moderation:
npm install @alicloud/imageaudit20191230Image Recognition:
npm install @alicloud/imagerecog20190930Image Generation:
npm install @alicloud/imageenhan20190930Image Segmentation:
npm install @alicloud/imageseg20191230Object Detection:
npm install @alicloud/objectdet20191230Image Analysis and Processing:
npm install @alicloud/imageprocess20200320Visual Search:
npm install @alicloud/imgsearch20200320Video Generation:
npm install @alicloud/videoenhan20200320Video Understanding:
npm install @alicloud/videorecog20200320Video Segmentation:
npm install @alicloud/videoseg20200320Asynchronous Task Management:
npm install @alicloud/viapi20230117Face Liveness server-side 20200910-specific version:
npm install @alicloud/facebody20200910
The following table provides the download links for the SDKs.
AI category |
SDK link |
GitHub link |
Face and Body |
||
Optical Character Recognition |
||
Product Understanding |
||
Content Moderation |
||
Image Recognition |
||
Image Generation |
||
Image Segmentation |
||
Object Detection |
||
Visual Search |
||
Image Analysis and Processing |
||
Video Generation |
||
Video Understanding |
||
Video Segmentation |
||
Asynchronous Task Management |
||
Face Liveness server-side 20200910-specific version |
Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.
Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
Configure environment variables on Linux and macOS
Open a terminal in IntelliJ IDEA.
Run the following commands to configure the environment variables.
Replace
<access_key_id>with the AccessKey ID of your RAM user and<access_key_secret>with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Configure environment variables on Windows
Create a new environment variable file, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.Open File Explorer, right-click This PC, and then select Properties.
In the left-side navigation pane, click Advanced system settings.
On the Advanced tab of the System Properties dialog box, click Environment Variables.
In the Environment Variables dialog box, click New.
In the New System Variable dialog box, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.Restart the Windows operating system for the configurations to take effect.
SDK examples
The following examples use the RecognizeBankCard API operation.
File in OSS
For the endpoints and supported regions for each AI category, see Endpoints.
// Import the SDK.
// 1. This example uses Optical Character Recognition (OCR). For other capabilities, import the package for the corresponding category. You can find package names in the "Prerequisites" section of this document and the capability name in the Action parameter of the corresponding API documentation. For example, to use the SegmentCommonImage API operation, refer to its API documentation at https://help.aliyun.com/document_detail/151960.html. You can determine that this capability belongs to the Image Segmentation category. Therefore, you must change `ocr20191230` to `imageseg20191230` and `RecognizeBankCard` to `SegmentCommonImage`.
const OcrClient = require('@alicloud/ocr20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// 2. To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// The AccessKey ID and AccessKey Secret are read from environment variables. You must configure the environment variables before you run the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// 3. The service endpoint. Note: You must change this to the endpoint of the corresponding category. For more information, see https://help.aliyun.com/document_detail/143103.html.
config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;
// 4. This example uses OCR. For other capabilities, use the Client class from the corresponding category package.
const client = new OcrClient.default(config);
// 5. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. For information about how to set the request parameters, see the documentation for the specific capability.
const recognizeBankCardRequest = new OcrClient.RecognizeBankCardRequest({
imageURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk1.jpg"
});
let runtime = new TeaUtil.RuntimeOptions({ });
// 6. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. The method name `recognizeBankCardWithOptions` must also be changed to the method name of the corresponding capability. The method name is generated based on the capability name according to specific rules. For example, if the capability name is `SegmentCommonImage`, the method name is `segmentCommonImageWithOptions`.
client.recognizeBankCardWithOptions(recognizeBankCardRequest, runtime)
.then(function(recognizeBankCardResponse) {
// Obtain the full response.
console.log(recognizeBankCardResponse);
// Obtain a specific field. This is only an example. For information about the fields for a specific capability, see the documentation for that capability.
console.log(recognizeBankCardResponse.body.data.cardNumber);
}, function(error) {
// Obtain the full error message.
console.log(error);
// Obtain a specific field from the error message.
console.log(error.data.Code);
})
The parts of the code that you need to modify are described in the comments. The following list provides a summary:
When importing packages, import the package and related classes of the corresponding category. You can find package names in the preceding list of SDK packages and find the capability name in the Action parameter of the corresponding API documentation.
For example, to use the
SegmentCommonImageAPI operation, you can learn from the API documentation for SegmentCommonImage that this capability belongs to the Image Segmentation category (imageseg20191230) and the capability name is SegmentCommonImage. You must changeocr20191230toimageseg20191230andRecognizeBankCardtoSegmentCommonImagein the code.Make sure to change the endpoint to the one for the correct category. If the endpoint and category do not match, an
InvalidAction.NotFounderror is returned. For more information about endpoints, see Endpoints.The Client class must be from the package of the corresponding category.
The Request and Response objects must be from the package and classes of the corresponding category.
When you call a Client method, change the method name to the one for the corresponding capability. The method name is generated based on the capability name. For example, if the capability name is
SegmentCommonImage, the method name issegmentCommonImageWithOptions.
Local file or URL
The main difference from the previous example is the use of an xxxAdvanceRequest object, where the file is passed as a stream to the imageURLObject parameter.
// Import the SDK.
// 1. This example uses Optical Character Recognition (OCR). For other capabilities, import the package for the corresponding category. You can find package names in the "Prerequisites" section of this document and the capability name in the Action parameter of the corresponding API documentation. For example, to use the SegmentCommonImage API operation, refer to its API documentation at https://help.aliyun.com/document_detail/151960.html. You can determine that this capability belongs to the Image Segmentation category. Therefore, you must change `ocr20191230` to `imageseg20191230` and `RecognizeBankCard` to `SegmentCommonImage`.
const OcrClient = require('@alicloud/ocr20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');
let config = new OpenapiClient.Config({
// 2. To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use an AccessKey of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// The AccessKey ID and AccessKey Secret are read from environment variables. You must configure the environment variables before you run the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// 3. The service endpoint. Note: You must change this to the endpoint of the corresponding category. For more information, see https://help.aliyun.com/document_detail/143103.html.
config.endpoint = `ocr.cn-shanghai.aliyuncs.com`;
// 4. This example uses OCR. For other capabilities, use the Client class from the corresponding category package.
const client = new OcrClient.default(config);
const getResponse = function (httpClient, url) {
return new Promise((resolve, reject) => {
httpClient.get(url, function (response) {
resolve(response);
})
})
}
const request = async function () {
try {
// 5. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. For information about how to set the request parameters, see the documentation for the specific capability.
let recognizeBankCardAdvanceRequest = new OcrClient.RecognizeBankCardAdvanceRequest();
// Scenario 1: Use a local file.
// const fileStream = fs.createReadStream('/tmp/bankCard.png');
// recognizeBankCardAdvanceRequest.imageURLObject = fileStream;
// Scenario 2: Use any accessible URL.
const url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk1.jpg");
const httpClient = (url.protocol == "https:") ? https : http;
recognizeBankCardAdvanceRequest.imageURLObject = await getResponse(httpClient, url);
let runtime = new TeaUtil.RuntimeOptions({});
// 6. This example uses the RecognizeBankCard operation of OCR. For other capabilities, use the package and class of the corresponding category. The method name `recognizeBankCardAdvance` must also be changed to the method name of the corresponding capability. The method name is generated based on the capability name according to specific rules. For example, if the capability name is `SegmentCommonImage`, the method name is `segmentCommonImageAdvance`.
client.recognizeBankCardAdvance(recognizeBankCardAdvanceRequest, runtime)
.then(function(recognizeBankCardResponse) {
// Obtain the full response.
console.log(recognizeBankCardResponse);
// Obtain a specific field from the response.
console.log(recognizeBankCardResponse.body.data);
}, function(error) {
// Obtain the full error message.
console.log(error);
// Obtain a specific field from the error message.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();
The parts of the code that you need to modify are described in the comments. The following list provides a summary:
When importing packages, import the package and related classes of the corresponding category. You can find package names in the preceding list of SDK packages and find the capability name in the Action parameter of the corresponding API documentation.
For example, to use the
SegmentCommonImageAPI operation, you can learn from the API documentation for SegmentCommonImage that this capability belongs to the Image Segmentation category (imageseg20191230) and the capability name is SegmentCommonImage. You must changeocr20191230toimageseg20191230andRecognizeBankCardtoSegmentCommonImagein the code.Make sure to change the endpoint to the one for the correct category. If the endpoint and category do not match, an
InvalidAction.NotFounderror is returned. For more information about endpoints, see Endpoints.The Client class must be from the package of the corresponding category.
The Request and Response objects must be from the package and classes of the corresponding category.
When you call a Client method, change the method name to the one for the corresponding capability. The method name is generated based on the capability name. For example, if the capability name is
SegmentCommonImage, the method name issegmentCommonImageAdvance.
FAQ
How do I troubleshoot API call errors?
If an API call fails, first upgrade the SDK package to the latest version, available at the SDK link for each category. If your program uses multiple packages, upgrade all of them to their latest versions to prevent conflicts.
Latest package version not in npm
If you find that the latest package version displayed in the OpenAPI interface is not available in the npm repository, this is because the new version has just been released and repository synchronization may be delayed. If you encounter this issue, please try again later or use the latest version that is available in the npm repository.
Client-side SDK usage
No, this SDK is designed for server-side use. To call the API from a web client or mini program, you must build and sign the request manually based on the request signing mechanism. For more information, see Direct calls from a web client or Direct calls in mini program scenarios.
Technical support
If the preceding suggestions do not resolve your issue, join the Alibaba Cloud Vision Intelligence Platform support group on DingTalk (ID: 23109592). A technical specialist will be available to assist you.