whisper语音识别部署及WER评价

这篇具有很好参考价值的文章主要介绍了whisper语音识别部署及WER评价。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.whisper部署

详细过程可以参照:🏠

创建项目文件夹

mkdir whisper
cd whisper

conda创建虚拟环境

conda create -n py310 python=3.10 -c conda-forge -y

安装pytorch

pip install --pre torch torchvision torchaudio --extra-index-url 

下载whisper

pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git

安装相关包

pip install tqdm
pip install numba
pip install tiktoken==0.3.3
brew install ffmpeg

测试一下whispet是否安装成功(默认识别为中文)

whisper test.wav --model small
#test.wav为自己的测试wav文件,map3也支持 small是指用小模型

whisper识别中文的时候经常会输出繁体,加入一下参数可以避免:

 whisper test.wav --model small --language zh --initial_prompt "以下是普通话的句子。"
#注意"以下是普通话的句子。"不能随便修改,只能是这句话才有效果。

2.脚本批量测试

创建test.sh脚本,输入一下内容,可以实现对某一文件夹下的wav文件逐个中文语音识别。

#!/bin/bash
for ((i=0;i<300;i++));do
        file="wav/A13_${i}.wav"
        if [ ! -f "$file" ];then
                break
        fi
        whisper "$file" --model medium --output_dir denied --language zh --initial_prompt "以下是普通话的句子。"
done
                                                                                                                                

 实现英文语音识别需要修改为:

#!/bin/bash
for ((i=0;i<300;i++));do
        file="en/${i}.wav"
        if [ ! -f "$file" ];then
                break
        fi
        whisper "$file" --model small --output_dir denied --language en
done
                                                                                                                          

3.对运行出来的结果进行评测

一般地,语音识别通常采用WER,即词错误率,评估语音识别和文本转换质量。

这里我们主要采用 github上的开源项目:🌟 编写的python-wer代码对结果进行评价。

其中,我们的正确样本形式为:whisper语音识别部署及WER评价

 whisper输出的预测结果形式为:

whisper语音识别部署及WER评价

 因此要对文本进行处理(去空格、去标点符号)后进行wer评价,相关代码如下:

(可根据具体情况修改calculate_WER)

import sys
import numpy

def editDistance(r, h):
    '''
    This function is to calculate the edit distance of reference sentence and the hypothesis sentence.
    Main algorithm used is dynamic programming.
    Attributes: 
        r -> the list of words produced by splitting reference sentence.
        h -> the list of words produced by splitting hypothesis sentence.
    '''
    d = numpy.zeros((len(r)+1)*(len(h)+1), dtype=numpy.uint8).reshape((len(r)+1, len(h)+1))
    for i in range(len(r)+1):
        d[i][0] = i
    for j in range(len(h)+1):
        d[0][j] = j
    for i in range(1, len(r)+1):
        for j in range(1, len(h)+1):
            if r[i-1] == h[j-1]:
                d[i][j] = d[i-1][j-1]
            else:
                substitute = d[i-1][j-1] + 1
                insert = d[i][j-1] + 1
                delete = d[i-1][j] + 1
                d[i][j] = min(substitute, insert, delete)
    return d

def getStepList(r, h, d):
    '''
    This function is to get the list of steps in the process of dynamic programming.
    Attributes: 
        r -> the list of words produced by splitting reference sentence.
        h -> the list of words produced by splitting hypothesis sentence.
        d -> the matrix built when calulating the editting distance of h and r.
    '''
    x = len(r)
    y = len(h)
    list = []
    while True:
        if x == 0 and y == 0: 
            break
        elif x >= 1 and y >= 1 and d[x][y] == d[x-1][y-1] and r[x-1] == h[y-1]: 
            list.append("e")
            x = x - 1
            y = y - 1
        elif y >= 1 and d[x][y] == d[x][y-1]+1:
            list.append("i")
            x = x
            y = y - 1
        elif x >= 1 and y >= 1 and d[x][y] == d[x-1][y-1]+1:
            list.append("s")
            x = x - 1
            y = y - 1
        else:
            list.append("d")
            x = x - 1
            y = y
    return list[::-1]

def alignedPrint(list, r, h, result):
    '''
    This funcition is to print the result of comparing reference and hypothesis sentences in an aligned way.
    
    Attributes:
        list   -> the list of steps.
        r      -> the list of words produced by splitting reference sentence.
        h      -> the list of words produced by splitting hypothesis sentence.
        result -> the rate calculated based on edit distance.
    '''
    print("REF:", end=" ")
    for i in range(len(list)):
        if list[i] == "i":
            count = 0
            for j in range(i):
                if list[j] == "d":
                    count += 1
            index = i - count
            print(" "*(len(h[index])), end=" ")
        elif list[i] == "s":
            count1 = 0
            for j in range(i):
                if list[j] == "i":
                    count1 += 1
            index1 = i - count1
            count2 = 0
            for j in range(i):
                if list[j] == "d":
                    count2 += 1
            index2 = i - count2
            if len(r[index1]) < len(h[index2]):
                print(r[index1] + " " * (len(h[index2])-len(r[index1])), end=" ")
            else:
                print(r[index1], end=" "),
        else:
            count = 0
            for j in range(i):
                if list[j] == "i":
                    count += 1
            index = i - count
            print(r[index], end=" "),
    print("\nHYP:", end=" ")
    for i in range(len(list)):
        if list[i] == "d":
            count = 0
            for j in range(i):
                if list[j] == "i":
                    count += 1
            index = i - count
            print(" " * (len(r[index])), end=" ")
        elif list[i] == "s":
            count1 = 0
            for j in range(i):
                if list[j] == "i":
                    count1 += 1
            index1 = i - count1
            count2 = 0
            for j in range(i):
                if list[j] == "d":
                    count2 += 1
            index2 = i - count2
            if len(r[index1]) > len(h[index2]):
                print(h[index2] + " " * (len(r[index1])-len(h[index2])), end=" ")
            else:
                print(h[index2], end=" ")
        else:
            count = 0
            for j in range(i):
                if list[j] == "d":
                    count += 1
            index = i - count
            print(h[index], end=" ")
    print("\nEVA:", end=" ")
    for i in range(len(list)):
        if list[i] == "d":
            count = 0
            for j in range(i):
                if list[j] == "i":
                    count += 1
            index = i - count
            print("D" + " " * (len(r[index])-1), end=" ")
        elif list[i] == "i":
            count = 0
            for j in range(i):
                if list[j] == "d":
                    count += 1
            index = i - count
            print("I" + " " * (len(h[index])-1), end=" ")
        elif list[i] == "s":
            count1 = 0
            for j in range(i):
                if list[j] == "i":
                    count1 += 1
            index1 = i - count1
            count2 = 0
            for j in range(i):
                if list[j] == "d":
                    count2 += 1
            index2 = i - count2
            if len(r[index1]) > len(h[index2]):
                print("S" + " " * (len(r[index1])-1), end=" ")
            else:
                print("S" + " " * (len(h[index2])-1), end=" ")
        else:
            count = 0
            for j in range(i):
                if list[j] == "i":
                    count += 1
            index = i - count
            print(" " * (len(r[index])), end=" ")
    print("\nWER: " + result)
    return result

def wer(r, h):
    """
    This is a function that calculate the word error rate in ASR.
    You can use it like this: wer("what is it".split(), "what is".split()) 
    """
    # build the matrix
    d = editDistance(r, h)

    # find out the manipulation steps
    list = getStepList(r, h, d)

    # print the result in aligned way
    result = float(d[len(r)][len(h)]) / len(r) * 100
    result = str("%.2f" % result) + "%"
    result=alignedPrint(list, r, h, result)
    return result

# 计算总WER
def calculate_WER():
    with open("whisper_out.txt", "r") as f:
        text1_list = [i[11:].strip("\n") for i in f.readlines()]
    with open("A13.txt", "r") as f:
        text2_orgin_list = [i[11:].strip("\n") for i in f.readlines()]

    total_distance = 0
    total_length = 0
    WER=0
    symbols = ",@#¥%……&*()——+~!{}【】;‘:“”‘。?》《、"
    # calculate distance between each pair of texts
    for i in range(len(text1_list)):
        match1 = re.search('[\u4e00-\u9fa5]', text1_list[i])
        if match1:
            index1 = match1.start()
        else:
            index1 = len(text1_list[i])
        match2 = re.search('[\u4e00-\u9fa5]', text2_orgin_list[i])
        if match2:
            index2 = match2.start()
        else:
            index2 = len( text2_orgin_list[i])
        result1=  text1_list[i][index1:]
        result1= result1.translate(str.maketrans('', '', symbols))
        result2=  text2_orgin_list[i][index2:]
        result2=result2.replace(" ", "")
        print(result1)
        print(result2)
        result=wer(result1,result2)
        WER+=float(result.strip('%')) / 100
    WER=WER/len(text1_list)
    print("总WER:", WER)
    print("总WER:", WER.__format__('0.2%'))
calculate_WER()

评价结果形如:

whisper语音识别部署及WER评价

4.与paddlespeech的测试对比:

数据集

数据量

paddle

(中英文分开)

paddle

(同一模型)

whisper(small)

(同一模型)

whisper(medium)

(同一模型)

zhthchs30

(中文错字率)

250

11.61%

45.53%

24.11%

13.95%

LibriSpeech

(英文错字率)

125

7.76%

50.88%

9.31%

9.31%

5.测试所用数据集

自己处理过的开源wav数据文章来源地址https://www.toymoban.com/news/detail-496316.html

到了这里,关于whisper语音识别部署及WER评价的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 本地部署_语音识别工具_Whisper

    1 简介 Whisper 是 OpenAI 的语音识别系统(几乎是最先进),它是免费的开源模型,可供本地部署。 2 docker https://hub.docker.com/r/onerahmet/openai-whisper-asr-webservice 3 github https://github.com/ahmetoner/whisper-asr-webservice 4 运行 image 大小:11.5G 运行后,即可在9000端口通过swagger调用,我先用手机录

    2024年02月05日
    浏览(29)
  • 学习实践-Whisper语音识别模型实战(部署+运行)

    OpenAI的语音识别模型Whisper,Whisper 是一个自动语音识别(ASR,Automatic Speech Recognition)系统,OpenAI 通过从网络上收集了 68 万小时的多语言(98 种语言)和多任务(multitask)监督数据对 Whisper 进行了训练。OpenAI 认为使用这样一个庞大而多样的数据集,可以提高对口音、背景噪音

    2024年02月06日
    浏览(38)
  • 实战whisper:本地化部署通用语音识别模型

            Whisper 是一种通用语音识别模型。它是在大量不同音频数据集上进行训练的,也是一个多任务模型,可以执行多语言语音识别、语音翻译和语言识别。         这里呢,我将给出我的一些代码,来帮助你尽快实现【语音转文字】的服务部署。         以下是该A

    2024年01月18日
    浏览(84)
  • 开源语音识别faster-whisper部署教程

    源码地址 模型下载地址: 下载 cuBLAS and cuDNN 在 conda 环境中创建 python 运行环境 激活虚拟环境 安装 faster-whisper 依赖 执行完以上步骤后,我们可以写代码了 说明: 更多内容欢迎访问博客 对应视频内容欢迎访问视频

    2024年02月04日
    浏览(43)
  • OpenAI Whisper 语音识别 API 模型使用 | python 语音识别

    OpenAI 除了 ChatGPT 的 GPT3.5 API 更新之外,又推出了一个 Whisper 的语音识别模型。支持96种语言。 Python 安装 openai 库后,把需要翻译的音频目录放进去,运行程序即可生成音频对应的文字。 以上。

    2024年02月16日
    浏览(39)
  • 【语音识别】OpenAI whisper

    目录 1. 简单介绍 2. 代码调用 Introducing Whisper https://openai.com/blog/whisper/ OpenAI 的开源自动语音识别神经网络 whisper 安装 Python 调用

    2024年02月13日
    浏览(35)
  • 探索Whisper语音识别

    问题一:python多版本切换 背景:有了anaconda环境  还有一个c盘的不知道什么东西 我准备下载一个python3.9.9 去官网 然后安装,安装之前一定要把原来的python卸载干净。  3.9.9安装不上,我用3.10 切换的话,就是去环境变量里面改变位置  最后发现直接用anaconda也可以,python3.8也

    2024年02月09日
    浏览(77)
  • 语音识别whisper

    Whisper是一个通用的语音识别模型,它使用了大量的多语言和多任务的监督数据来训练,能够在英语语音识别上达到接近人类水平的鲁棒性和准确性1。Whisper还可以进行多语言语音识别、语音翻译和语言识别等任务2。Whisper的架构是一个简单的端到端方法,采用了编码器-解码器

    2024年02月12日
    浏览(25)
  • Whisper 语音识别模型

    Whisper 语音识别模型 Whisper 是一种通用的语音识别模型。它是在包含各种音频的大型数据集上训练的,也是一个可以执行多语言语音识别、语音翻译和语言识别的多任务模型。 开源项目地址:https://github.com/openai/whisper Whisper 语音识别模型 Transformer 序列到序列模型针对各种语音

    2024年02月16日
    浏览(25)
  • python语音识别whisper

    一、背景 最近想提取一些视频的字幕,语音文案,研究了一波 二、whisper语音识别 Whisper 是一种通用的语音识别模型。它在不同音频的大型数据集上进行训练,也是一个多任务模型,可以执行多语言语音识别以及语音翻译和语言识别。 stable-ts在 OpenAI 的 Whisper 之上修改并添加

    2024年02月05日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包