wideband 发表于 2023-3-11 19:36:58

语音识别测试:ConnectionRefusedError: 由于目标计算机积极拒绝,无法连接。

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
   print('Say Something')
   audio = r.listen(source)
   voice_data = r.recognize_google(audio)
   print(voice_data)


################################################

Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\urllib\request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\http\client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\http\client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\http\client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\http\client.py", line 1037, in _send_output
    self.send(msg)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\http\client.py", line 975, in send
    self.connect()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\http\client.py", line 941, in connect
    self.sock = self._create_connection(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\socket.py", line 850, in create_connection
    raise exceptions
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311-32\Lib\socket.py", line 835, in create_connection
    sock.connect(sa)
ConnectionRefusedError: 由于目标计算机积极拒绝,无法连接。

isdkz 发表于 2023-3-11 19:36:59

这个得爬梯子

wideband 发表于 2023-3-13 10:09:14

speech_recognition python函数库:其主要的作用是通过麦克风来将我们的录音保存到指定的文件中

wideband 发表于 2023-3-13 10:10:34

s//zhuanlan.zhihu.com/p/359656966

wideband 发表于 2023-3-18 21:39:33

讯飞语音转写API进行音频转文字OK

# -*- coding: utf-8 -*-
# 使用讯飞语音转写API进行音频转文字#
import base64
import hashlib
import hmac
import json
import os
import time
import requests
import urllib

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:\\Desktop\\mp3\\'
fileList=[]

# 输入讯飞开放平台的appid,secret_key和待转写的文件路径
if __name__ == '__main__':
    keyString=['微信','老爸']
    fileList=os.listdir(dir_path)
    for fileName in fileList:
      print("正在识别:"+fileName)
      dir=dir_path+fileName
      api = RequestApi(appid="appid",
                        secret_key="xxxxxxxxxxxxxxxxxx",
                        upload_file_path=dir)
      res=api.get_result()
      obj=res['content']['orderResult']
      for key in keyString:
            if(key in obj):
                print(fileName+"--检测到关键字【"+key+"】")

wideband 发表于 2023-3-20 08:27:41

#这里是音乐文件的文件夹
dir_path = r'D:\\Desktop\\mp3\\'
fileList=[]

# 输入讯飞开放平台的appid,secret_key和待转写的文件路径
if __name__ == '__main__':
    keyString=['微信','老爸']
    fileList=os.listdir(dir_path)
    for fileName in fileList:
      print("正在识别:"+fileName)
      dir=dir_path+fileName
      api = RequestApi(appid="appid",
                        secret_key="xxxxxxxxxxxxxxxxxx",
                        upload_file_path=dir)
      res=api.get_result()
      obj=res['content']['orderResult']
      for key in keyString:
            if(key in obj):
                print(fileName+"--检测到关键字【"+key+"】")

wideband 发表于 2023-3-20 08:43:08

dir_path = r'D:\\Desktop\\mp3\\'

or

dir_path = r'D:/Desktop/mp3/'

wideband 发表于 2023-3-20 10:18:43

content: 主体、主结构

json对象,{a:1,b:2},   python中 访问a,    a=res["a"]

obj=res['content']    或者   print(res)


{'code': '000000', 'descInfo': 'success', 'content': {'orderInfo': {'orderId': 'DKHJQ2023032010143217800063C0035100000', 'failType': 11, 'status': -1, 'originalDuration': 200, 'realDuration': 269530, 'expireTime': 1679537585361}, 'orderResult': '{"lattice":[{"json_1best":"{\\"st\\":{\\"sc\\":\\"0.00\\",\\"pa\\":\\"0\\",\\"rt\\":[{\\"ws\\":[{\\"cw\\":[{\\"w\\":\\"没\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":57,\\"we\\":92},{\\"cw\\":[{\\"w\\":\\"过多\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":93,\\"we\\":152},{\\"cw\\":[{\\"w\\":\\"时\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":153,\\"we\\":196},{\\"cw\\":[{\\"w\\":\\",\\",\\"wp\\":\\"p\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":196,\\"we\\":196}]}],\\"bg\\":\\"1290\\",\\"rl\\":\\"0\\",\\"ed\\":\\"3260\\"}}"},{"json_1best":"{\\"st\\":{\\"sc\\":\\"0.00\\",\\"pa\\":\\"0\\",\\"rt\\":[{\\"ws\\":[{\\"cw\\":[{\\"w\\":\\"宋江\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":1,\\"we\\":64},{\\"cw\\":[{\\"w\\":\\"身\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":65,\\"we\\":100},{\\"cw\\":[{\\"w\\":\\"死\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":101,\\"we\\":164},{\\"cw\\":[{\\"w\\":\\",\\",\\"wp\\":\\"p\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":164,\\"we\\":164},{\\"cw\\":[{\\"w\\":\\"李逵\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":165,\\"we\\":280},{\\"cw\\":[{\\"w\\":\\"身\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":281,\\"we\\":328},{\\"cw\\":[{\\"w\\":\\"死\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":329,\\"we\\":388},{\\"cw\\":[{\\"w\\":\\",\\",\\"wp\\":\\"p\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":388,\\"we\\":388}]}],\\"bg\\":\\"3430\\",\\"rl\\":\\"0\\",\\"ed\\":\\"7400\\"}}"},{"json_1best":"{\\"st\\":{\\"sc\\":\\"0.00\\",\\"pa\\":\\"0\\",\\"rt\\":[{\\"ws\\":[{\\"cw\\":[{\\"w\\":\\"死后\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":1,\\"we\\":80},{\\"cw\\":[{\\"w\\":\\"两\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],\\"wb\\":81,\\"we\\":124},{\\"cw\\":[{\\"w\\":\\"人\\",\\"wp\\":\\"n\\",\\"wc\\":\\"0.0000\\"}],

wideband 发表于 2023-3-20 16:29:21

"code": "000000”,             ##### 返回的错误码
"descInfo": "success",      #####转写信息说明,
"content":{                     #####是请求返回体的主体

obj=res['content']['orderResult']   
返回示例:

{
        "code": "000000",
        "descInfo": "success",
        "content": {
                "orderId": "DKHJQ202209021522090215490FAAE7DD0008C",
                "taskEstimateTime": 28000
        }
}

wideband 发表于 2023-3-20 16:31:47

{
        "code": "000000",
        "descInfo": "success",
        "content": {
                "orderInfo": {
                        "orderId": "DKHJQ2022090510220905100562536FEF00062",
                        "failType": 0,
                        "status": 4,
                        "originalDuration": 200,
                        "realDuration": 1878
                },
                "orderResult": "{\"lattice\":[{\"json_1best\":\"{\\\"st\\\":{\\\"sc\\\":\\\"0.86\\\",\\\"pa\\\":\\\"0\\\",\\\"rt\\\":[{\\\"ws\\\":[{\\\"cw\\\":[{\\\"w\\\":\\\"这\\\",\\\"wp\\\":\\\"n\\\",\\\"wc\\\":\\\"1.0000\\\"}],\\\"wb\\\":1,\\\"we\\\":16},{\\\"cw\\\":[{\\\"w\\\":\\\"是\\\",\\\"wp\\\":\\\"n\\\",\\\"wc\\\":\\\"1.0000\\\"}],\\\"wb\\\":17,\\\"we\\\":36},{\\\"cw\\\":[{\\\"w\\\":\\\"一\\\",\\\"wp\\\":\\\"n\\\",\\\"wc\\\":\\\"1.0000\\\"}],\\\"wb\\\":37,\\\"we\\\":52},{\\\"cw\\\":[{\\\"w\\\":\\\"条\\\",\\\"wp\\\":\\\"n\\\",\\\"wc\\\":\\\"1.0000\\\"}],\\\"wb\\\":53,\\\"we\\\":80},{\\\"cw\\\":[{\\\"w\\\":\\\"测试\\\",\\\"wp\\\":\\\"n\\\",\\\"wc\\\":\\\"1.0000\\\"}],\\\"wb\\\":81,\\\"we\\\":116},{\\\"cw\\\":[{\\\"w\\\":\\\"音频\\\",\\\"wp\\\":\\\"n\\\",\\\"wc\\\":\\\"1.0000\\\"}],\\\"wb\\\":117,\\\"we\\\":172},{\\\"cw\\\":[{\\\"w\\\":\\\"。\\\",\\\"wp\\\":\\\"p\\\",\\\"wc\\\":\\\"0.0000\\\"}],\\\"wb\\\":172,\\\"we\\\":172},{\\\"cw\\\":[{\\\"w\\\":\\\"\\\",\\\"wp\\\":\\\"g\\\",\\\"wc\\\":\\\"0.0000\\\"}],\\\"wb\\\":172,\\\"we\\\":172}]}],\\\"bg\\\":\\\"50\\\",\\\"rl\\\":\\\"0\\\",\\\"ed\\\":\\\"1840\\\"}}\"}],\"lattice2\":[{\"lid\":\"0\",\"end\":\"1840\",\"begin\":\"50\",\"json_1best\":{\"st\":{\"sc\":\"0.86\",\"pa\":\"0\",\"rt\":[{\"nb\":\"1\",\"nc\":\"1.0\",\"ws\":[{\"cw\":[{\"w\":\"这\",\"wp\":\"n\",\"wc\":\"1.0000\"}],\"wb\":1,\"we\":16},{\"cw\":[{\"w\":\"是\",\"wp\":\"n\",\"wc\":\"1.0000\"}],\"wb\":17,\"we\":36},{\"cw\":[{\"w\":\"一\",\"wp\":\"n\",\"wc\":\"1.0000\"}],\"wb\":37,\"we\":52},{\"cw\":[{\"w\":\"条\",\"wp\":\"n\",\"wc\":\"1.0000\"}],\"wb\":53,\"we\":80},{\"cw\":[{\"w\":\"测试\",\"wp\":\"n\",\"wc\":\"1.0000\"}],\"wb\":81,\"we\":116},{\"cw\":[{\"w\":\"音频\",\"wp\":\"n\",\"wc\":\"1.0000\"}],\"wb\":117,\"we\":172},{\"cw\":[{\"w\":\"。\",\"wp\":\"p\",\"wc\":\"0.0000\"}],\"wb\":172,\"we\":172},{\"cw\":[{\"w\":\"\",\"wp\":\"g\",\"wc\":\"0.0000\"}],\"wb\":172,\"we\":172}]}],\"pt\":\"reserved\",\"bg\":\"50\",\"si\":\"0\",\"rl\":\"0\",\"ed\":\"1840\"}},\"spk\":\"段落-0\"}]}",
                "taskEstimateTime": 0
        }
}

wideband 发表于 2023-3-20 19:55:54

冬, 寒,枣   :\\ 反斜杆

wideband 发表于 2023-3-21 08:28:29

2. 音频编辑
这个自动化脚本将为你编辑音频文件,你可以提取声音、合并声音、播放声音、分割/切割声音等等,通过这个脚本,终于可以扔掉那些付费软件了。

from pydub import AudioSegment
from pydub.utils import mediainfo
from pydub.playback import play
————————————————

wideband 发表于 2023-3-21 08:29:49

# 分割音频
sound = AudioSegment.from_file("music.mp3", format="mp3")
sound_1 = sound[:10000]
sound_2 = sound
sound_1.export("music_1.mp3", format="mp3")
sound_2.export("music_2.mp3", format="mp3")
————————————————

wideband 发表于 2023-3-21 15:54:52

mp3文件切割成3个:ffmpeg.exe    ffplay.exe   ffprobe

import base64
import hashlib
import hmac
import json
import os
import time
import requests
import urllib
from pydub import AudioSegment
from pydub.utils import make_chunks
import os, re

dir_path = r'D:/Desktop/mp4test/mp3/'
fileList1=[]

# 输入讯飞开放平台的appid,secret_key和待转写的文件路径
# for index in range(0,len(fileName)):

if __name__ == '__main__':
   
    fileList=os.listdir(dir_path)
    print("列表中文件名1",fileList)   
    fileList2 = for f in fileList]


    for fileName in fileList:
      print("列表中文件名"+fileName)         
      dir=dir_path+fileName
      sound = AudioSegment.from_file(dir, format="mp3")#你的音频文件的路径

      fileName_1 = sound[:100000]
      fileName_2 = sound
      fileName_3 = sound
      fileName = fileName[:-4]
      fileName_1.export("D:/Desktop/mp4test/mp3-3/"+fileName+"_1.mp3", format="mp3")
      fileName_2.export("D:/Desktop/mp4test/mp3-3/"+fileName+"_2.mp3", format="mp3")
      fileName_3.export("D:/Desktop/mp4test/mp3-3/"+fileName+"_3.mp3", format="mp3")
      
   
      #print("正在识别2:"+mp3)



      

wideband 发表于 2023-3-22 09:23:53

chunks = make_chunks(mp3, 10000)# 将文件切割为10s一块

for each in os.listdir("D:/PycharmProjects/拾音器/"):# 循环目录

    filename = re.findall(r"(.*?)\.mp3", each)# 取出.mp3后缀的文件名
    print(each)
    if each:

      mp3 = AudioSegment.from_file('D:/PycharmProjects/拾音器//{}'.format(each), "mp3")# 打开mp3文件
      #         # # mp3.export(filename, format="mp3") #
      size = 10000# 切割的毫秒数 10s=10000

      chunks = make_chunks(mp3, size)# 将文件切割为10s一块

      for i, chunk in enumerate(chunks):
            chunk_name = "{}-{}.mp3".format(each.split("."), i)    # 也可以自定义名字
            print(chunk_name)
            chunk.export('D:/PycharmProjects/拾音器2/{}'.format(chunk_name), format="mp3")# 新建的保存文件夹

wideband 发表于 2023-3-23 20:32:25

语音转文字:


      pattern = re.compile("[\u4e00-\u9fff]+")

      # 用正则匹配所有的汉字
      matches = pattern.findall(str(obj))
      sencent = ""
      for i in range(len(matches)):
            sencent += matches

      with open("20230323.txt","w") as f:
            f.write(sencent)

wideband 发表于 2023-3-23 21:24:07

使用pallhub自动加标点

pip install paddlehub
pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

import paddlehub as hub      
mode = hub.Module(name='auto_punc', version='1.0.0')    //使用模型
punc_texts = mode.add_puncs(sencent)   // 获取最终结果,其中sencent是未断句的文本

wideband 发表于 2023-3-24 15:15:56

sys.exit(app.exec_())      #退出应用,并返回值到 父进程。app.exec_(),运行主循环

wideband 发表于 2023-3-24 18:27:09

pip install paddlehub -i https://mirrors.aliyun.com/pypi/simple --only-binary :all:

pip install paddlehub -i https://mirror.baidu.com/pypi/simple

这个库应该是 paddlehub,而不是 paddlepaddle


wideband 发表于 2023-3-25 17:49:47

用 3.7版本ok:

pip install requests
pip install paddlehub==2.3.1
pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip install scipy
页: [1] 2
查看完整版本: 语音识别测试:ConnectionRefusedError: 由于目标计算机积极拒绝,无法连接。