示例#1
0
文件: tengxun.py 项目: LGG2016/asr
 def GetAsrResult(self):
     #本地文件方式请求
     try:
         cred = credential.Credential(self.SecretID, self.SecretKey)
         httpProfile = HttpProfile()
         httpProfile.endpoint = self.Url
         clientProfile = ClientProfile()
         clientProfile.httpProfile = httpProfile
         clientProfile.signMethod = "TC3-HMAC-SHA256"
         client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile)
         #读取文件以及 base64
         with open(self.AudioFile, "rb") as f:
             audio = f.read()
             if self.Format == 'pcm':
                 audio = utils.PcmToWav(audio)
             if sys.version_info[0] == 2:
                 content = base64.b64encode(audio)
             else:
                 content = base64.b64encode(audio).decode('utf-8')
         #发送请求
         req = models.SentenceRecognitionRequest()
         params = {"ProjectId": 0, "SubServiceType": 2, "SourceType": 1, "UsrAudioKey": "session-123"}
         req._deserialize(params)
         req.DataLen = len(content)
         req.Data = content
         req.EngSerViceType = "16k_zh"
         req.VoiceFormat = "wav"
         req.FilterPunc = 2 #是否过滤标点符号(目前支持中文普通话引擎)。 0:不过滤,1:过滤句末标点,2:过滤所有标点。默认为0。
         req.ConvertNumMode = 0 #是否进行阿拉伯数字智能转换。0:不转换,直接输出中文数字,1:根据场景智能转换为阿拉伯数字。默认值为1
         resp = client.SentenceRecognition(req)
         logger.info("file:%s, result:%s, sid:%s", self.AudioFile, resp.Result, resp.RequestId)
         return resp.Result
     except TencentCloudSDKException as err:
         logger.error("code:%s, message:%s, sid:%s", err.code, err.message, err.requestId)
         return ""
示例#2
0
    def get_results(self, task_id, ind):
        try:
            print("Getting result for %s" % self.temp_data["file_path"][ind])
            cred = credential.Credential(self._secret_id, self._secret_key)
            httpProfile = HttpProfile()
            httpProfile.endpoint = self._endpoint

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = asr_client.AsrClient(cred, "", clientProfile)

            req = models.DescribeTaskStatusRequest()
            params = {"TaskId": task_id}
            req.from_json_string(json.dumps(params))
            seconds = 0
            while True:
                time.sleep(1)
                seconds += 1
                resp = client.DescribeTaskStatus(req)
                result_json = json.loads(resp.to_json_string())
                result_status = result_json["Data"]["StatusStr"]
                if result_status in ["success", "failed"]:
                    break
                print(seconds)
            result_data = result_json["Data"]["ResultDetail"]
            result_data_str = json.dumps(result_data, ensure_ascii=False)
            self.temp_data["result_data"].append(result_data_str)

        except TencentCloudSDKException as err:
            print(err)
示例#3
0
    def stt(self, audio_file):
        try:
            fwave = open(audio_file, mode='rb')
            data = fwave.read()
            dataLen = len(data)
            base64Wav = base64.b64encode(data)
            fwave.close()

            params = '{"ProjectId":0,"SubServiceType":2,"EngSerViceType":"16k","SourceType":1,"VoiceFormat":"mp3","UsrAudioKey":"session-123", ' + '"Data":"' + str(
                base64Wav, 'utf-8') + '", "DataLen":' + str(dataLen) + '}'

            cred = credential.Credential(self.app_id, self.app_key)
            httpProfile = HttpProfile()
            httpProfile.endpoint = self.api_url

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = asr_client.AsrClient(cred, "ap-chengdu", clientProfile)

            req = models.SentenceRecognitionRequest()

            req.from_json_string(params)
            res = client.SentenceRecognition(req)
            resp = json.loads(res.to_json_string())

            return resp['Result']
        except TencentCloudSDKException as err:
            ASRServerError(err, "404")
示例#4
0
def get(Taskid):
    try:
        cred = credential.Credential(config['tenxun_api']['secretId'],
                                     config['tenxun_api']['secretKey'])
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = asr_client.AsrClient(cred, "", clientProfile)

        req = models.DescribeTaskStatusRequest()
        params = {"TaskId": Taskid}
        req.from_json_string(json.dumps(params))

        resp = json.loads(client.DescribeTaskStatus(req).to_json_string())
        if (resp['Data']['StatusStr'] == "success"):
            if (resp['Data']['Result']):
                #print(resp['Data']['Result'])
                resp_e = resp['Data']['Result'].split(' ')[-1]

                return resp_e
            else:
                return "什么都没有听到呢"
        else:
            return get(Taskid)
    except TencentCloudSDKException as err:
        get(Taskid)
