aimachinePrepare

本文详细介绍了一种AI平台的SDK集成步骤与实践案例,包括下载SDK压缩包、注册成为开发者、获取密钥、创建AI模型及快速体验演示。通过具体代码示例,展示了如何在Java项目中使用SDK进行音频和图像数据的处理,为开发者提供了全面的指导。
Prepare

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();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值