#!/usr/bin/python # -*- coding: UTF-8 -*- import datetime #时间 import Adafruit_DHT #温湿度 sensor = Adafruit_DHT.DHT11 pin = 4 #GPIO4 import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) #修改全局变量需要使用global txt_src="/home/pi/Work/WorkPlace/python/2waibao/2face_lab/info" time_begin="8:00" time_over ="22:00" temperature_alram=40 humidity_alram=50 pinA1=6 #人脸识别门锁 pinA2=13 #风扇 pinB1=19 #总电源 pinB2=26 GPIO.setup(pinA1, GPIO.OUT) #设置脚为输出模式 GPIO.setup(pinA2, GPIO.OUT) #设置脚为输出模式 GPIO.setup(pinB1, GPIO.OUT) #设置脚为输出模式 GPIO.setup(pinB2, GPIO.OUT) #设置脚为输出模式 GPIO.output(pinA1, GPIO.HIGH) #high指示灯关 GPIO.output(pinA2, GPIO.HIGH) #high指示灯关 GPIO.output(pinB1, GPIO.HIGH) #high指示灯关 GPIO.output(pinB2, GPIO.HIGH) #high指示灯关 #函数名 读取txt中指定参数内容 #函数输入 # path_txt txt文件地址 # canshu 要从txt读取的内容 # fengefu 参数名字和值的分隔符号 默认 - #函数输出 # 返回字符型结果 def readtxt(path_txt,canshu): #/home/pi/Desktop/info fengefu="-" f = open(path_txt, mode='r+', encoding='utf-8') # 打开txt文件,以‘utf-8’编码读取 lines = f.readlines() # 以行的形式进行读取文件 for line in lines: a=line.strip().split(fengefu) # x.strip()#除去每行的换行符 按照:分割 b = a[0:1] # list--str c = "".join(b).strip() # 去除空格 if c==canshu: b = a[1:2] # 这是选取需要读取的位数 c="".join(b).strip() # 去除空格 #print(c) return c f.close() #获取初始化参数 #报警时间和温度,湿度范围 def read_config(): #修改全局变量需要使用global在函数内部声明下 global txt_src global time_begin global time_over global temperature_alram global humidity_alram print("---获取初始配置信息---") #读取开始时间 字符型 time_begin=readtxt(txt_src,"time_begin") print("最早时间:"+time_begin) #读取结束时间 time_over=readtxt(txt_src,"time_over") print("最晚时间:"+time_over) #读取报警温度 字符型 数字需要转化 int() temperature_alram=int(readtxt(txt_src,"temperature_alram")) print("温度高温报警:"+str(temperature_alram)) #读取湿度报警值 humidity_alram=int(readtxt(txt_src,"humidity_alram")) print("湿度报警报警:"+str(humidity_alram)) print("---获取初始配置信息结束---") #时间控制电源 def pow_timecontrol(): #获取当前和时间 now = datetime.datetime.now().strftime("%H:%M") #print("当前时间:" + now) #比较是否在时间段内 if time_begin < now < time_over: print("当前时间:" + now+",在此实验室正常开启时间段,电源打开") GPIO.output(pinB1, GPIO.LOW) #不亮 else: print("当前时间:" + now+",不在实验室正常开启时间段,电源关闭") GPIO.output(pinB1, GPIO.HIGH) #亮 # 温湿度 # 输入警报数值 其中湿度为百分比小数 def dh11_data(alram_tem,alram_hum): humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('当前温度={0:0.1f}*C 当前湿度={1:0.1f}%'.format(temperature, humidity)) if int(temperature)>int(alram_tem): print("温度警报-打开风扇散热") GPIO.output(pinA2, GPIO.LOW) #亮 else: print("温度正常-关闭风扇") GPIO.output(pinA2, GPIO.HIGH) #亮 if int(humidity)>int(alram_hum): print("湿度警报-打开风扇除湿") GPIO.output(pinA2, GPIO.LOW) #亮 else: print("湿度正常-关闭风扇") GPIO.output(pinA2, GPIO.HIGH) #亮 else: print('Failed to get reading. Try again!') #主函数 def main(): #1读取配置文件 read_config() #2电源时间管理判断 pow_timecontrol() #3温适度判断 dh11_data(int(temperature_alram),int(humidity_alram)) main()
#!/usr/bin/python # -*- coding: UTF-8 -*- import datetime #时间 import Adafruit_DHT #温湿度 sensor = Adafruit_DHT.DHT11 pin = 4 #GPIO4 import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) from multiprocessing import Process import cv2 import numpy as np import os #修改全局变量需要使用global txt_src="/home/pi/Work/WorkPlace/python/2waibao/2face_lab/info" time_begin="8:00" time_over ="22:00" temperature_alram=40 humidity_alram=50 pinA1=6 #人脸识别门锁 pinA2=26 #风扇 pinB1=19 #总电源 pinB2=13 GPIO.setup(pinA1, GPIO.OUT) #设置脚为输出模式 GPIO.setup(pinA2, GPIO.OUT) #设置脚为输出模式 GPIO.setup(pinB1, GPIO.OUT) #设置脚为输出模式 GPIO.setup(pinB2, GPIO.OUT) #设置脚为输出模式 GPIO.output(pinA1, GPIO.HIGH) #high指示灯关 GPIO.output(pinA2, GPIO.HIGH) #high指示灯关 GPIO.output(pinB1, GPIO.HIGH) #high指示灯关 GPIO.output(pinB2, GPIO.HIGH) #high指示灯关 #函数名 读取txt中指定参数内容 #函数输入 # path_txt txt文件地址 # canshu 要从txt读取的内容 # fengefu 参数名字和值的分隔符号 默认 - #函数输出 # 返回字符型结果 def readtxt(path_txt,canshu): #/home/pi/Desktop/info fengefu="-" f = open(path_txt, mode='r+', encoding='utf-8') # 打开txt文件,以‘utf-8’编码读取 lines = f.readlines() # 以行的形式进行读取文件 for line in lines: a=line.strip().split(fengefu) # x.strip()#除去每行的换行符 按照:分割 b = a[0:1] # list--str c = "".join(b).strip() # 去除空格 if c==canshu: b = a[1:2] # 这是选取需要读取的位数 c="".join(b).strip() # 去除空格 #print(c) return c f.close() #获取初始化参数 #报警时间和温度,湿度范围 def read_config(): #修改全局变量需要使用global在函数内部声明下 global txt_src global time_begin global time_over global temperature_alram global humidity_alram print("---获取初始配置信息---") #读取开始时间 字符型 time_begin=readtxt(txt_src,"time_begin") print("最早时间:"+time_begin) #读取结束时间 time_over=readtxt(txt_src,"time_over") print("最晚时间:"+time_over) #读取报警温度 字符型 数字需要转化 int() temperature_alram=int(readtxt(txt_src,"temperature_alram")) print("温度高温报警:"+str(temperature_alram)) #读取湿度报警值 humidity_alram=int(readtxt(txt_src,"humidity_alram")) print("湿度报警报警:"+str(humidity_alram)) print("---获取初始配置信息结束---") #时间控制电源 def pow_timecontrol(): #获取当前和时间 now = datetime.datetime.now().strftime("%H:%M") #print("当前时间:" + now) #比较是否在时间段内 if time_begin < now < time_over: print("当前时间:" + now+",在此实验室正常开启时间段,电源打开") GPIO.output(pinB1, GPIO.LOW) #不亮 else: print("当前时间:" + now+",不在实验室正常开启时间段,电源关闭") GPIO.output(pinB1, GPIO.HIGH) #亮 # 温湿度 # 输入警报数值 其中湿度为百分比小数 def dh11_data(alram_tem,alram_hum): humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('当前温度={0:0.1f}*C 当前湿度={1:0.1f}%'.format(temperature, humidity)) if int(temperature)>int(alram_tem): print("温度警报-打开风扇散热") GPIO.output(pinA2, GPIO.LOW) #亮 else: print("温度正常-关闭风扇") GPIO.output(pinA2, GPIO.HIGH) #亮 if int(humidity)>int(alram_hum): print("湿度警报-打开风扇除湿") GPIO.output(pinA2, GPIO.LOW) #亮 else: print("湿度正常-关闭风扇") GPIO.output(pinA2, GPIO.HIGH) #亮 else: print('FailedAQ to get reading. Try again!') def face(): recognizer = cv2.face.LBPHFaceRecognizer_create() recognizer.read('trainer/trainer.yml') cascadePath = "haarcascade_frontalface_default.xml" faceCascade = cv2.CascadeClassifier(cascadePath); font = cv2.FONT_HERSHEY_SIMPLEX #iniciate id counter id = 0 # names related to ids: example ==> Marcelo: id=1, etc names = ['None', 'me', 'he', 'Ilza', 'Z', 'W'] # Initialize and start realtime video capture cam = cv2.VideoCapture(0) cam.set(3, 640) # set video widht cam.set(4, 480) # set video height # Define min window size to be recognized as a face minW = 0.1*cam.get(3) minH = 0.1*cam.get(4) while True: ret, img =cam.read() #img = cv2.flip(img, -1) # Flip vertically gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor = 1.2, minNeighbors = 5, minSize = (int(minW), int(minH)), ) for(x,y,w,h) in faces: cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2) id, confidence = recognizer.predict(gray[y:y+h,x:x+w]) # Check if confidence is less them 100 ==> "0" is perfect match if (confidence < 100): id = names[id] confidence = " {0}%".format(round(100 - confidence)) else: id = "unknown" confidence = " {0}%".format(round(100 - confidence)) cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2) cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1) cut = img[y:y+h,x:x+w] cv2.imwrite('face_save/cut.jpg', cut) cv2.imshow('camera',img) # cam = cv2.VideoCapture(url) k = cv2.waitKey(10) & 0xff # Press 'ESC' for exiting video if k == 27: break # Do a bit of cleanup print(" [INFO] Exiting Program and cleanup stuff") cam.release() cv2.destroyAllWindows() #主函数 def main(): #1读取配置文件 read_config() #2电源时间管理判断 p1=Process(target=pow_timecontrol,args=()) #必须加,号 p2=Process(target=dh11_data,args=(int(temperature_alram),int(humidity_alram))) #必须加,号 p3=Process(target=face) #必须加,号 p1.deamon=True p2.deamon=True p3.deamon=True p1.start() p2.start() p3.start() #p1.join() #p2.join() p3.join() print("end") main()