一个基于Python2.7的智慧校园系统
生活随笔
收集整理的這篇文章主要介紹了
一个基于Python2.7的智慧校园系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個基于python和pcduino的智慧校園系統,本著開(zhuang)源(bi)的精神可以用來借鑒。233。
運行在pcduino上的,需要搭配一些元件。
在程序的函數里已經寫的很詳細了。結合硬件,里面可以實現在線人臉識別,火災警報,太陽能充電控制,雨水收集灌溉,大屏幕控制,上放學語音燈控等功能。
p.s.上學鈴聲是個雙和弦的《小星星》,雖然歌很簡單,但是寫死我了-_-
至于像analog_read,digital_write,digital_read一類的函數還是需要根據嵌入設備型號自己寫的。
這個小程序是今天3月份全國創客競賽初中組特等獎的作品。個人覺得由此可見初中創客學生水平低下。勿噴。
#!/usr/bin/python2.6 # -*- coding:utf-8 -*- #--------------UTF-8!!!!!!------------- from pcduino import * import time import numpy as np import cv2 import Image import sys import os #import os.system import datetime import time import random import string# Definitions of musical notes:----------------- ISOTIME = '%Y-%m-%d %X' Z = 261.9 X = 293.7 C = 329.6 V = 349.2 B = 392.0 N = 440 M = 493.9 A = 523.3 S = 587.3 D = 659.3 F = 698.5 G = 784 H = 880 J = 987.5 Q = 1047 W = 1175 E = 1319 R = 1397 T = 1568 Y = 1760 U = 1970 X = 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} def f(x): return muse_dict[x];#definitions over。----------------------. tempthreshold = 38 smokethreshold = 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)def mute():pwm_disable(5)pwm_disable(6)def clear():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 waterreturn "question!!!"def readSmoke(port=1):return analog_read(port) >= smokethreshold #if the voltage of sensor is smaller than 300def 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)/10def LightCtrl(state,port=10):pin_mode(port,OUTPUT)if state == True:digital_write(port,HIGH)else:digital_write(port,LOW)def cls(): # clear screentemp = os.system('clear')def mediumprint(strs): ad = strsif len(ad) >= 57:print 'an error occured:string length mismatch.'total = 57 beginpoint = (total-len(ad))/2print beginpoint*' ',strs def ScreenCali(str=None): timecur = time.strftime(ISOTIME,time.localtime())mediumprint('HHHHHHHHHHHHHHHH')mediumprint(timecur)#temp = readTemp() & ' C'#mediumprint(temp) mediumprint(str)def ReadFile(filepath):file = open(filepath)try:tempa = file.read()finally:file.close()return tempadef WaterPump(open,port=2):pin_mode(port,OUTPUT)if open:digital_write(port,HIGH)else:digital_write(port,LOW) def check(a):try:f = float(a)except ValueError:return Falsereturn Truedef 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()print result#print result#print 'here is the splited data:'splresult = result.split(',')confidenceraw = splresult[-1]confidencestr = confidenceraw[-7:-1]if check(confidencestr):confidence = string.atof(confidencestr)else:confidence = 0print confidenceraw,confidencestr,confidence,'.'if confidence >= 70:return Trueelse:return False#-----------------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 = befwhile waiting:time.sleep(0.5)if bef != cur :waiting = Falsebef = curcur = 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 in range(0,len(blinkingI)):dualsound(blinkingI[i],blinkingII[i],5,6,0.4)clear() curnote = 0 needtoplay = False current = True #thedebugworldexistence = False tot = 0 while True: #大循環------------------------------------------. cls()mat = [8,5,7,2]for b in mat: #open the desired portspin_mode(b,OUTPUT) #read datas...bef = curcur = 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 = Trueif ifsmoke:changedirection(2)sound(U,volume=18)sound(T,volume=18)ScreenCali('火情發現,請學生有序撤離。')else:if tempcur >= tempthreshold:changedirection(1)else:changedirection(0)ScreenCali('浙師大附屬杭州筧橋實驗中學歡迎您!')if not issoil:WaterPump(True)else:WaterPump(False)tot = tot + 1if tot >= 3:tot = 0if facecomp():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 in notes: time.sleep(0.1)sound(a)clear() LightCtrl(False) # shutdown...?
轉載于:https://www.cnblogs.com/pityhero233/p/7246862.html
總結
以上是生活随笔為你收集整理的一个基于Python2.7的智慧校园系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IO流中文件和文件夹的删除程序举例
- 下一篇: 如何使用Topshelf管理Window