音频转文字,搜索关键词ok:
# -*- coding: utf-8 -*-
# 使用讯飞语音转写API进行音频转文字#
import base64
import hashlib
import hmac
import json
import os
import time
import requests
import urllib
import re
import paddlehub as hub
mode = hub.Module(name='auto_punc', version='1.0.0')
lfasr_host = 'https://raasr.xfyun.cn/v2/api'
# 请求的接口名
api_upload = '/upload'
api_get_result = '/getResult'
class RequestApi(object):
def __init__(self, appid, secret_key, upload_file_path):
self.appid = appid
self.secret_key = secret_key
self.upload_file_path = upload_file_path
self.ts = str(int(time.time()))
self.signa = self.get_signa()
def get_signa(self):
appid = self.appid
secret_key = self.secret_key
m2 = hashlib.md5()
m2.update((appid + self.ts).encode('utf-8'))
md5 = m2.hexdigest()
md5 = bytes(md5, encoding='utf-8')
# 以secret_key为key, 上面的md5为msg, 使用hashlib.sha1加密结果为signa
signa = hmac.new(secret_key.encode('utf-8'), md5, hashlib.sha1).digest()
signa = base64.b64encode(signa)
signa = str(signa, 'utf-8')
return signa
def upload(self):
upload_file_path = self.upload_file_path
file_len = os.path.getsize(upload_file_path)
file_name = os.path.basename(upload_file_path)
param_dict = {}
param_dict['appId'] = self.appid
param_dict['signa'] = self.signa
param_dict['ts'] = self.ts
param_dict["fileSize"] = file_len
param_dict["fileName"] = file_name
param_dict["duration"] = "200"
data = open(upload_file_path, 'rb').read(file_len)
response = requests.post(url =lfasr_host + api_upload+"?"+urllib.parse.urlencode(param_dict),
headers = {"Content-type":"application/json"},data=data)
result = json.loads(response.text)
return result
def get_result(self):
uploadresp = self.upload()
orderId = uploadresp['content']['orderId']
param_dict = {}
param_dict['appId'] = self.appid
param_dict['signa'] = self.signa
param_dict['ts'] = self.ts
param_dict['orderId'] = orderId
param_dict['resultType'] = "transfer,predict"
status = 3
# 建议使用回调的方式查询结果,查询接口有请求频率限制
while status == 3:
response = requests.post(url=lfasr_host + api_get_result + "?" + urllib.parse.urlencode(param_dict),
headers={"Content-type": "application/json"})
result = json.loads(response.text)
status = result['content']['orderInfo']['status']
if status == 4:
break
time.sleep(5)
#print("get_result resp:",result)
return result
#这里是音乐文件的文件夹
dir_path = r'D:\\pythonProject13\\mp3\\'
fileList=[]
# 输入讯飞开放平台的appid,secret_key和待转写的文件路径
if __name__ == '__main__':
keyString=['微信','老爸']
fileList=os.listdir(dir_path)
for fileName in fileList:
time.sleep(2)
print("正在识别:"+fileName)
dir=dir_path+fileName
api = RequestApi(appid="1485995d",
secret_key="a5fe900e4baff4234241690022143024",
upload_file_path=dir)
res=api.get_result()
obj=res['content']['orderResult']
pattern = re.compile("[\u4e00-\u9fff]+")
# 用正则匹配所有的汉字
matches = pattern.findall(str(obj))
sencent = ""
for i in range(len(matches)):
sencent += matches[i]
print(sencent)
punc_texts = ""
m = (len(sencent)/10)
for i in range(10):
punc_texts += mode.add_puncs(sencent[int(i*m):int(i*m+m)])[0]
with open("202303292.txt","w") as f:
f.write(punc_texts)
for key in keyString:
if(key in obj):
print(fileName+"--检测到关键字【"+key+"】")
|