#!/usr/bin/python2.6#-*- coding:utf-8 -*-#--------------UTF-8!!!!!!-------------
from pcduino import *
importtimeimportnumpy as npimportcv2importImageimportsysimportos#import os.system
importdatetimeimporttimeimportrandomimportstring#Definitions of musical notes:-----------------
ISOTIME = '%Y-%m-%d %X'Z= 261.9X= 293.7C= 329.6V= 349.2B= 392.0N= 440M= 493.9A= 523.3S= 587.3D= 659.3F= 698.5G= 784H= 880J= 987.5Q= 1047W= 1175E= 1319R= 1397T= 1568Y= 1760U= 1970X=0#muse_dict ={'.1'=261.9,'.2'=293.7,'.3'=329.6,'.4'=349.2,'.5'=392.0,'.6'=440,'.7'=493.9,'1'=523.3,'2'=587.3,'3'=659.3,'4'=698.5,'5'=784,'6'=880,'7'=987.5,'1.'=1047,'2.'=1175,'3.'=1319,'4.'=1397,'5.'=1568,'6.'=1760,'7.'=1970}
deff(x):returnmuse_dict[x];#definitions over。----------------------.
tempthreshold = 38smokethreshold= 300nums=string.digits
cam=cv2.VideoCapture(0)#facedetection.-----------------.
IMAGE_FR = "FACE" #frontal name...
_CONTENT_TYPES = { 'image/png': '.png', 'image/gif': '.gif','image/jpeg':'.jpg', 'image/jpeg': '.jpeg' } #supporting types dictionary
#def lenIM(text):#n = 0#for i in text:#if i not in ' \n!"#$%^&*?/.:;[\\]@_-|`~'#n ++#return n
def sound(muse,port=5,times=0.5,volume=8):if muse !=0:
pin_mode(port,PWM)#sound a note
pwm_set(port,volume,muse)
pwm_enable(port)
time.sleep(times)else:
time.sleep(times)def Soil(port = 4):return analog_read(port) >= 100
#def dualsound(musea,museb,porta=5,portb=6,times=0.5):#pin_mode(porta,pwm)#pin_mode(portb,pwm)#pwm_set(porta,8,musea)#pwm_set(portb,5,museb)#pwm_enable(porta)#pwm_enable(portb)#time.sleep(times)
def dualsound(musea,museb,porta,portb,time=0.5):
sound(musea,porta,0.1,8)
sound(museb,portb,time,4)
pwm_disable(porta)
pwm_disable(portb)defmute():
pwm_disable(5)
pwm_disable(6)defclear():for i in range(1,10): #clear all
pin_mode(i,0)
digital_write(i,0)
pwm_disable(5)def readWater(port=1):return analog_read(port) >= 2000 #if there is water and how deep?
def readHumid(port=5):#found a question!!!!the port is definitly not this(5).
pin_mode(port,INPUT) #if there is water
return "question!!!"
def readSmoke(port=1):return analog_read(port) >= smokethreshold #if the voltage of sensor is smaller than 300
def changedirection(normal = 1,porta=1,portb=0):
pin_mode(porta,OUTPUT)
pin_mode(portb,OUTPUT)if normal ==0:
digital_write(portb,LOW)
digital_write(porta,LOW)elif normal == 1:
digital_write(portb,HIGH)
digital_write(porta,LOW)elif normal == 2:
digital_write(porta,HIGH)
digital_write(portb,LOW)def readTemp(port=3):return analog_read(port)/10
def LightCtrl(state,port=10):
pin_mode(port,OUTPUT)if state ==True:
digital_write(port,HIGH)else:
digital_write(port,LOW)def cls(): #clear screen
temp = os.system('clear')defmediumprint(strs):
ad=strsif len(ad) >= 57:print 'an error occured:string length mismatch.'total= 57beginpoint= (total-len(ad))/2
print beginpoint*' ',strsdef ScreenCali(str=None):
timecur=time.strftime(ISOTIME,time.localtime())
mediumprint('HHHHHHHHHHHHHHHH')
mediumprint(timecur)#temp = readTemp() & ' C'
#mediumprint(temp)
mediumprint(str)defReadFile(filepath):
file=open(filepath)try:
tempa=file.read()finally:
file.close()returntempadef WaterPump(open,port=2):
pin_mode(port,OUTPUT)ifopen:
digital_write(port,HIGH)else:
digital_write(port,LOW)defcheck(a):try:
f=float(a)exceptValueError:returnFalsereturnTruedef facecomp(): #face recognization
#print 'start!'
cmd = 'curl -X POST "https://api-cn.faceplusplus.com/facepp/v3/compare" -F "api_key=jb0zKEEB2jiG2GMNt29Gej-GxNTPbeCM" -F "api_secret=5AHULy15PjagzVvnHLUa2ouVJpWCPHMZ" -F "face_token1=f216f8b56678831f6dc5720ae10f341f" -F "image_file2=@photo.jpg"'
#print 'here is the datas!'
result =os.popen( cmd ).read()printresult#print result
#print 'here is the splited data:'
splresult = result.split(',')
confidenceraw= splresult[-1]
confidencestr= confidenceraw[-7:-1]ifcheck(confidencestr):
confidence=string.atof(confidencestr)else:
confidence=0print confidenceraw,confidencestr,confidence,'.'
if confidence >= 70:returnTrueelse:returnFalse#-----------------START-------------#tempb = os.system('rm /var/log/apache2/access.log')
tempb = os.system('mkdir /var/log/apache2')
tempb= os.system('cd /var/log/apache2')
tempb= os.system('touch /var/log/apache2/access.log')
tempb= os.system('/etc/init.d/apache2 restart')
cls()
waiting=True
bef= ReadFile('/var/log/apache2/access.log')
cur=befwhilewaiting:
time.sleep(0.5)if bef !=cur :
waiting=False
bef=cur
cur= ReadFile('/var/log/apache2/access.log')#notes = [E,E,E,E,D,C,C,B1,A1,A1,B1,C,E,A,A,A,A,G,F,F,E,D,D,E,F,E,F,E,G,F,E,E,D,C,C,B1,A1,B1,B1,B1,B1,C,B1,A1] #爱的罗曼史#notes = [A]
notes =[Q,J,H,G,D,D,S,A,S,G,D,D,S,A,S,A,A,A]#notesII = [C,G,G,G,G,G,C,G,G,G,G,C,G,G,G,G,C,A,A,A,A,D,A,A,A,A,A,B1,G,C,G,C,G,G,G,G,G,C,G,B1,G,A1,E,E,E,E,A1,E,E,E,E,F1,C,C,C,C,C2]
blinkingI =[A]#blinkingI = [Q,X,Q,X,T,X,T,X,Y,X,Y,X,T,X,X,X,R,X,R,X,E,X,E,X,W,X,W,X,Q]
blinkingII =[A,G,D,G,A,G,D,G,A,H,F,H,A,G,D,G,M,G,S,G,A,G,D,G,M,G,S,G,A,G,D,G,A]for i inrange(0,len(blinkingI)):
dualsound(blinkingI[i],blinkingII[i],5,6,0.4)
clear()
curnote=0
needtoplay=False
current=True#thedebugworldexistence = False
tot =0while True: #大循环------------------------------------------.
cls()
mat= [8,5,7,2]for b in mat: #open the desired ports
pin_mode(b,OUTPUT) #read datas...
bef =cur
cur= ReadFile('/var/log/apache2/access.log')#watercur = readWater()#humidcur = readHumid()
tempcur =readTemp()
ifsmoke= readSmoke() #read ended.
_, frame =cam.read()
issoil=Soil()
tempb= os.system('sudo rm /home/photo.jpg')
cv2.imwrite('/home/photo.jpg',frame)#face = facecomp()
#thedebugworldexistence = True
ififsmoke:
changedirection(2)
sound(U,volume=18)
sound(T,volume=18)
ScreenCali('火情发现,请学生有序撤离。')else:if tempcur >=tempthreshold:
changedirection(1)else:
changedirection(0)
ScreenCali('浙师大附属杭州笕桥实验中学欢迎您!')if notissoil:
WaterPump(True)else:
WaterPump(False)
tot= tot + 1
if tot >= 3:
tot=0iffacecomp():
sound(U,volume=18)
sound(T,volume=18)### --------!!!--------- ###
#if face != None:
#cv2.namedWindow('Image')
#cv2.imshow('Video',face)
#cv2.waitKey(0)
#cv2.destroyWindow('Video')
time.sleep(1)if cur !=bef:
tempb= os.system('sudo rm /home/photo.jpg')break #found user terminated the program.#-----while over-------------------------------------------------.
digital_write(0,HIGH)
digital_write(0,LOW)
digital_write(7,LOW)
digital_write(10,LOW)
digital_write(0,HIGH)
digital_write(1,HIGH)for a innotes:
time.sleep(0.1)
sound(a)
clear()
LightCtrl(False)#shutdown...