示例#5
0
def send(mid, now_time):
    try:
        file = open("voice/rep/{}-{}-end.wav".format(mid, now_time),
                    "rb").read()
        data = base64.b64encode(file).decode('utf-8')
        cred = credential.Credential(config['tenxun_api']['secretId'],
                                     config['tenxun_api']['secretKey'])
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = asr_client.AsrClient(cred, "", clientProfile)

        req = models.CreateRecTaskRequest()
        params = {
            "EngineModelType": "16k_zh",
            "ChannelNum": 1,
            "ResTextFormat": 0,
            "SourceType": 1,
            "Data": data
        }
        req.from_json_string(json.dumps(params))

        resp = json.loads(client.CreateRecTask(req).to_json_string())
        return resp['Data']['TaskId']

    except TencentCloudSDKException as err:
        print(err)
        send(mid, now_time)
示例#6
0
    def __init__(self):
        self.cred = credential.Credential(ID, KEY)
        self.httpProfile = HttpProfile()
        self.httpProfile.endpoint = "asr.tencentcloudapi.com"

        self.clientProfile = ClientProfile()
        self.clientProfile.httpProfile = self.httpProfile
        self.clientProfile.signMethod = "TC3-HMAC-SHA256"  # 签名方法v3
        self.client = asr_client.AsrClient(self.cred, "", self.clientProfile)
示例#7
0
def recognize_wav(wav_path, show_detail=True):
    # 通过本地语音上传方式调用
    try:

        # 重要:<Your SecretId>、<Your SecretKey>需要替换成用户自己的账号信息
        # 请参考接口说明中的使用步骤1进行获取。
        cred = credential.Credential(tencent_secret_id, tencent_secret_key)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        clientProfile.signMethod = "TC3-HMAC-SHA256"
        client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile)

        # 读取文件以及base64
        with reader.SaveOpen(to_mp3(wav_path), 'rb') as fwave:
            data = str(fwave.read())
        dataLen = len(data)
        base64Wav = base64.b64encode(data)

        # 发送请求
        req = models.SentenceRecognitionRequest()
        params = {
            "ProjectId": 0,
            "SubServiceType": 2,
            "EngSerViceType": "16k_en",
            "SourceType": 1,
            "Url": "",
            "VoiceFormat": "mp3",
            "UsrAudioKey": "session-123",
            "Data": base64Wav,
            "DataLen": dataLen
        }
        req._deserialize(params)
        resp = client.SentenceRecognition(req)
        if show_detail:
            print(resp.to_json_string())
        # windows系统使用下面一行替换上面一行
        # print(resp.to_json_string().decode('UTF-8').encode('GBK') )

        words = []

        for w in resp.Result.split():
            if not re.match('[a-zA-W]', w[-1:]):
                w = w[0:-1]
            words.append(w)
        return ' '.join(words).lower()

    except TencentCloudSDKException as err:
        print(err)
        return '[ERROR]'
 def __init__(self, keys: Dict[str, str]):
     """
     Arguments:
         keys {Dict[str, str]} -- authorization keys
         requires "secret_id", "secret_key"
     """
     cred = credential.Credential(keys['secret_id'], keys['secret_key'])
     httpProfile = HttpProfile()
     httpProfile.endpoint = "asr.tencentcloudapi.com"
     clientProfile = ClientProfile()
     clientProfile.httpProfile = httpProfile
     clientProfile.signMethod = "TC3-HMAC-SHA256"
     self.client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile)
     self.lang = keys.get('lang', 'zh')
示例#9
0
    def __init__(self):

        #####################################  以下为自定义设置区 ##########################################
        self.my_id = ''   # 自己的腾讯云secretId
        self.my_key = ''      # 自己的腾讯云secretKey

        #####################################  以上为自定义设置区 ##########################################

        cred = credential.Credential(self.my_id, self.my_key)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        clientProfile.signMethod = "TC3-HMAC-SHA256"

        self.client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile)
