Tags

, , , , , , , , , , ,

Monita, my Hispanic maid, is an old romantic. Keen to impress, I decided to create a passionate and tender cartoon for her reading pleasure, one which would arouse sensual feelings. Problem is, I’m no good with words. Not to worry though, I will simply let BBC News create the captions for the cartoon pictures I have drawn.

Using Python Tools for Visual Studio, I wrote the following Python code on my PC, which I then ported to my Raspberry Pi:

from speech import Speech
from storage import Storage
import webbrowser

speech = Speech()
storage = Storage()

# constants
CARTOON_FILE = "cartoon.html"
CARTOON_HTML = "<div style=float:left;padding:20px;><img src={} /><br /><font face=verdana color=blue size=2>{}</font></div>"
CARTOON_PICTURES = ["cartoon_picture_0.jpg", "cartoon_picture_1.jpg", "cartoon_picture_2.jpg", 
                     "cartoon_picture_3.jpg", "cartoon_picture_4.jpg", "cartoon_picture_5.jpg"]
CARTOON_KEYWORDS = ["weather", "sun", "rain", "cloud", "park", "grass", "football", "boy", "girl", "love", "kiss",
                     "happy", "joy", "heart", "trick", "betray", "cheat", "gun", "bullet", "blood", "kill", "dead"]

# loop until all pictures used
count = 0
while (count < len(CARTOON_PICTURES)):

    # get news sample
    news_sample = Speech().speech_to_text('/home/pi/PiAUISuite/VoiceCommand/speech-recog.sh')

    # check whether keyword found in sample
    for word in news_sample.split():
        if (word in CARTOON_KEYWORDS):
            
            # add box to cartoon (i.e. picture and caption)
            storage.write_value(CARTOON_FILE, CARTOON_HTML.format(CARTOON_PICTURES[count], news_sample))
            count += 1
            break
        
# display cartoon in browser
webbrowser.open(CARTOON_FILE)

So first we define some constants, which reference the cartoon webpage and pictures, as well as keywords associated with the cartoon.

Next we loop until all our pictures are matched with captions. To do this we use Google’s Speech To Text service to sample BBC News on our TV set, pressing a microphone up against the TV speaker and converting the anchorman’s words into text. Once we have a sample, we check to see if it contains any of our cartoon keywords – if it does then we use it as a caption, writing it to our cartoon webpage along with the current picture.

Finally, once all our pictures have been used up, we can display our cartoon webpage in a browser:

pi_cartoon

So romantic, it brings a tear to my eye. Go on, click on the screenshot above and feel yourself well up. Not only that, our captions are also topical.

Here’s the Speech class which handles Google’s Speech To Text service:

from subprocess import Popen, PIPE
 
class Speech(object):
 
    # converts speech to text
    def speech_to_text(self, filepath):
        try:
            # utilise PiAUISuite to turn speech into text
            text = Popen(['sudo', filepath], stdout=PIPE).communicate()[0]
 
            # tidy up text
            text = text.replace('"', '').strip()
 
            # debug
            print(text)

            return text
        except:
            print ("Error translating speech")

And here’s the Storage class which handles writing the pictures and captions to our cartoon webpage:

class Storage(object):

    # write to file
    def write_value(self, filename, value):
        try:
            with open(filename, "a") as value_file:
                value_file.write(value.strip() + '\n')
        except:
            print ("Error writing value")

‘Well,’ I said to Monita expectantly, ‘Do you have a lump in your throat?’

She curled her lip and retorted, ‘You sick man! What with all the blood?’

Oh. Does she not understand the tragedy of love? How each sweet kiss leads to betrayal? It seems not.

Anyhow, at least four years of art school did not go to waste:

cartoon_box_0

cartoon_box_1

cartoon_box_2

cartoon_box_3

cartoon_box_4

cartoon_box_5

Advertisements