Skip to content

基于baidu-api的语音识别转写+情感倾向分析的简单桌面应用。

Notifications You must be signed in to change notification settings

jujimeizuo/Music_Label

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Music_label

音频文件转写

Baidu Python Demo 点击下载

(该demo为百度自己写的,可以将其规整一下。)

1. 事前准备工作

百度账号!

1.1 账号的注册登录

img

1.2 选择产品服务

通过控制台左侧导航,选择产品服务-人工智能,进入具体AI服务项的控制面板(如文字识别、人脸识别),进行相关业务操作。

img

2. 创建语音识别应用

在控制台中,创建应用,勾选开通”语音技术“-”音频文件转写“能力。获取 AppID、API Key、Secret Key,并通过请求鉴权接口换取 token.

img

img

{
		"AppID": "******",
  	"API Key": "******",
  	"Secret Key": "******"
}

img

{
    "refresh_token": "25.35264e084797bc0bf19dd706168455a4.315360000.1949116227.282335-24960897",
    "expires_in": 2592000,
    "session_key": "9mzdDAE89vAYH2jOTi8J2UuSelhHulALOvtk9H3rBU4CgQu0hV+Jn6QfZ/dsGC5rnCEZZTYKN7xa/ryvBO3dTEZUC75ixw==",
    "access_token": "******",
    "scope": "audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test",
    "session_secret": "98507c8a97e59b8af784af8ff8d476f0"
}

3. 接口调用

接口描述:根据音频url、音频格式、语言id以及采样率等参数创建音频转写任务

请求接口:https://aip.baidubce.com/rpc/2.0/aasr/v1/create(POST)

KEY VALUE
access_token 通过 API Key 和 Secret Key 获取的 access_token,参考Access Token获取

Body中放置请求参数,参数如下:

参数名 类型 是否必需 对外状态 取值范围
speech_url str 音频url 可使用百度云对象存储进行音频存储,生成云端可外网访问的url链接,音频大小不超过500MB
format str 音频格式 ["mp3", "wav", "pcm","m4a","amr"]单声道,编码 16bits 位深
pid int 语言类型 [80001(中文语音近场识别模型极速版), 1737(英文模型)]
rate int 采样率 [16000] 固定值

Body请求示例:

{
    "speech_url": "https://platform.bj.bcebos.com/sdk%2Fasr%2Fasr_doc%2Fdoc_download_files%2F16k.pcm",
    "format": "pcm",
    "pid": 80001,
    "rate": 16000
}

返回参数

参数名 类型 是否必需 对外状态
log_id int log id
task_id str 任务id
task_status str 任务状态
error_code int 错误码
error_msg str 错误信息

Body返回示例:

# 创建成功
{
    "log_id": 12345678,
    "task_status": "Created"
    "task_id":  "234acb234acb234acb234acb"  #注意保存该id,用于后续请求识别结果
}
# 创建失败,缺少参数
{
    "error_code": 336203,
    "error_msg": "missing param: speech_url",
    "log_id": 5414433131138366128
}

注意:查询识别结果时,需要该步骤返回的task_id来进行请求。请注意保存task_id列表。

img

根据task_id的数组批量查询音频转写任务结果

请求接口:https://aip.baidubce.com/rpc/2.0/aasr/v1/query (POST)

KEY VALUE
access_token 通过 API Key 和 Secret Key 获取的 access_token,参考Access Token获取

Body中放置请求参数,参数如下:

参数名 类型 是否必需 描述 取值范围
task_ids list 任务id task_ids为空,返回空任务结果列表;单次查询任务数不超过200个

body请求示例:

{
    "task_ids":  ["234acb234acb234acb234acb", "234acb234acb234acb234acd", "234acb234acb234acb234acbe"]
}

返回参数:

参数名 类型 是否必需
log_id int
tasks_info list
+task_id str
+task_status str
+task_result dict
++corpus_no str
++result str
++detailed_result list
++err_no int
++err_msg str
++sn str
error_code int
error_msg str
error_info list