示例#10
0
    def sentence_asr(self):
        # 本地文件方式请求
        try:
            cred = credential.Credential(self.SecretId, self.SecretKey)
            httpProfile = HttpProfile()
            httpProfile.endpoint = "asr.tencentcloudapi.com"
            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            clientProfile.signMethod = "TC3-HMAC-SHA256"
            client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile)
            # 读取文件以及 base64
            # 此处可以下载测试音频 https://asr-audio-1300466766.cos.ap-nanjing.myqcloud.com/test16k.wav
            with open(self.data, "rb") as f:
                content = base64.b64encode(f.read()).decode('utf-8')
            # 发送请求
            req = models.SentenceRecognitionRequest()
            if self.type:
                params = {
                    "ProjectId": 0,
                    "SubServiceType": 2,
                    "SourceType": 1,
                    "UsrAudioKey": "session-123"
                }
            else:
                params = {
                    "ProjectId": 0,
                    "SubServiceType": 2,
                    "SourceType": 0,
                    "UsrAudioKey": "session-123"
                }
                req.url = self.data

            req._deserialize(params)
            req.EngSerViceType = "16k_zh"
            req.VoiceFormat = "wav"
            if self.type:
                req.DataLen = len(content)
                req.Data = content

            resp = client.SentenceRecognition(req)
            res = resp.to_json_string()
            res = json.loads(res)['Result']
            res = res.replace('。', '').replace(',', '').replace(',', '')
            return res
        except TencentCloudSDKException as err:
            print('tencent返回错误:%s' % err)
示例#11
0
文件: asr.py 项目: SCys/GoalKeepr
async def tx_asr_result(task_id: str) -> str:
    """腾讯云 ASR"""

    config = manager.config
    id = config["asr"]["tx_id"]
    key = config["asr"]["tx_key"]

    while True:
        try:
            cred = credential.Credential(id, key)
            httpProfile = HttpProfile()
            httpProfile.endpoint = "asr.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = asr_client.AsrClient(cred, "", clientProfile)

            req = models.DescribeTaskStatusRequest()
            req.from_json_string(dumps({"TaskId": task_id}))

            resp = client.DescribeTaskStatus(req)
            data = resp._serialize(allow_none=True)["Data"]
            status = data["StatusStr"]

            if status == "success":
                # detail = data["ResultDetail"]
                result = data["Result"]
                # duration = data["AudioDuration"]

                logger.info(f"tencent cloud sdk asr is done: {task_id} {result}")
                return result.strip()
            elif status == "failed":
                logger.error(f'task status failed: {data["ErrorMsg"]}')
            else:
                logger.warning(f"task status is {status}")
                continue

        except TencentCloudSDKException as err:
            print(err)

        break
示例#12
0
文件: asr.py 项目: SCys/GoalKeepr
async def tx_asr(audio_data: str) -> str:
    """腾讯云 ASR"""
    config = manager.config
    id = config["asr"]["tx_id"]
    key = config["asr"]["tx_key"]

    try:
        cred = credential.Credential(id, key)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = asr_client.AsrClient(cred, "", clientProfile)

        req = models.CreateRecTaskRequest()
        req.from_json_string(
            dumps(
                {
                    "EngineModelType": "16k_zh",
                    "ChannelNum": 1,
                    "SpeakerDiarization": 0,
                    "SpeakerNumber": 1,
                    "ResTextFormat": 2,
                    "SourceType": 1,
                    "Data": audio_data,
                }
            )
        )

        resp = client.CreateRecTask(req)
        data = resp._serialize(allow_none=True)

        logger.info(f"tencent cloud sdk asr is done: {data['Data']}")
        return data["Data"]["TaskId"]
    except TencentCloudSDKException:
        logger.exception("tencent cloud sdk error")
示例#13
0
    def upload_audio(self, file_path):
        print("Uploading %s" % file_path)
        try:
            audio_path = file_path
            with open(audio_path, "rb") as f:
                audio_data = f.read()
            audio_data_b64 = base64.b64encode(audio_data).decode("utf-8")
            cred = credential.Credential(self._secret_id, self._secret_key)
            httpProfile = HttpProfile()
            httpProfile.endpoint = self._endpoint

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = asr_client.AsrClient(cred, "", clientProfile)

            req = models.CreateRecTaskRequest()
            params = {
                "EngineModelType": "16k_zh_video",
                "ChannelNum": 1,
                "SpeakerDiarization": 1,
                "SpeakerNumber": 1,
                "ResTextFormat": 2,
                "SourceType": 1,
                "Data": audio_data_b64,
            }
            req.from_json_string(json.dumps(params))

            resp = client.CreateRecTask(req)
            result_json = json.loads(resp.to_json_string())
            task_id = result_json["Data"]["TaskId"]
            self.temp_data["file_path"].append(audio_path)
            self.temp_data["task_id"].append(task_id)
            print("Upload %s success" % file_path)

        except TencentCloudSDKException as err:
            print(err)
示例#14
0
import io 
import sys 
if sys.version_info[0] == 3:
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')

