How to make a Raspberry Pi that speaks the tweets of the specified user

Introduction

The 3rd laboratory hackathon was held. Other members Article 1, Article 2 This time, I decided to make a Raspberry Pi that speaks tweets. As for the motive for creating it, the article that I referred to when I made the initial settings of Raspberry Pi for the first time described Raspberry Pi that speaks loudly tweets. .. When I read it, I felt that I would like to make a Raspberry Pi that speaks the tweets of ** Tsui Abolition ** in the laboratory someday. I referred to this time ** Article **

What you prepared

--Raspberry Pi 2 + WiFi dongle --MicroSD with Raspbian installed --Power adapter --Ordinary earphones (for speakers) --Keyboard, mouse and display

Speaker check

Check the sound of the speaker

$ speaker-test -t wav

You can stop the sound with Ctr + C. At this time, sound came out from the display connected by HDMI, so in order to make sound from the earphone (virtual speaker)

$ amixer cset numid=3 1

You can convert from HDMI output to analog output by inputting, and you can play sound from the speaker.

Volume control

$ alsamixer

Installation of OpenJtalk (speech synthesis software)

What is OpenJtalk

--Open source speech synthesis engine --The following is required to make OpenJtalk speak --Text with the content to speak --Dictionary folder --Audio file --OpenJtalk standard voice --nitech_jp_atr503_m001.jhtsvoice (male voice)

Install OpenJtalk / dictionary voice data

$ sudo apt-get update
$ sudo apt-get install open-jtalk 
$ sudo apt-get install open-jtalk-mecab-naist-jdic 
$ sudo apt-get install htsengine libhtsengine-dev 
$ sudo apt-get install hts-voice-nitech-jp-atr503-m001

If the message "Do you want to continue [Y / n]?" Is displayed on the way, type "Y" and press the "Enter" key to continue the installation.

Creating jtalk.sh

Next, we will create a script to run OpenJtalk.

jtalk.sh


#!/bin/sh
tmpfile=/tmp/jtalk.wav
htsvoice="/usr/share/hts-voice/\
nitech-jp-atr503-m001/\
nitech_jp_atr503_m001.htsvoice"
echo "$1" | open_jtalk \
-x /var/lib/mecab/dic/open-jtalk/naist-jdic \
-m $htsvoice \
-ow $tmpfile && \
aplay --quiet $tmpfile
rm $tmpfile

Give execute permission to the created script.

$ chmod 755 jtalk.sh

Test the following on the command line

$ ./jtalk.sh Hello

If you hear from the speaker as "Hello" OK!

Get Twitter API Access Token

Log in to twitter from the URL below. https://apps.twitter.com/

  1. Click "create an Apps"
  2. Enter the required items (appropriate and okay) スクリーンショット 2020-02-12 18.07.20.png
  3. Click "create" and then click "Details" of the created APP.
  4. Select the "Keys and tokens" tab and click the "Generate" button for Access token & access token secret.
  5. Make a note of the displayed "API key", "API secret key", "Access token", and "Access token secret". スクリーンショット 2020-02-12 17.57.33.png

Creating a Python program

python settings

#Creating a virtual environment
$ sudo apt-get install python3-dev python3-venv
$ python3 -m venv env
$ cd env
$ source bin/activate

Install tweepy

(env) $ python -m pip install tweepy

Create teitter.py

#-*- coding:utf-8 -*-
import tweepy
import os
#Set various keys
CONSUMER_KEY = 'xxxxxxxxxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
ACCESS_TOKEN = 'xxxxxxxxxxxxxxxxxxxx'
ACCESS_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
#Create an API instance
api = tweepy.API(auth)
print('Done!') #Ready

#Get tweets
for status in tweepy.Cursor(api.user_timeline,screen_name = "ID of the acquired user",exclude_replies = True).items():
    tw_text = status.text
    tw_text = ''.join(tw_text.splitlines())
    tw_text = tw_text.replace('(',' ').replace(')',' ').replace(' ','')
    print(tw_text)
    os.system('/home/pi/jtalk.sh ' + tw_text)
    break #For the time being, only the latest tweets

Actually move!

Type the following code

(env) $ python twitter.py 

The latest tweet of the specified user was spoken in a male voice! (Even if it's a key, he talked if I followed him.)

in conclusion

We have succeeded in making Raspberry Pi speak the latest tweets of the user specified in this hackathon, so I would like to improve it so that the specified user can speak it when tweeting!

Thank you for reading!

Recommended Posts

How to make a Raspberry Pi that speaks the tweets of the specified user
Make a note of what you want to do in the future with Raspberry Pi
Install a tact switch on the Raspberry Pi to make daily "Good morning Yosoro !!" tweets comfortable
How to create a wrapper that preserves the signature of the function to wrap
How to calculate the volatility of a brand
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
How to mention a user group in slack notification, how to check the id of the user group
A new form of app that works with GitHub: How to make GitHub Apps
How to use the Raspberry Pi relay module Python
Make a BOT that shortens the URL of Discord
Basics of PyTorch (2) -How to make a neural network-
I refactored "I tried to make a script that saves posted images at once by going back to the tweets of a specific user on Twitter".
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
[Ubuntu] How to delete the entire contents of a directory
How to find the scaling factor of a biorthogonal wavelet
Is there a secret to the frequency of pi numbers?
How to connect the contents of a list into a string
I sent the data of Raspberry Pi to GCP (free)
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to make a crawler --Basic
How to determine the existence of a selenium element in Python
I tried to make a system that fetches only deleted tweets
To make sure that the specified key is in the specified bucket in Boto 3
I tried to automate the watering of the planter with Raspberry Pi
A story that struggled to handle the Python package of PocketSphinx
How to check the memory size of a variable in Python
Make the theme of Pythonista 3 like Monokai (how to make your own theme)
How to make a command to read the configuration file with pyramid
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
[Python] How to make a matrix of repeating patterns (repmat / tile)
[python] A note that started to understand the behavior of matplotlib.pyplot
[NNabla] How to remove the middle tier of a pre-built network
[Python] A program that rotates the contents of the list to the left
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
I tried to make a site that makes it easy to see the update information of Azure
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Hypothesis / Verification (176) How to make a textbook that is easier than "The easiest textbook for quantum computers"
A story that is a little addicted to the authority of the directory specified by expdp (for beginners)
[Twitter] I want to make the downloaded past tweets (of my account) into a beautiful CSV
Display the signal strength RSSI of a specific SSID (raspberry pi (linux))
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
Use Raspberry Pi to solve the problem of insufficient mobile Wi-Fi connection
[Python] How to make a class iterable
How to check the version of Django
Try to make a kernel of Jupyter
[Python] A program that calculates the number of socks to be paired
[Introduction to Python] How to sort the contents of a list efficiently with list sort
A memorandum of how to write pandas that I tend to forget personally
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[NNabla] How to add a quantization layer to the middle layer of a trained model