Body返回示例:

{
    "log_id": 12345678,
    "tasks_info": [
	    { # 转写中
	    	"task_status": "Running"
		    "task_id": "234acb234acb234acb234acb",
	    },
	    { # 转写失败
	    	"task_status": "Failure"
		    "task_id": "234acb234acb234acb234acd",
		    "task_result": {
			    "err_no":  3301
			    "err_msg": "speech quality error",
			    "sn": "xxx"
		    }
	    },
	    { # 转写成功
		    "task_status": "Success",
		    "task_result": {
		    	"result": [
			        "观众朋友大家好,欢迎收看本期视频哦。毕竟..."
			    ],
                "audio_duration": 6800,
			    "detailed_result": [
			        {
			        	"res": [
			                "观众朋友大家好,欢迎收看本期视频哦。"
			            ],
			            "end_time": 6700,
			            "begin_time": 4240,
			            "words_info": [],
			            "sn": "257826606251573543780",
			            "corpus_no": "6758319075297447880"
			        }
			        ...
			    ],
			    "corpus_no": "6758319075297447880" 
			},
			"task_id": "234acb234acb234acb234ace"
	    }
	]
}

img

img

4. 出现的问题

4.1 speech_url必须是音频url

用户必须要将音频文件上传至云端,这样接口才能正确

错误示例:

将音频文件放在window桌面下。

请求接口:正确返回tasks_id。

img

BUT!

img

解决方案:

可使用百度云对象存储进行音频存储,生成云端可外网访问的url链接,音频大小不超过500MB。

创建个Bucket,上传文件,会生成一个外网访问的url链接。

img

4.2 音频文件不对应

可以使用音频文件转码

自然语言情感分析

1. baidu-aip

Help on package aip:

NAME
    aip - aip public

PACKAGE CONTENTS
    base
    bodyanalysis
    easydl
    face
    imagecensor
    imageclassify
    imageprocess
    imagesearch
    kg
    nlp
    ocr
    speech

1.1 nlp

nlp全称Natural Language Processing,自然语言处理。

所以需要查看nlp里面有什么。

1.2 aip-nlp

NAME
    aip.nlp - 自然语言处理

CLASSES
    aip.base.AipBase(builtins.object)
        AipNlp
    
    class AipNlp(aip.base.AipBase)
     |  AipNlp(appId, apiKey, secretKey)
     |  
     |  自然语言处理
     |  
     |  Method resolution order:
     |      AipNlp
     |      aip.base.AipBase
     |      builtins.object
     |  
     |  Methods defined here:
     |  
     |  commentTag(self, text, options=None)
     |      评论观点抽取
     |  
     |  depParser(self, text, options=None)
     |      依存句法分析
     |  
     |  dnnlm(self, text, options=None)
     |      DNN语言模型
     |  
     |  ecnet(self, text, options=None)
     |      文本纠错
     |  
     |  emotion(self, text, options=None)
     |      对话情绪识别接口
     |  
     |  keyword(self, title, content, options=None)
     |      文章标签
     |  
     |  lexer(self, text, options=None)
     |      词法分析
     |  
     |  lexerCustom(self, text, options=None)
     |      词法分析(定制版)
     |  
     |  newsSummary(self, content, max_summary_len, options=None)
     |      新闻摘要接口
     |  
     |  sentimentClassify(self, text, options=None)
     |      情感倾向分析
     |  
     |  simnet(self, text_1, text_2, options=None)
     |      短文本相似度
     |  
     |  topic(self, title, content, options=None)
     |      文章分类
     |  
     |  wordEmbedding(self, word, options=None)
     |      词向量表示
     |  
     |  wordSimEmbedding(self, word_1, word_2, options=None)
     |      词义相似度
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from aip.base.AipBase:
     |  
     |  __init__(self, appId, apiKey, secretKey)
     |      AipBase(appId, apiKey, secretKey)
     |  
     |  getVersion(self)
     |      version
     |  
     |  post(self, url, data, headers=None)
     |      self.post('', {})
     |  
     |  report(self, feedback)
     |      数据反馈
     |  
     |  setConnectionTimeoutInMillis(self, ms)
     |      setConnectionTimeoutInMillis
     |  
     |  setProxies(self, proxies)
     |      proxies
     |  
     |  setSocketTimeoutInMillis(self, ms)
     |      setSocketTimeoutInMillis
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from aip.base.AipBase:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)