#本地文件方式请求
try: 
    #重要,此处<Your SecretId><Your SecretKey>需要替换成客户自己的账号信息,获取方法:
        #https://cloud.tencent.com/product/asr/getting-started
    cred = credential.Credential("AKIDX1GGzR5U9avzoPSmfKl79uNaaQ17nK2T", "IJ9l1D9oBH7oHWhbEaeS6VK4XjdKkz0t") 
    httpProfile = HttpProfile()
    httpProfile.endpoint = "asr.tencentcloudapi.com"
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    clientProfile.signMethod = "TC3-HMAC-SHA256"  
    client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile) 
    #读取文件以及 base64
    #此处可以下载测试音频 https://asr-audio-1300466766.cos.ap-nanjing.myqcloud.com/test16k.wav
    with open('./12.mp3', "rb") as f:
        if sys.version_info[0] == 2:
            content = base64.b64encode(f.read())
        else:
            content = base64.b64encode(f.read()).decode('utf-8')
    #发送请求
    req = models.SentenceRecognitionRequest()
    params = {"ProjectId":0,"SubServiceType":2,"SourceType":1,"UsrAudioKey":"session-123", "RequestId": '1c8400f1-913f-4938-a45f-0421b08782ad' }
    req._deserialize(params)
    req.DataLen = len(content)
    req.Data = content
    req.EngSerViceType = "16k_zh"
    req.VoiceFormat = "wav"
def video_to_text(video_url, video_id):
    """
    input:
        video_url
        video_id

    output:
        results 
            format: [{time_start:xxx,time_end:xxx,text:xxx,vid:xxx},...]
    """
    from tencentcloud.common import credential
    from tencentcloud.common.profile.client_profile import ClientProfile
    from tencentcloud.common.profile.http_profile import HttpProfile
    from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
    from tencentcloud.asr.v20190614 import asr_client, models
    import base64
    import io
    import time
    import sys
    if sys.version_info[0] == 3:
        sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

    # post the audio to tencent cloud platform
    try:
        # <Your SecretId><Your SecretKey> of tencent cloud
        cred = credential.Credential(
            "AKID1w0sUvY1A4nQyJC5O1PXP43qMimcZNCY", "LWSHzeGgrmQNBk4MOiqiRu40at7TYNJg")
        # set http request
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        clientProfile.signMethod = "TC3-HMAC-SHA256"
        client = asr_client.AsrClient(cred, "ap-beijing", clientProfile)

        # set params and create recognition task
        req = models.CreateRecTaskRequest()
        params = {"ChannelNum": 1, "ResTextFormat": 0, "SourceType": 0}
        req._deserialize(params)
        req.EngineModelType = "16k_en"
        req.Url = video_url
        resp = client.CreateRecTask(req)

        # set result query
        req = models.DescribeTaskStatusRequest()
        params = '{"TaskId":%s}' % resp.Data.TaskId
        req.from_json_string(params)

        # polling
        resp = client.DescribeTaskStatus(req)
        while resp.Data.Status != 2:
            print(111)
            if 'faild' in resp.Data.StatusStr:
                break
            print(222)
            print("video ID: " + str(video_id) + " is " +
                  resp.Data.StatusStr, flush=True)
            time.sleep(1)
            resp = client.DescribeTaskStatus(req)

        # process the result
        print("video ID: " + str(video_id) + " is " +
              resp.Data.StatusStr, flush=True)
        results = []
        lines = resp.Data.Result.split('\n')
        for line in lines:
            if len(line) < 1:
                continue
            time, text = line.split(']')[0][1:], line.split(']')[1][2:-1]
            result_dict = {}
            result_dict["time_start"], result_dict[
                "time_end"] = time.split(',')[0], time.split(',')[1]
            result_dict["text"] = text
            result_dict["vid"] = video_id
            results.append(result_dict)

        return results

    except TencentCloudSDKException as err:
        print(err)
示例#16
0
from tencentcloud.asr.v20190614 import asr_client, models
import base64
import io
import sys

SECRET_ID = "你的Secret ID"
SECRET_KEY = "你的Secret Key"

try:
    cred = credential.Credential(SECRET_ID, SECRET_KEY)
    httpProfile = HttpProfile()
    httpProfile.endpoint = "asr.tencentcloudapi.com"
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    clientProfile.signMethod = "TC3-HMAC-SHA256"
    client = asr_client.AsrClient(cred, "ap-beijing", clientProfile)
    # 读取文件以及 base64
    with open('./geektime-00.wav', "rb") as f:
        if sys.version_info[0] == 2:
            content = base64.b64encode(f.read())
        else:
            content = base64.b64encode(f.read()).decode('utf-8')
        f.close()
    # 发送请求
    req = models.SentenceRecognitionRequest()
    params = {
        "ProjectId": 0,
        "SubServiceType": 2,
        "SourceType": 1,
        "UsrAudioKey": "sessionid-geektime"
    }