Published © CC BY-NC-SA

AI Corona Test

The corona test we all deserve, based on feelings and embodied by an extinct bird.

ExpertFull instructions providedOver 1 day105
AI Corona Test

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
MG90s
×2
Adafruit CAP1188 Touch Sensor
×1
Google AIY Voice Kit
×1
LiFePO4wered/Pi+
×1
Silver Inch Plant
×1
Lego Flower Bouquet
×1

Software apps and online services

PhotoShop
NewsAPI
DeepAI

Hand tools and fabrication machines

Super Glue
Thin Metal Hobby Bars

Story

Read more

Code

Logic Code

Python
# Generic imports
import time
import requests
import random

# News data functions
def getLatestNews():

    url = "https://newsapi.org/v2/top-headlines?q=corona&apiKey="
    apiKey = "YOUR_API_KEY"
    response = requests.get(url+apiKey)

    articleAmount = len(response.json()['articles'])
    pickedArticleIndex = random.randint(0, (articleAmount - 1))
    pickedArticle = response.json()['articles'][pickedArticleIndex]["content"]
    print(pickedArticle)

    return pickedArticle


def sentimentAnalysis(text):

    url = "https://api.deepai.org/api/sentiment-analysis"
    apiKey = "YOUR_API_KEY"

    data = {'text': text}
    headers={'api-key': apiKey}

    response = requests.post(url, headers=headers, data=data)
    output = response.json()['output']

    negativeCount = output.count("Negative") + output.count("Very negative")
    positiveCount = output.count("Positive") + output.count("Very positive")
    neutralCount = output.count("Neutral")

    print("-- Negative count --")
    print(str(negativeCount))

    print("-- Neutral count --")
    print(str(neutralCount))

    print("-- Positive count --")
    print(str(positiveCount))

    totalEmotionCount = negativeCount + positiveCount + neutralCount
    positiveTestChance = (negativeCount / totalEmotionCount)

    print("--  " + str(positiveTestChance * 100) + "% positive test chance" + "  --")

    percentageNegative = (negativeCount/totalEmotionCount) * 100
    percentagePositive = (positiveCount/totalEmotionCount) * 100
    percentageNeutral = (neutralCount/totalEmotionCount) * 100

    return positiveTestChance


latestNews = getLatestNews()

positiveTestChance = sentimentAnalysis(latestNews)
testResultThreshold = positiveTestChance * 100

randomNumber = random.randint(1,100)

text = "This dodo deems you: "

if randomNumber >= testResultThreshold:

    text += "negative!"
    print(text)
    time.sleep(2)
    print("All good, bye bye!")

else:

    text += "positive!"
    print(text)
    print("To quarantine you go!")
    

dodoCleanAnimatronics.py

Python
# Generic imports
import time
import requests
import random
import threading

# AIY imports
import aiy.voice.tts

#AIY functions
def sayText(text):
    aiy.voice.tts.say(text, lang='en-GB', volume=60, pitch=60, speed=90, device='default')

#CAP1188 imports
import board
import busio
from adafruit_cap1188.i2c import CAP1188_I2C

#Cap1188 Touch setup
print('Setting up capacitive touch')
i2c = busio.I2C(board.SCL, board.SDA)
cap = CAP1188_I2C(i2c)
cap[1].recalibrate()
time.sleep(1)
sayText("Calibrating!")

# Servo import
from gpiozero import Servo

# Servo setup
headServo = Servo(26)
wingServo = Servo(24)

wingServo.detach()
headServo.detach()

# Servo functions
sleep = 0.4

def neutral():

    print("-- Neutral --")
    headServo.mid()
    time.sleep(sleep)

def sad():

    print("-- Sad --")
    headServo.max()
    time.sleep(sleep)

def happy():

    print("-- Happy --")
    headServo.min()
    time.sleep(sleep)

flapperSpeed = 0.2
def flapperWings():

    for i in range (0,5):

        wingServo.min()
        time.sleep(flapperSpeed)

        wingServo.max()
        time.sleep(flapperSpeed)


    wingServo.mid()
    time.sleep(flapperSpeed)
    detachServos()

def flapperHead():

    for i in range (0,5):

        headServo.min()
        time.sleep(flapperSpeed)

        headServo.max()
        time.sleep(flapperSpeed)


    headServo.mid()
    time.sleep(flapperSpeed)
    detachServos()

def detachServos():
    headServo.detach()
    wingServo.detach()

testSleep = 1
def testServos():

    for i in range (0,2):

        neutral()
        time.sleep(testSleep)

        sad()
        time.sleep(testSleep)


        time.sleep(testSleep)

        neutral()
        time.sleep(testSleep)

        flapperWings()
        time.sleep(testSleep)

# News data functions
def getLatestNews():

    sayText("Gathering the data from the internet.")

    url = "https://newsapi.org/v2/top-headlines?q=corona&apiKey="
    apiKey = "YOUR_API_KEY"
    response = requests.get(url+apiKey)

    articleAmount = len(response.json()['articles'])
    pickedArticleIndex = random.randint(0, (articleAmount - 1))
    pickedArticle = response.json()['articles'][pickedArticleIndex]["content"]

    sayText("Relevant data point found, with the following title;")
    sayText(response.json()['articles'][pickedArticleIndex]['title'])

    return pickedArticle

def sentimentAnalysis(text):

    sayText("Inspecting...")

    url = "https://api.deepai.org/api/sentiment-analysis"
    apiKey = "YOUR_API_KEY"

    data = {'text': text}
    headers={'api-key': apiKey}

    response = requests.post(url, headers=headers, data=data)
    output = response.json()['output']

    negativeCount = output.count("Negative") + output.count("Very negative")
    positiveCount = output.count("Positive") + output.count("Very positive")
    neutralCount = output.count("Neutral")

    totalEmotionCount = negativeCount + positiveCount + neutralCount
    positiveTestChance = (negativeCount / totalEmotionCount)

    print("--  " + str(positiveTestChance * 100) + "% positive test chance" + "  --")

    percentageNegative = (negativeCount/totalEmotionCount) * 100
    percentagePositive = (positiveCount/totalEmotionCount) * 100
    percentageNeutral = (neutralCount/totalEmotionCount) * 100

    print("--  " + str(percentagePositive) + " positive." + "  --")
    print("--  " + str(percentageNegative) + " negative." + "  --")
    print("--  " + str(percentageNeutral) + " neutral." + "  --")

    sayText("Judging...")
    time.sleep(2.7)

    return positiveTestChance

#CAP1188 functions
def checkTouch():

    if cap[1].value:

        flapperWingsThread = threading.Thread(target=flapperWings)
        flapperHeadThread = threading.Thread(target=flapperHead)

        flapperWingsThread.start()
        flapperHeadThread.start()

       	sayText("No touching! No touching!")
        return True
    else:
        return False

# Main code
while True:

    if checkTouch():

        latestNews = getLatestNews()

        positiveTestChance = sentimentAnalysis(latestNews)
        testResultThreshold = positiveTestChance * 100

        randomNumber = random.randint(1,100)

        text = "This dodo deems you: "

        if randomNumber >= testResultThreshold:

            text += "negative!"
            sayText(text)
            time.sleep(2)

            sayText("All good, bye bye!")

        else:

            text += "positive!"
            sayText(text)
            time.sleep(2)

            sayText("To quarantine you go!")

        print(text)

Credits

Comments