查看上图,情感分析为sentimentClassify(self, text, options=None)

2. 如何使用aip.nlp

2.1 创建应用

img

img

img

{
	"client_appId" = "25004504",
	"client_apiKey" = "******",
	"client_secretKey" = "******"
}

2.2 具体代码实现

import aip

client_appId = '25004504'
client_apiKey = '******'
client_secretKey = '******'

lyrics = '今天天气很好'
print(lyrics)

# 调用自然语言处理api
my_nlp = aip.nlp.AipNlp(client_appId, client_apiKey, client_secretKey)

print(my_nlp.sentimentClassify(lyrics))

# sentimentClassify(self, text, options=None)

1. PyQt5下载

使用豆瓣提供的镜像服务。

1.1 命令行安装

1.1.1 pip install PyQt5 -i https://pypi.douban.com/simple

1.1.2 pip install PyQt5-tools -i https://pypi.douban.com/simple

img

安装完成后在Python安装目录下的Lib\site-packages目录中可以看到PyQt5、pyqt5-tools目录。

img

1.2 Pycharm安装

imgimg

img

1.3 使用PyQt

在pycharm中新建一个python文件,然后在其他写入代码:

import sys
from PyQt5.QtWidgets import QWidget, QApplication

app = QApplication(sys.argv)
widget = QWidget()
widget.resize(800, 600)
widget.setWindowTitle("Hello, PyQt5!")
widget.show()
sys.exit(app.exec())

img

2. PyQt5在Pycharm中配置

File->Settings->Tools->External Tools.

工具 *.exe 说明
Qt Designer ./designer.exe 打开Qt Designer界面,对软件的界面进行设计
PyUIC ./python.exe 将Qt Designer设计的UI文件转换为,py文件
PyRCC ./pyrcc5.exe 将资源文件如图片等转成python代码能识别的文件

img

2.1 添加Qt Designer

img

2.2 添加PyUIC

img

2.3 添加PyRCC

img

2.4 成品展示

img

因为每个人安装配置不同,对于找不到.exe文件可以自行百度。

3. PyQt设计界面

img

本来想要美化界面,突然想要透明化是不是挺好,然后就是上图孤零零的Upload。

4. 集成.py文件

4.1 各文件

img

4.2 大致流程

img

5. 缺点

5.1 API调用速度慢

由于每次调用api时,会返回一个状态,例如Running,但是之后并不会返回result,所以需要调用多次直到状态为Success,这里设置sleep(7)

if dict_json['tasks_info'][0]['task_status'] == 'Success' :
	my_txt = dict_json['tasks_info'][0]['task_result']['result'][0]

	print(my_txt)

	with open('../speech_txt/lyrics.txt', "w", encoding='utf-8') as f :
		f.write(my_txt)
		emotion_analysis.emo()

elif dict_json['tasks_info'][0]['task_status'] == 'Running':
	time.sleep(7)
	query() # 递归调用

5.2 健壮性不强

由于开发时间有限,程序每次只能运行一个文件,但是可以运行多个文件,后续有时间会补上。

5.3 结果不准确

这个准确度控制不了,因为是调用的百度的nlp。

6. 完结撒花

结对作业bye。

About

基于baidu-api的语音识别转写+情感倾向分析的简单桌面应用。

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages