美文网首页大数据 爬虫Python AI SqlPython小哥哥
只有学会怎样生成验证码才会破解验证码!

只有学会怎样生成验证码才会破解验证码!

作者: 14e61d025165 | 来源:发表于2019-04-27 15:11 被阅读1次

    一到周末,总觉得不知道做什么,觉得无聊狠!

    之前说了太多度假、太多远方、太多说走就走的旅行,可问题是,程序猿特么的并不是说走真的就能走呀,指不定啥时服务器崩了呢?
    python学习扣qun【 1004391443】,内有安装包和学习视频资料免费分享,好友都会在里面交流,分享一些学习的方法和需要注意的小细节,每天也会准时的讲一些项目实战案例,欢迎加入
    所以,我无聊的蛋疼,与大家一起,用Python 为自己录一段动听的音频,然后葛优躺在摇椅上,听听自己的音频,此处省略一万字。。。

    音频预处理

    主要介绍些音频基本处理方式,为接下来的语音识别打基础。

    三种播放音频的方式

    使用 python 播放音频有以下几种方式:

    os.system()

    os.system(file) 调用系统应用来打开文件,file 可为图片或者音频文件。

    缺点:要打开具体的应用,不能在后台播放音频。

    pyaudio

    安装:pip install pyaudio

    官方提供了播放音频与录音的 api ,使用十分方便,只要把Filename更改为你的音频文件的文字,就可以播放音频了。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">"""PyAudio Example: Play a WAVE file."""
    import pyaudio
    import wave
    CHUNK = 1024
    FILENAME = '你的音频文件'
    def play(filename = FILENAME):
    wf = wave.open(filename, 'rb')
    p = pyaudio.PyAudio()
    stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
    channels=wf.getnchannels(),
    rate=wf.getframerate(),
    output=True)
    data = wf.readframes(CHUNK)
    while data != b'':
    stream.write(data)
    data = wf.readframes(CHUNK)
    stream.stop_stream()
    stream.close()
    p.terminate()
    </pre>

    jupyter notebook

    在 jupyer notebook 中播放音频可以使用以下函数:

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import IPython.display as ipd
    ipd.Audio(文件名)
    </pre>

    几种读取音频的方式

    python 有很多读取音频文件的方法,内置的库 wave ,科学计算库 scipy, 和方便易用的语音处理库 librosa。

    下面将介绍分别使用这几种库读取音频文件:

    安装

    wave 是内置库直接导入即可。

    scipy: pip install scipy

    librosa: pip install librosa

    使用

    wave.open:

    参数 path 为文件名,mode 为打开方式

    以'rb'方式打开文件返回一个 Wave_read 对象,而以'wb'方式打开文件返回一个 Wave_write 对象。

    scipy.io.wavfile:

    参数 path 为文件名

    返回 rate : 采样率(每秒采样点的个数),data : 音频数据

    librosa.load:

    参数 path 为文件名

    返回 y 为音频数据,sr 为采样率

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># read wav file from path
    from scipy.io import wavfile
    import librosa
    import pyaudio

    wave

    file = wave.open(path,'rb')

    wavfile

    rate, data = wavfile.read(path)

    librosa

    y, sr = librosa.load(path)
    </pre>

    下面演示一个使用 wavfile 读取音频文件并且画出波形的例子

    首先要计算音频到底持续了多长时间,wave 的 shape 就是总的采样点个数,除以采样频率可以得到持续的总时间(秒),乘1000得到总持续时间(毫秒)。接着通过 np.linsapce 产生时间的序列,最后使用 matplotlib 画出图像。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from scipy.io import wavfile
    import matplotlib.pyplot as plt
    import numpy as np
    %matplotlib inline

    一秒采样数

    sr, wave = wavfile.read('D://QQPCMgr/Desktop/python3/skip.wav')
    sample_number = wave.shape[0]
    total_time = int(sample_number / sr * 1000)
    time_series = np.linspace(0,total_time,sample_number)
    fig, ax = plt.subplots(1, 1)
    ax.plot(time_series, wave)
    ax.set_title('Time*Amplitude')
    ax.set_xlabel('Time/ms')
    ax.set_ylabel('Amplitude/dB')
    </pre>

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556349051228 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    最后再借用 pyaudio 的 api 我们可以实现连续录音功能:

    python 实现录音功能

    其中,函数 multi_record每结束一次录音会询问 “是否进行下一次录音?”,按回车就可以进行下一次录音了。

    <pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import wave
    import pyaudio
    import matplotlib.pyplot as plt
    import time
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = 5
    def record(filename='output.wav'):
    """官方录音教程
    """

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    frames_per_buffer=CHUNK)

    print("* recording")

    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

    print("* done recording")

    stream.stop_stream()
    stream.close()
    p.terminate()

    wf = wave.open(filename, 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
    def multi_record(num=3):
    """implement 多次录音"""
    for i in range(1,num+1):
    print('第{}次录音准备'.format(i))
    filename = 'record_{}.wav'.format(i)
    record(filename)
    time.sleep(second)
    _ = input('进行下一次录音?')
    def main():
    multi_record()
    if name == 'main':
    main()
    </pre>

    完毕!

    相关文章

      网友评论

        本文标题:只有学会怎样生成验证码才会破解验证码!

        本文链接:https://www.haomeiwen.com/subject/cudknqtx.html