• 技术文章 >头条

    【光棍节,秀恩爱】用Python为女朋友打造一款智能语音闹钟

    爱喝马黛茶的安东尼爱喝马黛茶的安东尼2019-11-08 21:43:34转载2393
    在看这篇文章之前,你至少应该是一个会装操作系统的程序猿,懂点 Linux,会些 Python,最主要的是你得有一个女朋友。当然没有也没关系,相信看完这篇文章,你也尝试做了这么一款闹钟,说不定......

    软硬件清单

    读卡器以及 SD 卡(装系统用)

    音箱一枚,最好支持 3.5mm

    SSH连接工具(SecureCRT,Xshell)

    宽带、路由器(这应该是家中常备了)

    装好系统的树莓派 3B+ 一只(充电器、CPU散热风扇等)

    在开始之前先秀一下这半成品的闹钟:

    编码

    一个合格的程序员,怎么能不懂点 Python,虽然做 Java这么多年,我还是想用她来开发。

    树莓派 3B+ 的系统默认预装了 Python3 ,我们只需要安装一些第三方依赖就可以,以下便是主要代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    import time

    import random

    import os

    import pygame

    import urllib.request

    import json

    from aip import AipSpeech

     

    """

    树莓派打造智能闹钟

    pip3 install pygame

    pip3 install baidu-aip

    """

     

     

    # 获取天气

    def get_weather():

        # 青岛天气,101120201 是青岛地区的编码,其他地区请自行查找

        url = 'http://www.weather.com.cn/data/cityinfo/101120201.html'

        obj = urllib.request.urlopen(url)

        data_b = obj.read()

        data_s = data_b.decode('utf-8')

        data_dict = json.loads(data_s)

        rt = data_dict['weatherinfo']

        weather = '亲爱的:该起床了,别睡了,快变小猪了,哈哈哈哈哈,我想你了,你想我吗?青岛的温度是 {} 到 {},天气 {}'

        weather = weather.format(rt['temp1'], rt['temp2'], rt['weather'])

        if '雨' in weather:

            weather += '今天别忘记带雨伞哦!'

        du_say(weather)

     

     

    # 文字转语音

    def du_say(weather):

        app_id = '****'

        api_key = '****'

        secret_key = '****'

        client = AipSpeech(app_id, api_key, secret_key)

        # per 3是汉子 4是妹子,spd 是语速,vol 是音量

        result = client.synthesis(weather, 'zh', 1, {

            'vol': 5, 'per': 3, 'spd': 4

        })

        # 识别正确返回语音二进制 错误则返回dict 参照下面错误码

        if not isinstance(result, dict):

            with open('weather.mp3', 'wb') as f:

                f.write(result)

        py_game_player('weather.mp3')

     

     

    # 播放天气和音乐

    def py_game_player(file):

        pygame.mixer.init()

        print("播报天气")

        pygame.mixer.music.load(file)

        pygame.mixer.music.play(loops=1, start=0.0)

        print("播放音乐")

        while True:

            if pygame.mixer.music.get_busy() == 0:

                # Linux 配置定时任务要设置绝对路径

                mp3 = "/home/pi/alarmClock/"+str(random.randint(1, 6)) + ".mp3"

                # mp3 = str(random.randint(1, 6)) + ".mp3"

                pygame.mixer.music.load(mp3)

                pygame.mixer.music.play(loops=1, start=0.0)

                break

        while True:

            if pygame.mixer.music.get_busy() == 0:

                print("播报完毕,起床啦")

                break

     

     

    if __name__ == '__main__':

        get_weather()

    代码看不懂,没关系,来一张清晰的流程图:

    定时

    当然了,闹钟可不能自己播放,我们还需要加入定时任务脚本,实现定时播放。

    运行crontab -e 标志来编辑 cron 表

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    no crontab for pi - using an empty one

     

    Select an editor.  To change later, run 'select-editor'.

     

      1. /bin/ed

      2. /bin/nano        <---- easiest

     

      3. /usr/bin/vim.tiny

     

    Choose 1-3 [2]: 3

    这里我选择最熟悉的 Vim 命令进行编辑。

    cron 条目的布局由六个部分组成:分钟,小时,月份,月份,星期几和要执行的命令。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    # * * * * *  command to execute (要执行的命令)

     

    # ┬ ┬ ┬ ┬ ┬

    # │ │ │ │ │

    # │ │ │ │ │

    # │ │ │ │ └───── 星期中的哪一天(0-7)(从0到6代表星期日到星期六,也可以使用名字;7是星期天,等同于0)

    # │ │ │ └────────── 月份 (1 - 12)

    # │ │ └───────────────几号 (1 - 31)

    # │ └──────────────────── 小时 (0 - 23)

    # └───────────────────────── 分钟 (0 - 59)

    添加一个计划任务:

    1

    2

    # 早上七点

    00 07 * * *   python3 /home/pi/alarmClock/play.py

    配置完成以后一定要检查一下时间,新装系统有可能不是中国时区:

    1

    date

    更正时区:

    1

    2

    3

    # 根据提示选择 亚洲(Asia)-上海(上海)

     

    sudo dpkg-reconfigure tzdata

    小结

    其实,这款闹钟并不智能,并且还有一些昂贵,幸好身边有两个平时不怎么用的音箱,就拿来废物利用了。好处是可以随心所欲的DIY,比如做一款APP,或者后台管理,进行远程控制,给予女朋友无时无刻的关怀。

    当然,她可能不仅仅是一款闹钟,你也可以加入语音唤醒,语音聊天,打造一款全功能的智能机器人。

    专题推荐:python 女朋友
    上一篇:一份数据科学家的完整学习路径 下一篇:python学习者有福了!微软官方上线了免费Python在线教程

    相关文章推荐

    • 学习Python类型和对象,看这篇文章足矣!• python如何实例化对象• python如何判断对象的某个属性

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网