Call Youdao API to realize speech translation (Chinese to English)

1. Introduction of the author

Nan Xudong, male, 2022 graduate student, School of Electronic Information, Xi'an Polytechnic University
Research direction: Machine Vision and Artificial Intelligence
Email: [email protected]

Lu Zhidong, male, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student, Zhang Hongwei's artificial intelligence research group
Research direction: machine vision and artificial intelligence
Email: [email protected]

2. Related introduction

2.1 API introduction

API (Application Programming Interface) , the translation is the application programming interface, which is essentially some pre-defined functions. The popular explanation is that you need to achieve certain requirements, such as very hot, I want the fan to turn on, so that I don’t get hot, I know that turning the fan requires electricity, but I don’t know how to generate electricity, and I don’t want to study How to generate electricity, I just want to use ready-made ones, and the power station also knows that I am so lazy and stupid, so they built an interface for me, and told me that you only need to plug the fan plug into this interface, and you can use the power station to produce When the electricity is turned on, the fan will turn on. Putting it into the code writing part is, for example, I want to implement something, the effect is to generate a window, I will not start from 0, and I don’t want to study how to start from 0, I just want to call a function, give this function Adding some parameters can realize the size of the window I want. At this time, the so-called api is this function.

2.2 Introduction to Netease API

Youdao Translation API provides an open interface for developers. Your application or website can use Youdao Translation API to build a variety of functions or applications to bring users an instant, accurate and convenient word search or translation experience, thereby lowering the threshold for language understanding and application.
insert image description here

3. Experimental procedure

3.1 Call process

1. Search Youdao Zhiyun API , click to enter
insert image description here
2. Click to log in
insert image description here
3. After logging in, click your avatar to enter the personal center .
insert image description here
4. Select "Natural Language Translation Service" in the left column , and then click Voice Translation . 5. Click Create Application
insert image description here
in the upper right corner 6. The options are shown in Figure 7. After the creation is complete, select your own ID and YEK
insert image description here

insert image description here

insert image description here

3.2 Code acquisition

1. Come to the home page again, download and call the code file package in the order shown in the figure
insert image description here
insert image description here
insert image description here
2. Get the folders shown in the figure below, and open them in turn
insert image description here
3.Prepare your own audio file, which cannot be larger than 2M and must be a voice file in WAV format
Here is a speech format conversion website: link: https://www.pdf365.cn/?agent=ck15

3.3 Complete code

import base64

import requests

from utils.AuthV3Util import addAuthParams

# 您的应用ID
APP_KEY = ''
# 您的应用密钥
APP_SECRET = ''

# 待翻译语音路径, 例windows路径:PATH = "C:\\youdao\\media.wav"
PATH = ''

def createRequest():
    '''
    note: 将下列变量替换为需要请求的参数
    取值参考文档: https://ai.youdao.com/DOCSIRMA/html/%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E7%BF%BB%E8%AF%91/API%E6%96%87%E6%A1%A3/%E5%9B%BE%E7%89%87%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1/%E5%9B%BE%E7%89%87%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html
    '''
    lang_from = 'en'
    lang_to = 'zh-CHS'
    format = 'wav'
    rate = '16000'

    # 数据的base64编码
    q = readFileAsBase64(PATH)
    data = {'q': q, 'from': lang_from, 'to': lang_to, 'format': format, 'rate': rate, 'channel': '1', 'type': '1'}

    addAuthParams(APP_KEY, APP_SECRET, data)

    header = {'Content-Type': 'application/x-www-form-urlencoded'}
    res = doCall('https://openapi.youdao.com/speechtransapi', header, data, 'post')
    print(str(res.content, 'utf-8'))

def doCall(url, header, params, method):
    if 'get' == method:
        return requests.get(url, params)
    elif 'post' == method:
        return requests.post(url, params, header)

def readFileAsBase64(path):
    f = open(path, 'rb')
    data = f.read()
    return str(base64.b64encode(data), 'utf-8')

# 网易有道智云语音翻译服务api调用demo
# api接口: https://openapi.youdao.com/speechtransapi
if __name__ == '__main__':
    createRequest()

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/131321536