Third-party API use

Third-party API use

1. Baidu AI-speech recognition & image search

Ready to work

  1. Enter http://ai.baidu.com/ in the browser to open the home page of Baidu AI development platform
    [External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-9VbLe3Lr-1614334876945) (C:\Users\qmm1998\AppData\Roaming\Typora\typora-user-images\ image-20210210132709265.png)]
    2) Click on the console in the upper right corner to enter the user registration login interface

3) Enter the management center

Speech Recognition

https://cloud.baidu.com/doc/SPEECH/s/pk4o0bkx8

The first step: select voice technology in the left menu

Step 2: Click Create Application on the main interface of speech recognition

Step 3: Create an application and fill in application information

Step 4: After the creation is successful, enter the application list to view
Insert picture description here

[Note: AppID, API Key, Secret Key must be used in the code]

Step 5: Click on the technical document in the left menu of the above figure

Short Speech Recognition Standard Edition-----------REST-API-JavaSDK---------Quick Start

Step 6: Open the voice quick start document according to the figure below

Step 7: Read the documentation for development and debugging

<!-------------------------添加依赖----------------------------->
        <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>4.15.3</version>
        </dependency>
/**************************测试代码*******************************/
public class SpeechTest {
    
    
    //设置APPID/AK/SK
      public static final String APP_ID = "";//你的APP_ID
    public static final String API_KEY = "";//你的API_KEY
    public static final String SECRET_KEY = "";//你的SECRET_KEY
    /*
     功能描述:语音识别
    */
    @Test
    public  void speechRecognition() throws JSONException {
    
    
        // 初始化一个AipSpeech
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
       client.setConnectionTimeoutInMillis(2000);
       client.setSocketTimeoutInMillis(60000);
        // 调用接口
        JSONObject res = client.asr("D:\\public\\8k.pcm", "pcm", 8000, null);
        System.out.println(res.toString(2));

    }
}

Run screenshot:
[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-LykBp3eu-1614334876952)(C:\Users\qmm1998\AppData\Roaming\Typora\typora-user-images\ image-20210210153233046.png)]

  • bug:

1) Interface call failed "err_msg": "request pv too much"

Solution: Need to receive free quota
Insert picture description here

Speech synthesis

https://cloud.baidu.com/doc/SPEECH/s/Mk4nlz5am

First, click on the top right corner of the page speech technology management console

2. Enter the voice technology homepage in the management console

Third, select Speech Technology Home Overview - Speech Synthesis

4. Receive free speech synthesis times

Fifth, click on the technical document to enter the voice technology API document

6. Open the speech synthesis API document
Insert picture description here
7. Read the document for development and debugging

bug:

bug1:
Insert picture description here

Solution: Introduce the latest version of java-sdk

 <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>4.15.3</version>
 </dependency>

8. Code

  /* 功能描述:语音合成
    */
    @Test
    public void speechSynthesis()throws JSONException {
    
    
        // 初始化一个AipSpeech
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        //System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        TtsResponse res = client.synthesis("你好小明", "zh", 1, null);
        byte[] data = res.getData();
        JSONObject res1 = res.getResult();
        if (data != null) {
    
    
            try {
    
    
                Util.writeBytesToFileSystem(data, "D:\\public\\cc.pcm");
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
        if (res1 != null) {
    
    
            System.out.println(res1.toString(2));
        }

    }

Image search

(1) Select image search from the menu on the left side of the console homepage

(2) Click to create application on the main interface of avatar search

(3) Fill in the application information

(4) Enter the application list to view

(5) Click "app name" to enter the application details page

(6) Apply to build a database

点击 应用名称------ 建库

(7) After the library is successfully built, import the picture

(8) Enter technical documents

(9) Read the documentation for development and testing

(10) Code test

package com.api.speech;

import com.baidu.aip.imagesearch.AipImageSearch;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.HashMap;

/**
 * @author 屈明明
 * @packagename com.api.speech
 * @create 2021-02-10 17:49
 * 百度AI-图像搜索
 */
public class ImgSearchTest {
    
    
    //设置APPID/AK/SK
    public static final String APP_ID = "";//你的APP_ID
    public static final String API_KEY = "";//你的API_KEY
    public static final String SECRET_KEY = "";//你的SECRET_KEY

    @Test
    public void imgSearch() throws JSONException {
    
    
        // 初始化一个AipImageSearch
        AipImageSearch client = new AipImageSearch(APP_ID, API_KEY, SECRET_KEY);

        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
        //client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        //client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
       // System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
        String path = "D:\\public\\7.jpg";//图片存储路径
        JSONObject res = client.productSearch(path, new HashMap<String, String>());
        System.out.println(res.toString(2));

    }
}




The code abuses me thousands of times, I regard the code as first love...

Guess you like

Origin blog.csdn.net/weixin_44490884/article/details/114145529