1 itchat已停用

自从微信禁止网页版登陆之后,itchat 库实现的功能也就都不能用了。

CMD>pip install itchat
chardet-3.0.4 
idna-2.8 
itchat-1.3.10 
pypng-0.0.20 
pyqrcode-1.2.1 
urllib3-1.25.11

在这里插入图片描述更新已经停止在2017年。
Quick Response Code,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。

2 免费聊天机器人

2.1 青云客机器人

完全免费,支持功能:天气、翻译、藏头诗、笑话、歌词、计算、域名信息/备案/收录查询、IP查询、手机号码归属、人工智能聊天。
不用注册,不用申请key,拿来就用!

urllib是Python自带的标准库,无需安装,直接可以用。

# -*- coding:utf-8 -*-
import urllib
import requests
def qingyunke(msg):
    re = urllib.parse.quote(msg)
    url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(re)
    html = requests.get(url)
    return html.json()["content"]

msg = '我漂亮吗'
print("原话>>", msg)
res = qingyunke(msg)
print("青云客>>", res)

输出
原话>> 我漂亮吗
青云客>> 你很漂亮,可是没有菲菲这么有气质

其中urllib.parse.quote

>>>import urllib.parse
>>>help(urllib.parse.quote)

quote(string, 
safe='/', 
encoding=None, 
errors=None)
例如
quote('abc def') -> 'abc%20def'

    Each part of a URL, e.g. the path info, the query, etc., has a
    different set of reserved characters that must be quoted.

    RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists
    the following reserved characters.

    reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                  "$" | ","

    Each of these characters is reserved in some component of a URL,
    but not necessarily in all of them.

    By default, the quote function is intended for quoting the path
    section of a URL.  Thus, it will not encode '/'.  This character
    is reserved, but in typical usage the quote function is being
    called on a path where the existing slash characters are used as
    reserved characters.

    string and safe may be either str or bytes objects. encoding and errors
    must not be specified if string is a bytes object.

    The optional encoding and errors parameters specify how to deal with
    non-ASCII characters, as accepted by the str.encode method.
    By default, encoding='utf-8' (characters are encoded with UTF-8), and
    errors='strict' (unsupported characters raise a UnicodeEncodeError).


2.2 图灵机器人

图灵机器人
网址http://www.tuling123.com
tuling_key: ‘98f95153fb5c4684a5602b909949ba61’
#建议使用自己的图灵机器人API Key

# -*- coding:utf-8 -*-
import urllib
import requests
import json
def tuling(msg):
    api_key = "98f95153fb5c4684a5602b909949ba61"
    url = 'http://openapi.tuling123.com/openapi/api/v2'
    data = {"perception": {"inputText": {"text": msg}},
             "userInfo": {"apiKey": api_key, "userId": "1"}}
    datas = json.dumps(data)
    html = requests.post(url, datas).json()
    if html['intent']['code'] == 4003:
        print("次数用完")
        return None
    return html['results'][0]['values']['text']

msg = '我好看吗'
print("原话>>", msg)
res = tuling(msg)
print("图灵>>", res)

输出
原话>> 我好看吗
图灵>> 我看不到,声音好听,应该也很漂亮吧。

3 百度api实现语音识别

pip install pyaudio

3.1 本地录音

import wave
from pyaudio import PyAudio, paInt16
import time
framerate = 16000  # 采样率
num_samples = 2000  # 采样点
channels = 1  # 声道
sampwidth = 2  # 采样宽度2bytes
FILEPATH = 'speech.wav'


