Use Python to make picture and voice verification codes

environment

  • windows 10 64bit

  • python 3.8

  • captcha 0.4

foreword

captchaIt is a third-party library used to generate image or audio verification. Verification code technology is very common in webapplications . In this article we will take a look at some of its common uses.

Install

pipInstall using the command

pip install captcha

example

First look at an example of a picture verification code

import argparse
from captcha.image import ImageCaptcha

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--text', type=str, default='xugaoxiang', help='text that show in the image')
    opt = parser.parse_args()

    # 实例化,指定宽度和高度,如果想更换显示的字体,可以使用参数 fonts
    image = ImageCaptcha(width=300, height=100)
    captcha_text = opt.text
    # 生成图片
    data = image.generate_image(captcha_text)
    # 保存图片
    image.write(captcha_text, 'captcha.png')

--textExecute with parameters , if there are no parameters, the default value will be usedxugaoxiang

python test.py --text xgx

2fd20d097b46d64ca596bdcd5c1b83aa.png

Next, let’s take a look at the example of the voice verification code, the core code is very similar

from captcha.audio import AudioCaptcha

if __name__ == '__main__':
    audio = AudioCaptcha()
    captcha_text = '1234'
    audio_data = audio.generate(captcha_text)
    audio.write(captcha_text, 'out.wav')

After execution, an audio file will be generated in the current directory out.wav. The audio will contain strings 1234, and of course there are some noises to disturb you.

If the voice directory is not specified, only numbers can be generated by default. If characters are to be generated, the code will report an error. At this time, we can create our own voice library, which can be realized by using the two tools espeak(text-to-speech) and (speech processing). The following is the usage method of the systemffmpegubuntu

The first is to install these 2 tools, useapt install

sudo apt install espeak ffmpeg

Common characters include numbers from 0 to 9, and 26 letters. It is best to distinguish between uppercase and lowercase. After clarifying this, we execute the command

# 环境变量
export ESLANG=en

# 创建目录
mkdir $ESLANG

# 给每个字符创建一个文件夹,使用 espeak 生成对应文本的语音,最后使用 ffmpeg 处理一下
for i in {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9}; do mkdir $ESLANG/$i; espeak -a 150 -s 100 -p 15 -v$ESLANG $i -w $ESLANG/$i/orig_default.wav; ffmpeg -i $ESLANG/$i/orig_default.wav -ar 8000 -ac 1 -acodec pcm_u8 $ESLANG/$i/$i.wav; rm $ESLANG/$i/orig_default.wav; done

for i in {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9}; do mkdir $ESLANG/$i; espeak -a 150 -s 100 -p 15 -v$ESLANG $i -w $ESLANG/$i/orig_default.wav; ffmpeg -i $ESLANG/$i/orig_default.wav -ar 8000 -ac 1 -acodec pcm_u8 $ESLANG/$i/$i.wav; rm $ESLANG/$i/orig_default.wav; done

c374ed6a0af6f5ec2bd6ec9caffcfe77.png

With our own voice library, we can use it, just look at the code

from captcha.audio import AudioCaptcha

if __name__ == '__main__':
    # 使用自己的语音库
    audio = AudioCaptcha(voicedir="en")
    captcha_text = 'XGX123'
    audio_data = audio.generate(captcha_text)
    audio.write(captcha_text, 'out.wav')

In this way, the phonetics of the characters can also be generated. It should be noted here that ubuntuit is case-sensitive, windowsbut insensitive, that is to say X, and xare the same thing. Therefore, windowsin , you need to do a unified treatment of all uppercase or lowercase first, and then generate the corresponding voice.

As for the Chinese character-to-speech part apt, espeakit is not supported by default through installation, and an error message will appear. Full dictionary is not installed for 'zh'The solution is to recompile espeakand add compilation --compile=zhparameters . Interested children's shoes can try it by themselves.

For more details, please refer to the official documentation https://pypi.org/project/captcha/

Topics on Python Practical Modules

For more useful pythonmodules , please move to

https://xugaoxiang.com/category/python/modules/

Guess you like

Origin blog.csdn.net/djstavaV/article/details/126516068