You can call Visual Intelligence API operations using a software development kit (SDK). We recommend integrating the SDK on the server-side because directly embedding your AccessKey ID and AccessKey secret in client-side code creates a security risk. Instead, use a Security Token Service (STS) authorized user to call the service.
Background information
Before you make a web call, you must obtain temporary access credentials from the Security Token Service (STS). Alibaba Cloud STS is a service that manages temporary access permissions. You can use STS to issue temporary access credentials to other users. These users can then use the credentials to call Visual Intelligence API services for a limited time. Temporary credentials protect your account because you do not need to expose your long-term keys. For more information about how to obtain temporary credentials, see Obtain temporary identity credentials for a RAM role.
If your file is in an Object Storage Service (OSS) bucket in the China (Shanghai) region:
If your file is in an Object Storage Service (OSS) bucket in the China (Shanghai) region, you must use the request signing mechanism to call the API. For the specific method, see Scenario 1: The file is in an OSS bucket in the China (Shanghai) region. For more information about OSS operations, see Activate OSS.
If you have a local file or a file at another accessible URL:
If you have a local file or a file at another accessible URL, you must first convert the file to an OSS link in the China (Shanghai) region. For instructions, see File URL processing. For the specific calling method, see Scenario 2: The file is a local file or at an accessible URL.
If you have questions about API access, usage, or other issues related to the visual AI capabilities of the Visual Intelligence API, contact us by joining the DingTalk group: 23109592.
Scenario 1: The file is in an OSS bucket in the China (Shanghai) region
If your file is in an OSS bucket in the China (Shanghai) region, you can call the API using the request signing method. This topic uses the RecognizeBankCard operation as an example and shows only the key steps and code. For a complete example, download the WebDemo. To call other algorithms, you can see the comments in the code and modify it as needed.
Interaction flow
Prerequisites
Obtain an STS temporary credential:
Grant permissions:
Before obtaining an STS temporary credential, the caller, such as a Resource Access Management (RAM) user or RAM role, must have the permissions to call STS operations. You can grant these permissions by creating a RAM access policy. For more information about the setup steps and access policies, see Use temporary access credentials provided by STS to access OSS. You can configure more fine-grained authorization policies as needed to prevent security threats that are caused by excessive permissions. For more information about how to configure fine-grained authorization policies, see Custom policies for Visual Intelligence API.
ImportantTo complete the following steps, the caller, such as a RAM user or RAM role, must be granted the AliyunSTSAssumeRoleAccess permission, which is required to call the AssumeRole operation of STS, and the AliyunVIAPIFullAccess permission. For this example, the AliyunVIAPIFullAccess permission is used to manage the Visual Intelligence API. In a production environment, we strongly recommend that you configure more fine-grained authorization policies as needed to prevent security threats that are caused by excessive permissions. For more information about how to configure fine-grained authorization policies, see Custom policies for Visual Intelligence API.
Call the AssumeRole operation:
Use an authorized RAM user or RAM role to call the AssumeRole operation and fill in the required parameters as described in the API documentation. For more information about the operation and its usage, see the AssumeRole documentation.
Use the STS token:
After you successfully call the AssumeRole operation, you will receive an STS token that contains an
AccessKeyId, anAccessKeySecret, and aSecurityToken, as shown in the following code. When you call other Alibaba Cloud service operations, you must replace<ALIBABA_CLOUD_ACCESS_KEY_ID>,<ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and<ALIBABA_CLOUD_SECURITY_TOKEN>in the code with the temporaryAccessKeyId,AccessKeySecret, andSecurityTokenfrom the STS token data.
{
"RequestId": "429D9967-C809-5A30-B65E-9B742CF*****",
"AssumedRoleUser": {
"Arn": "acs:ram::175805416243****:role/STStokenTestRole/STSsessionName",
"AssumedRoleId": "39779315882322****:STSsessionName"
},
"Credentials": {
"SecurityToken": "exampleToken",
"AccessKeyId": "STS.exampleAccessKeyID",
"AccessKeySecret": "exampleAccessKeySecret",
"Expiration": "2024-06-12T03:21:29Z"
}
}Step 1: Configure basic parameters
The following code segment is from the js/script.js file in the WebDemo and calls the RecognizeBankCard service of Alibaba Cloud.
Prepare the parameters for the API call:
AccessKeyId, AccessKeySecret, and SecurityToken: First, replace the
<ALIBABA_CLOUD_ACCESS_KEY_ID>,<ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and<ALIBABA_CLOUD_SECURITY_TOKEN>variables with the temporary AccessKey pair and security token from the STS token that you obtained in the Prerequisites section.Endpoint: The API endpoint. In this example, it is
ocr.cn-shanghai.aliyuncs.com.Action: The name of the API operation. In this example, it is
RecognizeBankCard, which indicates that the bank card recognition service is being called.API Version: The API version. In this example, it is
2019-12-30.Make sure to grant permissions to the RAM user or role to ensure that it has the permissions to call the Visual Intelligence API operations. For more information, see Grant permissions in the Prerequisites section.
Construct the request parameter object:
The
request_object contains the system parameters required for the API call, such as the signature method, timestamp, and format, and the business parameters. In this example, the business parameter is the URL of the bank card image to be recognized, which is specified byImageURL.Initiate the API request:
Call the encapsulated
callApiRequestfunction. Pass the prepared request parameters, the HTTP request method, the API endpoint, the AccessKey secret of the STS token, and a callback function. The callback function is used to process the API response.
For example, if you want to use the common segmentation feature, you can find in the common segmentation API documentation that this feature belongs to the image segmentation category (imageseg20191230) and the operation name is SegmentCommonImage. In this case, you can change the endpoint to imageseg.cn-shanghai.aliyuncs.com and the Action to SegmentCommonImage. Keep the API_VERSION as 2019-12-30 and the request_["ImageURL"] parameter name as ImageURL. When you receive the result, note that the ImageURL parameter is not a bank card number, but the URL of the segmented image.
Before running the code, you must import the CryptoJS package to calculate the signature. For more information, see the CryptoJS package download page and the CDN address. For more information about the signature calculation logic, see Request signing.
/**
* ========================================================================================================================
* This is an example for RecognizeBankCard.
* In the code, replace <ALIBABA_CLOUD_ACCESS_KEY_ID>, <ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and <ALIBABA_CLOUD_SECURITY_TOKEN> with the temporary AccessKeyId, AccessKeySecret, and SecurityToken from the Alibaba Cloud STS token data.
* For more information about how to recognize bank cards, see https://help.aliyun.com/document_detail/151893.html
* ========================================================================================================================
*/
function callRecognizeBankCard(callback) {
/**
Replace <ALIBABA_CLOUD_ACCESS_KEY_ID>, <ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and <ALIBABA_CLOUD_SECURITY_TOKEN> with the temporary AccessKeyId, AccessKeySecret, and SecurityToken from the STS token data.
You must grant the AliyunVIAPIFullAccess permission to the RAM user or RAM role. For more information, see https://help.aliyun.com/document_detail/145025.html
*/
//AccessKeyID
const accessKeyId = "<ALIBABA_CLOUD_ACCESS_KEY_ID>";
//AccessKeySecret
const accessKeySecret = "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>";
//SecurityToken
const securityToken = "<ALIBABA_CLOUD_SECURITY_TOKEN>";
// The endpoint is the API endpoint, which is related to the category. For more information about API endpoints for different categories, see https://help.aliyun.com/document_detail/143103.html
const endpoint = "ocr.cn-shanghai.aliyuncs.com";
// The Action is the name of the operation. For more information about the Action parameter for a specific algorithm, see its API documentation. This example uses RecognizeBankCard: https://help.aliyun.com/document_detail/151893.html
const Action = "RecognizeBankCard";
// We recommend that you set API_HTTP_METHOD to POST.
const API_HTTP_METHOD = "POST";
// API_VERSION is the API version, which is related to the category. For more information about API versions for different categories, see https://help.aliyun.com/document_detail/464194.html
const API_VERSION = "2019-12-30";
const request_ = {};
//System parameters
request_["SignatureMethod"] = "HMAC-SHA1";
request_["SignatureNonce"] = signNRandom();
request_["AccessKeyId"] = accessKeyId;
request_["SignatureVersion"] = "1.0";
request_["Timestamp"] = getTimestamp();
request_["Format"] = "JSON";
request_["RegionId"] = "cn-shanghai";
request_["Version"] = API_VERSION;
request_["Action"] = Action;
request_["SecurityToken"] = securityToken;
// Business parameters. Modify them based on the API documentation for the specific AI capability.
request_["ImageURL"] = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeBankCard/yhk3.jpg";
callApiRequest(request_, API_HTTP_METHOD, endpoint, accessKeySecret, callback);
}Step 2: Call the server-side operation and calculate the signature
The following code segment implements the signature calculation and request sending process for Visual Intelligence API operations. Signing is a common security measure for cloud services. It ensures that requests sent to the cloud service are not tampered with and are initiated by legitimate users with the corresponding credentials. For more information about the logic, see Request signing.
The following section describes the meaning and steps of this code segment:
Signature steps:
Generate a random number (Nonce): The
signNRandomfunction generates a random number. This makes each request unique to prevent replay attacks.Get the timestamp: The
getTimestampfunction generates a timestamp that complies with the ISO 8601 standard. It indicates the time the request was sent.Sort the parameters: The
ksortfunction sorts all request parameters in lexicographic order. The signature algorithm requires that the parameters are arranged in a specific order.Calculate the signature: The
createHmacandgetSignaturefunctions are used together. They use the provided AccessKey secret and the canonical request string generated above to calculate the request signature using the HMAC-SHA1 algorithm.Construct the request URL: The
generateUrlfunction constructs the complete request URL. It includes all the sorted request parameters and the calculated signature.
Request sending steps:
Send the request: The
callApiRequestfunction uses thehttp.postfunction to send an HTTP POST request with the URL constructed above.Process the response: When the server responds to the request, the callback function is called to process the returned result, such as converting the JSON response text into an object.
Step 3: Encapsulate the request and send data to the specified Visual Intelligence API operation
The following code segment is an example implementation in a web frontend environment for encapsulating HTTP requests and sending data to a specified API. It provides an encapsulated method for calling the API from the web frontend, which simplifies the handling of asynchronous HTTP requests by defining request parameters and setting a callback method. It also provides basic error handling, such as for timeouts, and simplifies the process of sending JSON data using the POST method.
The following section describes the meaning and steps of this code segment:
Build the HTTP library: Create an
httpobject that contains all HTTP request methods.Implement the basic request method: Use the
http.requestfunction to support basic HTTP requests usingXMLHttpRequest.Encapsulate specific request types: Provide the
http.postmethod to simplify the process of sending POST requests in JSON format.Initiate the request and process the result: Use the
callApiRequestfunction to show how to use the encapsulated HTTP library to send requests and process responses.
Step 4: Trigger the button on the page to call the Visual Intelligence API operation
You can trigger the button on the page to call the API, in this case, the RecognizeBankCard operation. The following section describes the meaning and steps of this code segment:
The user clicks the button: This triggers the
callRecognizeBankCardfunction.Perform the bank card recognition operation: The
callRecognizeBankCardfunction sends a request to the backend to recognize the bank card information.Process the recognition result:
Error handling: Display different error messages based on the error code (
Code).Success handling: After the bank card information is successfully recognized, display the bank card number (
CardNumber).
Scenario 2: The file is a local file or at an accessible URL
If you have a local file or a file at an accessible URL, see File URL processing to explicitly convert the file to an OSS link in the China (Shanghai) region. Then, you can call the API as described in Scenario 1: The file is in an OSS bucket in the China (Shanghai) region. This topic uses the RecognizeBankCard operation as an example and shows only the key steps and code. For a complete example, download the WebDemo. To call other algorithms, you can see the comments in the code and modify it as needed.
Interaction flow
Prerequisites
Obtain an STS temporary credential:
Grant permissions:
Before obtaining an STS temporary credential, the caller, such as a RAM user or RAM role, must have the permissions to call STS operations. You can grant these permissions by creating a RAM access policy. For more information about the setup steps, see Use temporary access credentials provided by STS to access OSS. You can configure more fine-grained authorization policies as needed to prevent security threats that are caused by excessive permissions. For more information about how to configure fine-grained authorization policies, see Custom policies for Visual Intelligence API.
ImportantTo complete the following steps, the caller, such as a RAM user or RAM role, must be granted the AliyunSTSAssumeRoleAccess permission, which is required to call the AssumeRole operation of STS, the permission for a RAM role to upload OSS files, and the AliyunVIAPIFullAccess permission. For this example, the AliyunVIAPIFullAccess permission is used to manage the Visual Intelligence API. In a production environment, we strongly recommend that you configure more fine-grained authorization policies as needed to prevent security threats that are caused by excessive permissions. For more information about how to configure fine-grained authorization policies, see Custom policies for Visual Intelligence API.
Call the AssumeRole operation:
Use an authorized RAM user or RAM role to call the AssumeRole operation and fill in the required parameters as described in the API documentation. For more information about the operation and its usage, see the AssumeRole documentation.
Use the STS token:
After you successfully call the AssumeRole operation, you will receive an STS token that contains an AccessKeyId, an AccessKeySecret, and a SecurityToken, as shown in the following code. When you call other Alibaba Cloud service operations, you must replace
<ALIBABA_CLOUD_ACCESS_KEY_ID>, <ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and <ALIBABA_CLOUD_SECURITY_TOKEN>in the code with the temporaryAccessKeyId, AccessKeySecret, and SecurityTokenfrom the STS token data.
{
"RequestId": "429D9967-C809-5A30-B65E-9B742CF*****",
"AssumedRoleUser": {
"Arn": "acs:ram::175805416243****:role/STStokenTestRole/STSsessionName",
"AssumedRoleId": "39779315882322****:STSsessionName"
},
"Credentials": {
"SecurityToken": "exampleToken",
"AccessKeyId": "STS.exampleAccessKeyID",
"AccessKeySecret": "exampleAccessKeySecret",
"Expiration": "2024-06-12T03:21:29Z"
}
}Step 1: Configure basic parameters
The following code segment is from the js/script.js file in the WebDemo and calls the RecognizeBankCard service of Alibaba Cloud. You must replace <ALIBABA_CLOUD_ACCESS_KEY_ID>, <ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and <ALIBABA_CLOUD_SECURITY_TOKEN> in the code with the temporary AccessKeyId, AccessKeySecret, and SecurityToken from the STS token data.
The temporary AccessKey ID, AccessKey secret, and security token in this step prevent you from exposing your long-term credentials on the frontend. The temporary OSS STS token that you obtain in Step 2 is used only to upload the file to a temporary OSS bucket and retrieve the file URL.
/**
* For more information about how to obtain an Alibaba Cloud STS token, see https://help.aliyun.com/zh/oss/developer-reference/use-temporary-access-credentials-provided-by-sts-to-access-oss
* Replace <ALIBABA_CLOUD_ACCESS_KEY_ID>, <ALIBABA_CLOUD_ACCESS_KEY_SECRET>, and <ALIBABA_CLOUD_SECURITY_TOKEN> with the temporary AccessKeyId, AccessKeySecret, and SecurityToken from the Alibaba Cloud STS token data.
* You must grant the AliyunVIAPIFullAccess permission to the RAM user or RAM role. For more information, see https://help.aliyun.com/document_detail/145025.html
*/
//AccessKeyID
const accessKeyId = "<ALIBABA_CLOUD_ACCESS_KEY_ID>";
//AccessKeySecret
const accessKeySecret = "<ALIBABA_CLOUD_ACCESS_KEY_SECRET>";
//SecurityToken
const securityToken = "<ALIBABA_CLOUD_SECURITY_TOKEN>";Step 2: Call the GetOssStsToken operation to obtain a temporary OSS STS token
In the following code segment, the getOssStsToken function is used to obtain an STS token for OSS. This token is used to request temporary access permissions from Alibaba Cloud to upload files to OSS. The uploaded files are stored in the OSS bucket for one day. The request is constructed using the specified endpoint (API endpoint), Action (name of the operation to be performed), and API_VERSION (API version). The result is then returned through the callback function. The callApi function is used to call any API operation on the Alibaba Cloud platform and supports request authentication through signatures. The function accepts the endpoint (API endpoint), action (API operation name), version (API version), optional business parameters params, and a callback function. The function constructs a compliant request object request_, which includes system parameters, such as the signature method, timestamp, and AccessKey ID, and business parameters. The signNRandom function (implementation not provided) generates a unique random value (SignatureNonce) for the signature to prevent replay attacks. The getTimestamp function (implementation not provided) provides the current timestamp. Finally, the function calls the callApiRequest function (implementation not provided) to make the actual API call and returns the result through the callback.
For more information about the principles of this step, see File URL processing.
Step 3: Use the temporary OSS STS token to upload the file to the official Visual Intelligence API OSS bucket
The following code segment is a JavaScript implementation for uploading a file or URL resource to OSS, specifically to the official temporary storage bucket of the Visual Intelligence API. The uploaded file is valid for one day, which is convenient for debugging the API.
The following section describes the meaning and steps of this code segment:
Initialize the OSS client: Use the STS token to initialize the OSS client and specify the OSS region and bucket.
Generate the file path: Use the
getNoncefunction to generate a random string as part of the file name to ensure the uniqueness of the upload path.Upload the file: Use the
putmethod of the OSS client to upload the local file or the File object converted from a URL to OSS.NoteThe path for uploading the file to the OSS bucket is
accessKeyId/getNonce(6)/fileName.The
accessKeyIdin the file path is the<ALIBABA_CLOUD_ACCESS_KEY_ID>retrieved in Step 1: Configure basic parameters.
Process the response: After the upload is complete, the upload result is returned through a callback function. This typically includes the URL of the newly uploaded file. If an exception occurs during the upload process, an error message is printed.
This step requires you to import the OSS Browser.js SDK. For more information, see OSS Browser.js SDK.
Step 4: Use the returned OSS URL to call the Visual Intelligence API operation
The following code segment uses the returned OSS URL to call the Visual Intelligence API operation. The specific steps are the same as those in Scenario 1: The file is in an OSS bucket in the China (Shanghai) region. Each step of the code is encapsulated. For the complete example code, you can download the WebDemo.
Prepare the parameters for the API call:
Endpoint: The API endpoint. In this example, it is
ocr.cn-shanghai.aliyuncs.com.Action: The name of the API operation. In this example, it is
RecognizeBankCard, which indicates that the bank card recognition service is being called.API Version: The API version. In this example, it is
2019-12-30.Make sure to grant permissions to the RAM user or role to ensure that it has the permissions to call the Visual Intelligence API operations. For more information, see Grant permissions in the Prerequisites section.
Construct the request parameter object:
The
request_object contains the system parameters required for the API call, such as the signature method, timestamp, and format, and the business parameters. In this example, the business parameter is the URL of the bank card image to be recognized, which isImageURL.Initiate the API request:
Call the encapsulated
callApifunction. Pass the prepared request parameters, the API endpoint, and a callback function. The callback function is used to process the API response.
In the js/script.js file of the WebDemo, the endpoint, Action, and API_VERSION parameters of the callRecognizeBankCard function and the request_["ImageURL"] line represent the business parameters. For example, if you want to use the common segmentation feature, you can find in the common segmentation API documentation that this feature belongs to the image segmentation category (imageseg20191230) and the operation name is SegmentCommonImage. In this case, you can change the endpoint to imageseg.cn-shanghai.aliyuncs.com and the Action to SegmentCommonImage. Keep the API_VERSION as 2019-12-30 and the request_["ImageURL"] parameter name as ImageURL. When you receive the result, note that the ImageURL parameter is not a bank card number, but the URL of the segmented image.
/**
* ========================================================================================================================
* This is an example for RecognizeBankCard.
* For more information about how to recognize bank cards, see https://help.aliyun.com/document_detail/151893.html
* ========================================================================================================================
*/
function callRecognizeBankCard(imageUrl, callback) {
// The endpoint is the API endpoint, which is related to the category. For more information about API endpoints for different categories, see https://help.aliyun.com/document_detail/143103.html
const endpoint = "ocr.cn-shanghai.aliyuncs.com";
// The Action is the name of the operation. For more information about the Action parameter for a specific algorithm, see its API documentation. This example uses RecognizeBankCard: https://help.aliyun.com/document_detail/151893.html
const Action = "RecognizeBankCard";
// API_VERSION is the API version, which is related to the category. For more information about API versions for different categories, see https://help.aliyun.com/document_detail/464194.html
const API_VERSION = "2019-12-30";
const params = {};
// Business parameters. Modify them based on the API documentation for the specific AI capability.
params["ImageURL"] = imageUrl;
callApi(endpoint, Action, API_VERSION, params, callback);
}