def save_wave_file(filepath, data):
    wf = wave.open(filepath, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes(b''.join(data))
    wf.close()


# 录音
def my_record():
    pa = PyAudio()
    # 打开一个新的音频stream
    stream = pa.open(format=paInt16, channels=channels,
                     rate=framerate, input=True, 
                     frames_per_buffer=num_samples)
    my_buf = []  # 存放录音数据

    t = time.time()
    print('正在录音...')

    while time.time() < t + 4:  # 设置录音时间(秒)
        # 循环read,每次read 2000frames
        string_audio_data = stream.read(num_samples)
        my_buf.append(string_audio_data)
    print('录音结束...')
    save_wave_file(FILEPATH, my_buf)
    stream.close()

输出speech.wave文件

3.2 语音识别

3.2.1 复杂模式

# -*- coding:utf-8 -*-
''' 你的APPID AK SK  参数在申请的百度云语音服务的控制台查看'''
APP_ID = '23***929'
API_KEY = 'DRGC6******RsWOQZbC'
SECRET_KEY = 'OnCmaG9SkR****GFq5nTnKEfukXQ'
import requests
import base64

base_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
APIKey = API_KEY
SecretKey = SECRET_KEY
HOST = base_url % (APIKey, SecretKey)
FILEPATH = "speech.wav"


def getToken(host):
    res = requests.post(host)
    return res.json()['access_token']


def get_audio(file):
    with open(file, 'rb') as f:
        data = f.read()
    return data


def speech2text(speech_data, token, dev_pid=1537):
    # dev_pid,1536:普通话(简单英文), 1537: 普通话(有标点), 1737: 英语, 1637: 粤语, 1837: 四川话
    FORMAT = 'wav'
    RATE = '16000'
    CHANNEL = 1
    CUID = APP_ID
    SPEECH = base64.b64encode(speech_data).decode('utf-8')

    data = {
        'format': FORMAT,
        'rate': RATE,
        'channel': CHANNEL,
        'cuid': CUID,
        'len': len(speech_data),
        'speech': SPEECH,
        'token': token,
        'dev_pid': dev_pid
    }
    url = 'https://vop.baidu.com/server_api'
    headers = {'Content-Type': 'application/json'}
    print('正在识别...')
    r = requests.post(url, json=data, headers=headers)
    Result = r.json()
    if 'result' in Result:
        return Result['result'][0]
    else:
        return Result
if __name__ == '__main__':
    TOKEN = getToken(HOST)
    speech = get_audio(FILEPATH)
    result = speech2text(speech, TOKEN)
    print(result)

识别出speech.wav中的文字。

3.2.2 简单模式

百度语音aip安装与使用

# -*- coding:utf-8 -*-
from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = '237**929'
API_KEY = 'DRGC6L****e8hG5RsWOQZbC'
SECRET_KEY = 'OnCmaG9Sk*****iK11GFq5nTnKEfukXQ'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

#以下是读取录音文件返回文字的方法
# 读取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

# 识别本地文件,并返回识别结果
my_file = 'speech.wav'
result = client.asr(get_file_content(my_file), 'pcm', 16000, { 'dev_pid': 1537,})
print(result)

3.3 触发应用

# -*- coding:utf-8 -*-
import webbrowser

def openbrowser(text):
    maps = {
        '百度': ['百度', 'baidu'],
        '腾讯': ['腾讯', 'tengxun'],
        '网易': ['网易', 'wangyi']

    }
    if text in maps['百度']:
        webbrowser.open_new_tab('https://www.baidu.com')
    elif text in maps['腾讯']:
        webbrowser.open_new_tab('https://www.qq.com')
    elif text in maps['网易']:
        webbrowser.open_new_tab('https://www.163.com/')
    else:
        webbrowser.open_new_tab('https://www.baidu.com/s?wd=%s' % text)


if __name__ == '__main__':
    result = "baidu"
    openbrowser(result.strip(','))

3.4 整体代码

# -*- coding:utf-8 -*-
import wave
import requests
import time
import base64
from pyaudio import PyAudio, paInt16
import webbrowser

''' 你的APPID AK SK  参数在申请的百度云语音服务的控制台查看'''
APP_ID = '23***929'
API_KEY = 'DRGC****RsWOQZbC'
SECRET_KEY = 'OnCmaG9S****GFq5nTnKEfukXQ'

framerate = 16000  # 采样率
num_samples = 2000  # 采样点
channels = 1  # 声道
sampwidth = 2  # 采样宽度2bytes
FILEPATH = 'speech.wav'

base_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
APIKey = API_KEY
SecretKey = SECRET_KEY
HOST = base_url % (APIKey, SecretKey)


def getToken(host):
    res = requests.post(host)
    return res.json()['access_token']


def save_wave_file(filepath, data):
    wf = wave.open(filepath, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes(b''.join(data))
    wf.close()


def my_record():
    pa = PyAudio()
    stream = pa.open(format=paInt16, channels=channels,
                     rate=framerate, input=True, frames_per_buffer=num_samples)
    my_buf = []
    # count = 0
    t = time.time()
    print('正在录音...')

    while time.time() < t + 4:  # 秒
        string_audio_data = stream.read(num_samples)
        my_buf.append(string_audio_data)
    print('录音结束.')
    save_wave_file(FILEPATH, my_buf)
    stream.close()


def get_audio(file):
    with open(file, 'rb') as f:
        data = f.read()
    return data


def speech2text(speech_data, token, dev_pid=1537):
    FORMAT = 'wav'
    RATE = '16000'
    CHANNEL = 1
    CUID = APP_ID
    SPEECH = base64.b64encode(speech_data).decode('utf-8')

    data = {
        'format': FORMAT,
        'rate': RATE,
        'channel': CHANNEL,
        'cuid': CUID,
        'len': len(speech_data),
        'speech': SPEECH,
        'token': token,
        'dev_pid': dev_pid
    }
    url = 'https://vop.baidu.com/server_api'
    headers = {'Content-Type': 'application/json'}
    print('正在识别...')
    r = requests.post(url, json=data, headers=headers)
    Result = r.json()
    if 'result' in Result:
        return Result['result'][0]
    else:
        return Result


def openbrowser(text):
    maps = {
        '百度': ['百度', 'baidu'],
        '腾讯': ['腾讯', 'tengxun'],
        '网易': ['网易', 'wangyi']

    }
    if text in maps['百度']:
        webbrowser.open_new_tab('https://www.baidu.com')
    elif text in maps['腾讯']:
        webbrowser.open_new_tab('https://www.qq.com')
    elif text in maps['网易']:
        webbrowser.open_new_tab('https://www.163.com/')
    else:
        webbrowser.open_new_tab('https://www.baidu.com/s?wd=%s' % text)


if __name__ == '__main__':
    flag = 'y'
    while flag.lower() == 'y':
        print('请输入数字选择语言:')
        devpid = input('1536:普通话(简单英文),1537:普通话(有标点),1737:英语,1637:粤语,1837:四川话\n')
        my_record()
        TOKEN = getToken(HOST)
        speech = get_audio(FILEPATH)
        result = speech2text(speech, TOKEN, int(devpid))
        print(result)
        if type(result) == str:
            openbrowser(result.strip(','))
        flag = input('Continue?(y/n):')

4 参考附录

参考自己动手实现4大免费聊天机器人:小冰、图灵、腾讯、青云客
参考:python通过调用百度api实现语音识别
参考:查看wave波形图

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