sikun xie
Published

To realize desktop AI with raspberry pi 3b

To realize full-desktop control,A high precision gesture recognition and speech synthesis system based on raspberry pi is established

IntermediateFull instructions provided10 hours2,227
To realize desktop AI with raspberry pi 3b

Things used in this project

Hardware components

Raspberry Pi 3 Model B
×1
Raspberry Pi Camera Module V2
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Code

the code of ai control

Python
It is very simple code to realize the smart control.
#python 3
#-*-coding:utf-8-*_
#Sikun Xie

from picamera import PiCamera,Color
import socket
import binascii
import time
import demjson
from bs4 import BeautifulSoup
import requests
from pygame import mixer
from aip import AipBodyAnalysis
from aip import AipSpeech
import os,datetime,time,subprocess,shlex
import urllib.request,sys
import re
import chardet
import struct


hand={'One':'1','Five':'5','Fist':'quantou','Ok':'OK',
      'Prayer':'qidai','Congratulation':'zuoyi','Honour':'zuobie',
      'Heart_single':'bixinxin','Thumb_up':'7','Thumb_down':'Diss',
      'ILY':'woaini','Palm_up':'zhangxinxiangshang','Heart_1':'shuangshoubixinyi',
      'Heart_2':'shuangshoubixiner','Heart_3':'shuangshoubixinsan','Two':'2',
      'Three':'3','Four':'4','Six':'6','Seven':'shuziqi',
      'Eight':'shuziba','Nine':'shuzijiu','Rock':'8','Insult':'shuzhongzhi','Face':'lian'}

"""body analysis appid ak sk"""
BodyAPP_ID = '18287598'
BodyAPI_KEY = '1gsjAa1Orsds4kqsVrHu01OO'
BodySECRET_KEY = 'PEsdpFfiZvc2HjuhhtOfziBmmeVc8Gdh'

"""speech analysis appid ak sk"""
SpeechAPP_ID = '18287719'
SpeechAPI_KEY = 'OZEIuVxa19tTU2R6HimRLiI4'
SpeechSECRET_KEY = 'YDtbQ4tZ0LqYoGa3Oa9C2fPNijozyHzw'

camera = PiCamera()
Bodyclient = AipBodyAnalysis(BodyAPP_ID,BodyAPI_KEY,BodySECRET_KEY)
Speechclient = AipSpeech(SpeechAPP_ID,SpeechAPI_KEY,SpeechSECRET_KEY)
"""cam config"""
camera.resolution = (1280,720)
camera.annotate_text = "seeed"
camera.annotate_text_size = 20
camera.annotate_foreground = Color('white')
camera.brightness = 55

dayc = ["today","tomorrow","the day after tomorrow","three days later","four days later"]

"""read pic"""
def get_file_content(filePath):
    with open(filePath,'rb') as fp:
        return fp.read()

def wakeup(mac = '8C-EC-4B-7A-84-49',ip = '192.168.9.51'):
    if re.match(r"([0-9a-fA-F]{2}(-|:){0,1}){5}([a-fA-F0-9]{2})",mac) and (len(mac)==12 or len(mac)==17):
        mac = mac.replace("-",'').replace(':','').upper()
        IP,port = ip,5
        sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
        send_data = binascii.unhexlify('FF' * 6  + mac.upper() * 20)
        sock.sendto(send_data,(IP,port))
        sock.close()
        print(mac+"ok")
    else:
        print("error")   


def get_htmltext():
    try:
        r = requests.get('http://www.weather.com.cn/weather/101230101.shtml')
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return 'unusual'

def get_htmldata(html):
    final_list = []
    soup = BeautifulSoup(html,'html.parser')
    body = soup.body
    data =body.find('div',{'id':'7d'})
    ul = data.find('ul')
    lis = ul.find_all('li')
    
    for day in lis:
        temp_list = []
        data = day.find('h1').string
        temp_list.append(data)
        info = day.find_all('p')
        temp_list.append(info[0].string)
        if info[1].find('span') is None:
            temperature_highest = ' '
        else:
            temperature_highest = info[1].find('span').string
            temperature_highest = temperature_highest.replace('C',' ')
        if info[1].find('i') is None:
            temperature_lowest = ' '
        else:
            temperature_lowest = info[1].find('i').string
            temperature_lowest = temperature_lowest.replace('C',' ')
        temp_list.append(temperature_highest)
        temp_list.append(temperature_lowest)
        wind_scale = info[2].find('i').string
        temp_list.append(wind_scale)
        final_list.append(temp_list)
    return final_list

def print_html(final_list,num):
    final = final_list[num]
    c = "the highest temperature is"+final[2]+"the lowest temperature is"+final[3]
    voiwea = "Hello!"+dayc[num]+c
    voicy = Speechclient.synthesis(voiwea,'zh',6,{'vol':5,'per':2})
    if not isinstance(voicy,dict):
        with open('./wea1.mp3','wb')as f:
            f.write(voicy)
        mixer.music.load('./wea1.mp3')
        mixer.music.play()

def get_time():
    cur = datetime.datetime.now()
    mins = cur.minute
    hours = cur.hour
    
    time_now = "It is %d:%d."%(hours,mins)
    voicx = time_now
    voicy = Speechclient.synthesis(voicx,'zh',5,{'vol':5,'per':1})
    print(voicx)
    if not isinstance(voicy,dict):
        with open('./weat.mp3','wb')as f:
            f.write(voicy)
        mixer.music.load('./weat.mp3')
        mixer.music.play()

def get_date():
    cur = datetime.datetime.now()
    months = cur.month
    dates = cur.day
    
    time_date = "Today is 2020.%d.%d."%(months,dates)
    voicx = time_date
    voicy = Speechclient.synthesis(voicx,'zh',8,{'vol':5,'per':1})
    print(voicx)
    if not isinstance(voicy,dict):
        with open('./weat1.mp3','wb')as f:
            f.write(voicy)
        mixer.music.load('./weat1.mp3')
        mixer.music.play()

mixer.init()
while True:
    """take photo"""
    camera.start_preview()
    time.sleep(2)
    mixer.music.stop()
    camera.capture('./image.jpg')
    camera.stop_preview()
    image = get_file_content('./image.jpg')
    raw = str(Bodyclient.gesture(image))
    text = demjson.decode(raw)
    try:
        res = text['result'][0]['classname']
    except:
        print('result:noting')
    else:
        print('result:'+hand[res])
        if hand[res]=='6':
            get_time()
        elif hand[res]=='7':
            get_date()
        elif hand[res]=='8':
            wakeup()
        elif hand[res]=='1':
            print_html(get_htmldata(get_htmltext()),0)
        elif hand[res]=='2':            
            print_html(get_htmldata(get_htmltext()),1)   
        elif hand[res]=='3':
            print_html(get_htmldata(get_htmltext()),2) 
        elif hand[res]=='4':
            print_html(get_htmldata(get_htmltext()),3) 
        elif hand[res]=='5':
            print_html(get_htmldata(get_htmltext()),4)       
            
    time.sleep(10)            

Credits

sikun xie

sikun xie

3 projects • 6 followers

Comments