一个基于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 = 300

nums = 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 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')

def mediumprint(strs): 
    ad = strs
    if len(ad) >= 57:
        print 'an error occured:string length mismatch.'
    total = 57 
    beginpoint = (total-len(ad))/2
    print 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 tempa

def 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 False
        return True


def 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 = 0
    print confidenceraw,confidencestr,confidence,'.'
    if confidence >= 70:
        return True
    else:
        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 = bef

while waiting:
    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 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 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
        if 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 + 1
        if tot >= 3:
            tot = 0
            if 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...

 

posted @ 2017-07-27 20:06  pityhero  阅读(1207)  评论(0编辑  收藏  举报