Prepare
-
SDK Acquire
Download:ai-machine-java.zip. -
Register as Developer
First, please go to Become an AI platform developer website to learn how to become an AI platform developer
Second,please go to Create an Application ,and learn how to create an application ,then get the
accesskey,secretkey
Third, please go to white list to learn how to edit yurr white list
Forth, Go to Pipeline Designer and create a runnable aimodel(Users have two default available aimodels)and then get the
aimodelKey.
Quick Experience Demo
- Download the Java SDK Compression Toolkit on the [ai-script-java.zip].
- After unzipping the downloaded ai-script-java.zip, copy it to the project folder.
- In the IDEA tool, the “File - > Open” tool opens the decompressed ai-script-java project
- Check whether all MVN packages in pom.xml have been imported (ai-script-java.jar may need to be manually added to the local MVN repository).
- If you want to run an instance , find the file AiScriptJavaApplicationTests.java
Right-click, Debug contextLoads()
SDK Integration
- Import ai-script-java.jar to your project
- Change the
accesskey. secrectkey . aimodelKey to what you applied.
package com.lenovo.aiscript.sdk.aiscript;
import com.alibaba.fastjson.JSONObject;
import com.lenovo.aimachine.sdk.bean.*;
import com.lenovo.aimachine.sdk.client.AIMachineClient;
import com.lenovo.aimachine.sdk.common.FileData;
import com.lenovo.aimachine.sdk.exception.AIMachineException;
import com.lenovo.aimachine.sdk.utils.Base64EncodedUtil;
import com.lenovo.aimachine.sdk.utils.ReadPathUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AiScriptJavaApplicationTests {
public static final Logger logger = LoggerFactory.getLogger(AiScriptJavaApplicationTests.class);
@Test
public void contextLoads() {
//audio 需要的数据
String audioPath = "F:\\LX\\Text_Recognition\\TextRecognition\\src\\main\\resources\\test.wav";
FileData readFile = new FileData();
try {
readFile = ReadPathUtils.readFile(audioPath);
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = readFile.getBytes();
String base64Data = Base64EncodedUtil.encode(bytes);
Map<String, Object> map = new HashMap<>();
map.put("base64Data", base64Data);
map.put("sampleRate",16000);
map.put("bit", 16);
//image需要的数据
String imagePath = "F:\\LX\\Text_Recognition\\TextRecognition\\src\\main\\resources\\object.jpg";
FileData imageReadFile = new FileData();
try {
imageReadFile = ReadPathUtils.readFile(imagePath);
} catch (IOException e) {
e.printStackTrace();
}
byte[] imageBytes = imageReadFile.getBytes();
String imageBase64Data = Base64EncodedUtil.encode(imageBytes);
Map<String, Object> imageMap = new HashMap<>();
imageMap.put("base64Data", imageBase64Data);
imageMap.put("format", "jpg");
//json格式数据
JSONObject jsonObject = new JSONObject();
jsonObject.put("audio", new Audio(base64Data, SampleRate.matchSampleRate(16000), Bit.matchBit(16)));
jsonObject.put("image", new Image(imageBase64Data, ImageFormat.matchImageFormat("jpg")));
jsonObject.put("confidence", "0.5");
try {
//Your Accesskey and Secretkey
AIMachineClient AIMachineClient = new AIMachineClient("04B4C53D4851476FAEFF14F8234AA1B7", "42B247033DC470EBCC15312E91D6494D");
//方法一,传使用地址
String resultByPath = AIMachineClient.addAudioByPath("audio", audioPath)
.addImageByPath("image", imagePath, "jpg").addParam("confidence", "0.5")
//Your AIModelKey
.send("aimodelkey");
//方法二,传base64Data
String resultByBase64Data = AIMachineClient.addAudioByBase64Data("audio",base64Data)
.addImageByBase64Data("image",imageBase64Data,"jpg").addParam("confidence","0.5")
//Your AIModelKey
.send("aimodelkey");
//方法三,传map
String resultByMap = AIMachineClient.addAudioByMap("audio",map).addImageByMap("image",imageMap)
.addParam("confidence","0.5")
//Your AIModelKey
.send("aimodelkey");
//方法四,传二进制
String resultByByte = AIMachineClient.addAudioByBytes("audio",bytes).addImageByBytes("image",imageBytes,"jpg").addParam("confidence","0.5").send("aimodelkey");
//方法五,传json数据
String resultByJSON = AIMachineClient.addJSON(jsonObject).send("aimodelkey");
System.out.println("AIModel resultByPath is :"+resultByPath);
System.out.println("AIModel resultByBase64Data is :"+resultByBase64Data);
System.out.println("AIModel resultByMap is :"+resultByMap);
System.out.println("AIModel resultByByte is :"+resultByByte);
System.out.println("AIModel resultByJSON is :"+resultByJSON);
}catch (AIMachineException e){
e.printStackTrace();
}
}
}