Raccoon 8 months ago
parent f5e60f3e8a
commit 5d4ded5fe1

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Remote Python 3.9.2 (sftp://cxg@192.168.137.56:22/usr/bin/python3)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PublishConfigData" autoUpload="Always" serverName="cxg@192.168.137.56:22 password" remoteFilesAllowedToDisappearOnAutoupload="false">
<serverData>
<paths name="RaspberryPi">
<serverdata>
<mappings>
<mapping deploy="/ZNJJ" local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
<paths name="cxg@192.168.137.56:22 password">
<serverdata>
<mappings>
<mapping deploy="/home/cxg/BYSJ/ZNJJ" local="$PROJECT_DIR$" />
</mappings>
</serverdata>
</paths>
<paths name="cxg@192.168.213.128:22">
<serverdata>
<mappings>
<mapping local="$PROJECT_DIR$" web="/" />
</mappings>
</serverdata>
</paths>
</serverData>
<option name="myAutoUpload" value="ALWAYS" />
</component>
</project>

@ -0,0 +1,20 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N802" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="models.crnn" />
<option value="web.template" />
</list>
</option>
</inspection_tool>
</profile>
</component>

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Remote Python 3.9.2 (sftp://cxg@192.168.137.56:22/usr/bin/python3)" project-jdk-type="Python SDK" />
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/BYSJ.iml" filepath="$PROJECT_DIR$/.idea/BYSJ.iml" />
</modules>
</component>
</project>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="WebServers">
<option name="servers">
<webServer id="6131294b-40ea-4014-b4c7-88b88010fcda" name="RaspberryPi">
<fileTransfer rootFolder="/home/cxg/BYSJ" accessType="SFTP" host="192.168.137.56" port="22" sshConfigId="8908913a-364b-4ced-8be2-b1a9c3db950f" sshConfig="cxg@192.168.137.56:22 password">
<advancedOptions>
<advancedOptions dataProtectionLevel="Private" passiveMode="true" shareSSLContext="true" />
</advancedOptions>
</fileTransfer>
</webServer>
</option>
</component>
</project>

@ -0,0 +1,337 @@
import pymysql
import time
t = time.strftime('%Y-%m-%d %H:%M:%S')
# 连接数据库
def begin():
db = pymysql.connect(host='192.168.137.56',
user='root',
password='123456',
database='znjj',
charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# print("数据库连接成功!")
return cursor, db
# 使用 execute() 方法执行 SQL 查询
# cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
# data = cursor.fetchone()
# 关闭数据库连接
# db.close()
'''
============================================================================================
以上是数据库连接操作
============================================================================================
'''
def rl_insert(num, name):
cursor, db = begin()
try:
sql = "insert into user(id, name) values ('"+num+"','"+name+"')"
print(sql)
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
# rl_insert("8","312313")
# print("12345678910")
def rl_update(id ,information, name):
cursor, db = begin()
try:
sql = "update user set "+ information +" = '"+ name +"' WHERE id = "+ id
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
# rl_update("7", "name", "32131231231")
def rl_select(function):
cursor, db = begin()
# 查询语句
try:
cursor = db.cursor()
sql = "select "+ function +" from user "
cursor.execute(sql)
result = cursor.fetchall()
# for data in result:
# print(data)
except Exception:
print("查询失败")
return result
# 用户表信息删除
def rl_delete(id):
cursor, db = begin()
# SQL 删除语句
sql = "DELETE FROM user WHERE id="+ id
try:
# 执行SQL语句
cursor.execute(sql)
# 向数据库提交
db.commit()
except:
# 发生错误时回滚
db.rollback()
# 关闭连接
db.close()
# 成功提示
print("victory!")
# rl_delete("8")
def rl_Nselect(information, id):
cursor, db = begin()
# 查询语句
try:
cursor = db.cursor()
sql = "select "+ information +" from user where id = " + id
cursor.execute(sql)
result = cursor.fetchall()
# for data in result:
# print(data)
except Exception:
print("查询失败")
return result
# name = rl_Nselect("sex","6")
# print(t+name[0][0])
'''
============================================================================================
以上是居家人士user表的操作
============================================================================================
'''
def hcsr_insert(table, id, time, num):
cursor, db = begin()
try:
sql = "insert into "+ table +" (num_id, monitor_time, first_photo) values ('" + id + "','" + time + "','"+ num +"')"
print(sql)
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
t = time.strftime('%Y-%m-%d %H:%M:%S')
# hcsr_insert("hcsr", "8", t, "5")
def hcsr_update(table, information, name, use_id):
cursor, db = begin()
try:
sql = "update "+ table +" set "+ information +" = '"+ name +"' WHERE num_id = "+ use_id
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
# hcsr_update("hcsr", "last_photo", "5", "7")
def hcsr_select(table, function):
cursor, db = begin()
# 查询语句
try:
cursor = db.cursor()
sql = "select "+function+" from "+ table
cursor.execute(sql)
result = cursor.fetchall()
# for data in result:
# print(data)
except Exception:
print("查询失败")
return result
# x = hcsr_select("hcsr", "max(id)")
# print(x[0][0])
'''
============================================================================================
以上是自动监控user表的操作和视频监控m_photo表的操作
============================================================================================
'''
def check_insert(num, id, name, function, time):
cursor, db = begin()
try:
sql = "insert into check_auth (number, id, name, function_use, time) values ('"+ num +"','" + id + "','" + name + "','" + function +"','"+ time +"')"
print(sql)
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
# check_insert("2","5","常旭光","门锁", t)
def check_select(function):
cursor, db = begin()
# 查询语句
try:
cursor = db.cursor()
sql = "select "+function+" from check_auth"
cursor.execute(sql)
result = cursor.fetchall()
# for data in result:
# print(data)
except Exception:
print("查询失败")
return result
x = check_select("max(number)")
# print(x)
'''
============================================================================================
以上是查看权限操作check_auth表的操作
============================================================================================
'''
# 数据库插入信息 温湿度表
def th_insert(number, time, t_number, h_number):
cursor, db = begin()
try:
sql = "insert into th (number, check_time, temperature, humidity) values ('"+number+"','"+time+"','"+t_number+"','"+h_number+"')"
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
# th_insert("2", "2023-04-19 23:44:15","53.0","21.4")
def th_select(function):
cursor, db = begin()
# 查询语句
try:
cursor = db.cursor()
sql = "select "+function+" from th"
cursor.execute(sql)
result = cursor.fetchall()
# for data in result:
# print(data)
except Exception:
print("查询失败")
return result
'''
============================================================================================
以上是温湿度th表的操作
============================================================================================
'''
def ms_insert(num, time, state):
cursor, db = begin()
try:
sql = "insert into ms (number, time_open, state) values ('"+num+"','"+time+"','"+state+"')"
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
t = time.strftime('%Y-%m-%d %H:%M:%S')
# ms_insert("1",t,"close")
def ms_update(information, n, number):
cursor, db = begin()
try:
sql = "update ms set "+ information +" = '"+ n +"' WHERE number = "+ number
# 运行sql语句
cursor.execute(sql)
# 修改
db.commit()
# 关闭游标
cursor.close()
# 关闭连接
db.close()
print("victory!")
except:
print("false")
# ms_update("close_state", "Y", "1")
def ms_select(function):
cursor, db = begin()
# 查询语句
try:
cursor = db.cursor()
sql = "select "+function+" from ms"
cursor.execute(sql)
result = cursor.fetchall()
# for data in result:
# print(data)
except Exception:
print("查询失败")
return result
# x = ms_select("max(number)")
# print(x[0][0])
'''
============================================================================================
以上是门锁控制ms表的操作
============================================================================================
'''

@ -0,0 +1,66 @@
#!/usr/bin/python
# encoding:utf-8
# 目前用的人体红外传感器模块是最迷你型的,因此感应的距离只有一米
# 当检测到人体活动时会持续高电平24秒
import RPi.GPIO as GPIO
import time
import renlian as rl
import lighting as l
import MySQL as m
import jiqiren as jqr
import huoyan as hy
# !/usr/bin/python
# encoding:utf-8
def gy():
Infrared = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(Infrared, GPIO.IN)
for i in range(5):
t = time.strftime('%Y-%m-%d %H:%M:%S')
if (GPIO.input(Infrared) == True):
print(t + " 家居环境异常 !")
GPIO.cleanup(Infrared)
return t
else:
# print(t + " Nobody !")
time.sleep(0.1)
GPIO.cleanup(Infrared)
return 0
# gy()
# time.sleep(10) #找出感应器重置的时间
# gy()
def check_env():
hy.hy()
sign = gy()
if (sign != 0):
jqr.Key("检测到环境异常!即将启动自动监控!")
while True:
l.L_hcsr_open()
id = rl.auto_video()
if (id[1] == 0):
jqr.Key("警报解除!")
l.L_hcsr_close()
break
sign = gy()
if (sign != 0):
jqr.Key("警告警告!环境异常!")
m.hcsr_update("hcsr", "alarm_time", sign, id[0])
continue
else:
jqr.Key("警报解除!")
l.L_hcsr_close()
break
# check_env()

File diff suppressed because it is too large Load Diff

@ -0,0 +1,24 @@
#!/usr/bin/python
# encoding:utf-8
import RPi.GPIO as GPIO
import time
import jiqiren as jqr
def hy():
pin_fire = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_fire, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
for i in range(5):
status = GPIO.input(pin_fire)
if status == True:
# print('没有检测到火')
continue
else:
print('检测到火灾')
for i in range(3):
jqr.Key('请注意!检测到火灾!')
GPIO.cleanup(pin_fire)
break
GPIO.cleanup(pin_fire)

@ -0,0 +1,201 @@
import random
import pyttsx3
import requests
import yuyin as yy
import urllib
import renlian as rl
import wenshidu as wsd
import mensuo as ms
import MySQL as m
import time
import lighting as l
string_01 = ['01。','02。','03。','04。','05。','06。','07。','08。','09。','10。','11。','12。','13。','14。']
class RobotSay():
def __init__(self):
# 初始化语音
self.engine = pyttsx3.init() # 初始化语音库
# 设置语速
self.rate = self.engine.getProperty('rate')
self.engine.setProperty('rate', self.rate - 20)
self.engine.setProperty('voice', 'zh+f2')
def say(self, msg):
# 输出语音
self.engine.say(msg) # 合成语音
self.engine.runAndWait()
robotSay = RobotSay()
def talkWithRobot(msg):
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(urllib.parse.quote(msg))
html = requests.get(url)
return html.json()["content"]
robotSay = RobotSay()
# speak = Speak()
# readTalk = ReadWav()
def Say():
# while True:
'''speak.my_record() #录音
text = readTalk.predict()['result'][0] #调用百度AI接口, 将录音转化为文本信息
'''
yy.record()
text = yy.asr_updata()
# if("退出" in text):
# break
print("本人说:", text) #输出文本信息
reply(text)
return text
'''
response_dialogue = talkWithRobot(text) #调用青云客机器人回答文本信息并返回
print("青云客说:", response_dialogue) #输出回答文本信息
robotSay.say(response_dialogue) #播放回答信息
return text
'''
def Key(msg):
response_dialogue = msg
print("小溪说:", response_dialogue) # 输出回答文本信息
robotSay.say(response_dialogue) # 播放回答信息
def check_record(id, function):
num = m.check_select("max(number)")[0][0]+1
t = time.strftime('%Y-%m-%d %H:%M:%S')
name = m.rl_Nselect("name", id)[0][0]
m.check_insert(str(num), id, name, function, t)
def reply(msg):
text = msg
if('' == text):
time.sleep(0.5)
# Key('正在实现功能')
elif ("人脸识别" in text or "人脸检测" in text):
Key("正在进行人脸识别。。。")
rl.f_scan_face()
elif ("录入" in text):
num = m.rl_select("count(id)")
if(num == 0):
Key("正在进行初次人脸录入。。。")
rl.f_rec_face()
else:
id = rl.check_face()
if id != 0:
check_record(id, "录入人脸信息")
Key("正在进行人脸录入。。。")
rl.f_rec_face()
else:
Key("正在退出人脸录入。。。")
elif("修改人员" in text):
Key("请说出自己的用户编号") #如果是一位数请说0X如果是两位及以上正常说即可
id = yy.record_text()
# print(id)
if ('不知道' in id):
Key("进行人脸检测确认用户。")
id = rl.check_face()
if (id != 0):
check_record(id, "修改人员信息")
rl.rl_message_update(id)
elif(id not in string_01): #############优化
Key("不存在该用户")
else:
id = id[1: len(id) - 1]
result = m.rl_select("id")
results = ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''] ###############优化
for i in range(0, len(result)):
results[i] = result[i][0]
# print(results)
if (int(id) in results):
check_record(id, "修改人员信息")
rl.rl_message_update(id)
else:
Key("不存在该用户")
elif("查看人员" in text):
id = rl.check_face()
if (id != 0):
check_record(id, "查看人员信息")
results = m.rl_select("*")
for i in range(0, len(results)):
print(results[i])
Key("已将信息打印出来。请查看!")
time.sleep(5)
else:
Key("未识别到人脸信息,将随机说出一个具有权限的用户")
a = random.randint(1,m.rl_select("count(id)")[0][0])
name = m.rl_Nselect("name", str(a))
Key(name[0][0])
elif("删除人员" in text):
id = rl.check_face()
if (id != 0):
check_record(id, "删除人员信息")
Key("即将对"+id+"号用户的信息进行删除")
m.rl_delete(id)
Key("删除成功")
elif("权限" in text or "记录" in text):
id = rl.check_face()
if (id != 0 ):
check_record(id, "查看权限操作记录")
results = m.check_select("*")
for i in range(0 , len(results)):
print(results[i])
Key("已将记录打印出来。请查看!")
time.sleep(5)
elif( "视频监控" in text):
Key("即将进入视频监控。")
rl.manual_video()
elif("自动监控" in text):
Key("即将进入自动监控。")
id = rl.auto_video()
print("本次监控编号为:"+id)
elif("温湿度" in text):
Key("正在检测温湿度。")
wsd.wsd()
elif('开门' in text or '打开门锁' in text):
# check = rl.check_face()
# if check == 1:
# Key("正在打开门锁。。。")
# ms.km()
id = rl.check_face()
if(id != 0):
check_record(id, "门锁控制")
Key("正在打开门锁。")
ms.lock_control()
elif("打开" in text and "灯光" in text):
l.L_control_open(text)
elif("关闭" in text and "灯光" in text):
l.L_control_close(text)
elif ("退出" in text):
Key("正在退出程序。")
else:
Key(talkWithRobot(text))

@ -0,0 +1,90 @@
import RPi.GPIO as GPIO # 引入GPIO模块
import time # 引入time模块
import jiqiren as jqr
'''
'''
def L_hcsr_open():
GPIO.setmode(GPIO.BCM) # 使用BCM编号方式
GPIO.setup(13, GPIO.OUT) # 将GPIO13设置为输出模式
# 无限循环
GPIO.output(13, GPIO.HIGH) # 将GPIO13设置为高电平点亮LED
def L_hcsr_close():
GPIO.output(13, GPIO.LOW) # 将GPIO13设置为低电平熄灭LED
GPIO.cleanup(13) # 清理释放GPIO资源将GPIO复位
'''
============================================================================================
以上是关于报警灯光的设计
============================================================================================
'''
def L_living_open():
GPIO.setmode(GPIO.BCM) # 使用BCM编号方式
GPIO.setup(19, GPIO.OUT) # 将GPIO19设置为输出模式
# 无限循环
GPIO.output(19, GPIO.HIGH) # 将GPIO19设置为高电平点亮LED
def L_living_close():
GPIO.output(19, GPIO.LOW) # 将GPIO19设置为低电平熄灭LED
GPIO.cleanup(19) # 清理释放GPIO资源将GPIO复位
'''
============================================================================================
以上是关于客厅灯光的设计
============================================================================================
'''
def L_bed_open():
GPIO.setmode(GPIO.BCM) # 使用BCM编号方式
GPIO.setup(26, GPIO.OUT) # 将GPIO26设置为输出模式
# 无限循环
GPIO.output(26, GPIO.HIGH) # 将GPIO26设置为高电平点亮LED
def L_bed_close():
GPIO.output(26, GPIO.LOW) # 将GPIO26设置为低电平熄灭LED
GPIO.cleanup(26) # 清理释放GPIO资源将GPIO复位
'''
============================================================================================
以上是关于卧室灯光的设计
============================================================================================
'''
def L_control_open(text):
if ("卧室" in text):
L_bed_open()
jqr.Key("已打开卧室灯光")
elif ("客厅" in text):
L_living_open()
jqr.Key("已打开客厅灯光")
elif ("全部" in text or "所有" in text):
L_bed_open()
L_living_open()
jqr.Key("已打开全部灯光")
def L_control_close(text):
if ("卧室" in text):
L_bed_close()
jqr.Key("已关闭卧室灯光")
elif ("客厅" in text):
L_living_close()
jqr.Key("已关闭客厅灯光")
elif ("全部" in text or "所有" in text):
L_bed_close()
L_living_close()
jqr.Key("已关闭所有灯光")

@ -0,0 +1,33 @@
import yuyin as yy
import renlian as rl
import time
import jiqiren as jqr
import ganying as gy
import RPi.GPIO as GPIO # 引入GPIO模块
if __name__ == '__main__':
while True:
gy.check_env()
text = yy.record_text()
gy.check_env()
# text = '小爱'
if('小溪' in text):
jqr.Key("来了,来了!")
while True:
gy.check_env()
text = jqr.Say()
gy.check_env()
if('退出' in text):
break
elif ("退出" in text):
GPIO.cleanup()
break
elif(text not in ''):
jqr.Key("请尝试呼喊我的名字:小溪。")
else:
time.sleep(0.5)
continue

@ -0,0 +1,50 @@
#! /usr/bin/env python3
# encoding=utf-8
import RPi.GPIO as GPIO
import time
import signal
import atexit
import MySQL as m
def lock_insert(num):
t = time.strftime('%Y-%m-%d %H:%M:%S')
m.ms_insert(num, t, "open")
def lock_update(num):
m.ms_update("close_state", "Y", num)
t = time.strftime('%Y-%m-%d %H:%M:%S')
m.ms_update("time_close", t, num)
def lock_control():
atexit.register(GPIO.cleanup)
num = m.ms_select("max(number)")[0][0] + 1
servopin = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(servopin, GPIO.OUT, initial=False)
p = GPIO.PWM(servopin, 50) # 50HZ:频率就是周期脉冲的周期的倒数
p.start(0) # start(initdutycycle)占空比0-100间0表示暂不输出
time.sleep(2)
for i in range(0, 181, 10):
p.ChangeDutyCycle(2.5 + 10 * i / 180) # 设置转动角度
time.sleep(0.02) # 等该20ms周期结束
p.ChangeDutyCycle(0) # 归零信号
time.sleep(0.2)
lock_insert(str(num))
time.sleep(5)
for i in range(181, 0, -10):
p.ChangeDutyCycle(2.5 + 10 * i / 180)
time.sleep(0.02)
p.ChangeDutyCycle(0)
time.sleep(0.2)
lock_update(str(num))
GPIO.cleanup(servopin)

@ -0,0 +1,522 @@
import cv2
import numpy as np
import os
import shutil
import threading
import tkinter as tk
from PIL import Image, ImageTk
import jiqiren as jqr
import yuyin as yy
import MySQL as m
import time
# 首先读取config文件第一行代表当前已经储存的人名个数接下来每一行是idname标签和对应的人名
id_dict = {} # 字典里存的是id——name键值对
Total_face_num = 999 # 已经被识别有用户名的人脸个数,
def init(): # 将config文件内的信息读入到字典中
global Total_face_num
Total_face_num = m.rl_select('count(name)')[0][0]
def begin():
init()
# 加载OpenCV人脸检测分类器Haar
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# 准备好识别方法LBPH方法
recognizer = cv2.face.LBPHFaceRecognizer_create()
# 打开标号为0的摄像头
camera = cv2.VideoCapture(0) # 摄像头
success, img = camera.read() # 从摄像头读取照片
W_size = 0.1 * camera.get(3)
H_size = 0.1 * camera.get(4)
system_state_lock = 0 # 标志系统状态的量 0表示无子线程在运行 1表示正在刷脸 2表示正在录入新面孔。
# 相当于mutex锁用于线程同步
a = (face_cascade, recognizer, success, W_size, H_size, system_state_lock, camera)
return a
'''
============================================================================================
以上是初始化
============================================================================================
'''
def Get_new_face(a):
# a = (face_cascade, recognizer, success, W_size, H_size, system_state_lock ,camera)
face_cascade = a[0]
recognizer = a[1]
W_size = a[3]
H_size = a[4]
camera = a[6]
jqr.Key("请对该面部信息进行命名:")
yy.record()
name = yy.asr_updata()
l = len(name)
name = name[0: l-1]
print("正在从摄像头录入新人脸信息 \n")
# 存在目录data就清空不存在就创建确保最后存在空的data目录
filepath = "data"
if not os.path.exists(filepath):
os.mkdir(filepath)
else:
shutil.rmtree(filepath)
os.mkdir(filepath)
sample_num = 0 # 已经获得的样本数
while True: # 从摄像头读取图片
global success
global img # 因为要显示在可视化的控件内,所以要用全局的
success, img = camera.read()
cv2.imshow("capture", img) # 生成摄像头窗口
# 转为灰度图片
if success is True:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
return name
# 检测人脸将每一帧摄像头记录的数据带入OpenCv中让Classifier判断人脸
# 其中gray为要检测的灰度图像1.3为每次图像尺寸减小的比例5为minNeighbors
face_detector = face_cascade
faces = face_detector.detectMultiScale(gray, 1.3, 5)
# 框选人脸for循环保证一个能检测的实时动态视频流
for (x, y, w, h) in faces:
# xy为左上角的坐标,w为宽h为高用rectangle为人脸标记画框
cv2.rectangle(img, (x, y), (x + w, y + w), (255, 0, 0))
# 样本数加1
sample_num += 1
# 保存图像把灰度图片看成二维数组来检测人脸区域这里是保存在data缓冲文件夹内
T = Total_face_num
cv2.imwrite("./data/User." + str(T) + '.' + str(sample_num) + '.jpg', gray[y:y + h, x:x + w])
pictur_num = 30 # 表示摄像头拍摄取样的数量,越多效果越好,但获取以及训练的越慢
cv2.waitKey(1)
if sample_num > pictur_num:
return name
else: # 控制台内输出进度条
l = int(sample_num / pictur_num * 50)
r = int((pictur_num - sample_num) / pictur_num * 50)
print("\r" + "%{:.1f}".format(sample_num / pictur_num * 100) + "=" * l + "->" + "_" * r, end="")
# var.set("%{:.1f}".format(sample_num / pictur_num * 100)) # 控件可视化进度信息
# tk.Tk().update()
# window.update() # 刷新控件以实时显示进度
def Train_new_face():
print("\n正在训练")
# cv2.destroyAllWindows()
path = 'data'
# 初始化识别的方法
recog = cv2.face.LBPHFaceRecognizer_create()
# 调用函数并将数据喂给识别器训练
faces, ids = get_images_and_labels(path)
print('本次用于训练的识别码为:') # 调试信息
print(ids) # 输出识别码
# 训练模型 #将输入的所有图片转成四维数组
recog.train(faces, np.array(ids))
# 保存模型
yml ='/home/cxg/BYSJ/RL-data/' + str(Total_face_num) + ".yml"
print(yml)
rec_f = open(yml, "w+")
rec_f.close()
recog.save(yml)
# recog.save('aaa.yml')
# 创建一个函数,用于从数据集文件夹中获取训练图片,并获取id
# 注意图片的命名格式为User.id.sampleNum
def get_images_and_labels(path):
image_paths = [os.path.join(path, f) for f in os.listdir(path)]
# 新建连个list用于存放
face_samples = []
ids = []
# 遍历图片路径导入图片和id添加到list中
for image_path in image_paths:
# 通过图片路径将其转换为灰度图片
img = Image.open(image_path).convert('L')
# 将图片转化为数组
img_np = np.array(img, 'uint8')
if os.path.split(image_path)[-1].split(".")[-1] != 'jpg':
continue
# 为了获取id将图片和路径分裂并获取
id = int(os.path.split(image_path)[-1].split(".")[1])
# 调用熟悉的人脸分类器
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = detector.detectMultiScale(img_np)
# 将获取的图片和id添加到list中
for (x, y, w, h) in faces:
face_samples.append(img_np[y:y + h, x:x + w])
ids.append(id)
return face_samples, ids
def write_config(name):
print("新人脸训练结束")
T = Total_face_num
m.rl_insert(str(T),name,)
jqr.Key("是否要设置其他个人信息?")
text = yy.record_text()
if ("" in text):
information_update(str(T), "年龄")
information_update(str(T), "性别")
information_update(str(T), "电话号码")
information_update(str(T), "生日")
jqr.Key("完成录入信息")
'''
============================================================================================
以上是录入新人脸信息功能的实现
============================================================================================
'''
def scan_face(a):
# 使用之前训练好的模型
# a = (face_cascade, recognizer, success, W_size, H_size, system_state_lock ,camera)
face_cascade = a[0]
recognizer = a[1]
W_size = a[3]
H_size = a[4]
camera = a[6]
for i in range(Total_face_num): # 每个识别器都要用
i += 1
yml ='/home/cxg/BYSJ/RL-data/' + str(i) + ".yml"
print("\n本次:" + yml) # 调试信息
recognizer.read(yml)
ave_poss = 0
for times in range(10): # 每个识别器扫描十遍
times += 1
cur_poss = 0
global success
global img
'''
global system_state_lock
while system_state_lock == 2: # 如果正在录入新面孔就阻塞
print("\r刷脸被录入面容阻塞", end="")
pass
'''
success, img = camera.read()
cv2.imshow("capture", img) # 生成摄像头窗口
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 识别人脸
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(int(W_size), int(H_size))
)
# 进行校验
for (x, y, w, h) in faces:
'''
global system_state_lock
while system_state_lock == 2: # 如果正在录入新面孔就阻塞
print("\r刷脸被录入面容阻塞", end="")
pass
'''
# 这里调用Cv2中的rectangle函数 在人脸周围画一个矩形
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 调用分类器的预测函数,接收返回值标签和置信度
idnum, confidence = recognizer.predict(gray[y:y + h, x:x + w])
conf = confidence
# 计算出一个检验结果
if confidence < 100: # 可以识别出已经训练的对象——直接输出姓名在屏幕上
if idnum in id_dict:
user_name = id_dict[idnum]
else:
# print("无法识别的ID:{}\t".format(idnum), end="")
user_name = "Untagged user:" + str(idnum)
confidence = "{0}%", format(round(100 - confidence))
else: # 无法识别此对象,那么就开始训练
user_name = "unknown"
# print("检测到陌生人脸\n")
# cv2.destroyAllWindows()
# global Total_face_num
# Total_face_num += 1
# Get_new_face() # 采集新人脸
# Train_new_face() # 训练采集到的新人脸
# write_config() # 修改配置文件
# recognizer.read('aaa.yml') # 读取新识别器
# 加载一个字体用于输出识别对象的信息
font = cv2.FONT_HERSHEY_SIMPLEX
# 输出检验结果以及用户名
cv2.putText(img, str(user_name), (x + 5, y - 5), font, 1, (0, 0, 255), 1)
cv2.putText(img, str(confidence), (x + 5, y + h - 5), font, 1, (0, 0, 0), 1)
# 展示结果
# cv2.imshow('camera', img)
print("conf=" + str(conf), end="\t")
if 35 > conf > 0:
cur_poss = 1 # 表示可以识别
elif 75 > conf > 35:
cur_poss = 1 # 表示可以识别
else:
cur_poss = 0 # 表示不可以识别
k = cv2.waitKey(1)
if k == 27:
# cam.release() # 释放资源
cv2.destroyAllWindows()
break
ave_poss += cur_poss
if ave_poss >= 5: # 有一半以上识别说明可行则返回
return i
return 0 # 全部过一遍还没识别出说明无法识别
'''
============================================================================================
以上是关于刷脸功能的设计
============================================================================================
'''
def information_update(T, str):
if ("生日" in str):
birthday_update(T)
elif (str in "年龄,性别,姓名,电话"):
jqr.Key("该用户"+str+"为:")
str = change(str)
text = yy.record_text()
text = text[0: len(text) - 1]
# print(text,T)
m.rl_update(T, str, text)
def birthday_update(T):
message = ['','','']
jqr.Key("该用户出生年份为:")
text = yy.record_text()
message[0] = text[0: len(text) - 1]
jqr.Key("该用户出生月份为:")
text = yy.record_text()
message[1] = text[0: len(text) - 1]
jqr.Key("该用户出生日期为:")
text = yy.record_text()
message[2] = text[0: len(text) - 1]
m.rl_update(str(T), "birthday", str(message[0]) + "-" + str(message[1]) + "-" + str(message[2]))
def change(str):
if ("姓名" in str):
return "name"
elif ("年龄" in str):
return "age"
elif ("性别" in str):
return "sex"
elif ("电话" in str):
return "phone"
def rl_message_update(id):
while True:
jqr.Key("需要修改什么信息?")
text = yy.record_text()
if ("取消" in text):
break
elif (text not in ""):
text = text[0: len(text) - 1]
information_update(id, text)
'''
============================================================================================
以上是修改人脸信息功能的实现
============================================================================================
'''
def f_scan_face():
# 使用之前训练好的模型
# recognizer.read('aaa.yml')
# var.set('刷脸')
a = begin() #申请摄像头资源
ans = scan_face(a)
if ans == 0:
# print("最终结果:无法识别")
# var.set("最终结果:无法识别")
jqr.Key("人脸识别完毕。最终结果:检测失败")
cv2.destroyAllWindows()
a[6].release()
return 0,0
else:
id_dict = m.rl_select('id')
name_dict = m.rl_select("name")
ans_name = "最终结果User" + str(id_dict[ans-1][0]) + name_dict[ans-1][0]
# print(ans_name)
# var.set(ans_name)
jqr.Key("人脸识别完毕。"+ ans_name)
jqr.Key("人脸检测成功。。。")
cv2.destroyAllWindows()
a[6].release()
return 1,str(id_dict[ans-1][0])
def f_rec_face():
a = begin() #申请摄像头资源
# var.set('录入')
global Total_face_num
Total_face_num += 1
name = Get_new_face(a) # 采集新人脸
print("采集完毕,开始训练")
jqr.Key("人脸录入完毕。")
global system_state_lock # 采集完就可以解开锁
# print("锁被释放0")
system_state_lock = 0
Train_new_face() # 训练采集到的新人脸
write_config(name) # 修改数据库
cv2.destroyAllWindows()
a[6].release()
# recognizer.read('aaa.yml') # 读取新识别器
# global system_state_lock
# print("锁被释放0")
# system_state_lock = 0 # 修改system_state_lock,释放资源
def check_face():
while True:
jqr.Key("即将进行人脸检测。。。。")
sign = f_scan_face()
# print(sign)
if(0 in sign):
jqr.Key("人脸检测失败,是否要重新检测。。。")
yy.record()
text = yy.asr_updata()
print("本人说:", text)
if("" in text or "重新检测" in text):
continue
elif("" in text or "取消" in text):
return 0
else:
jqr.Key("未检测到关键字!将继续进行人脸检测。。。")
continue
else:
return sign[1]
'''
============================================================================================
以上是关于人脸的执行设计
============================================================================================
'''
def manual_video(): #视频监控
camera = cv2.VideoCapture(0) # 摄像头
print('开始视频监控')
id = m.hcsr_select("m_photo", "max(num_id)")[0][0] + 1 # 确认操作编号
t = time.strftime('%Y-%m-%d %H:%M:%S')
photoname = m.hcsr_select("m_photo", "max(last_photo)")[0][0] + 1 # 文件名序号初始值
temp = photoname # 记录第一张照片的临时变量
m.hcsr_insert("m_photo", str(id), t, str(photoname)) # 保存数据
while True:
# get a frame
ret, frame = camera.read()
frame = cv2.flip(frame, 1) # 摄像头是和人对立的,将图像左右调换回来正常显示
# show a frame
cv2.imshow("capture", frame) # 生成摄像头窗口
if cv2.waitKey(1) & 0xFF == ord('q'): # 如果按下q 就截图保存并退出
cv2.imwrite('/home/cxg/BYSJ/manual-photo/' +"m_video" + str(photoname) + ".png", frame) # 保存路径
cv2.destroyAllWindows()
m.hcsr_update("m_photo", "last_photo", str(photoname), str(id)) # 保存最后一张照片的数据
m.hcsr_update("m_photo", "num", str(photoname - temp + 1), str(id))
break
elif cv2.waitKey(1) & 0xFF == ord('z'):
cv2.imwrite('/home/cxg/BYSJ/manual-photo/' +"m_video" + str(photoname) + ".png", frame) # 保存路径
m.hcsr_update("m_photo", "last_photo", str(photoname), str(id)) # 保存最后一张照片的数据
m.hcsr_update("m_photo", "num", str(photoname - temp + 1), str(id))
photoname += 1
continue
camera.release()
def auto_video(): #自动监控
camera = cv2.VideoCapture(0) # 摄像头
print('开始自动监控')
i = 0 # 定时装置初始值
id = m.hcsr_select("hcsr", "max(num_id)")[0][0] + 1 #确认操作编号
t = time.strftime('%Y-%m-%d %H:%M:%S')
photoname = m.hcsr_select("hcsr", "max(last_photo)")[0][0] + 1 # 文件名序号初始值
temp = photoname #记录第一张照片的临时变量
m.hcsr_insert("hcsr", str(id), t, str(photoname)) #保存数据
while True:
i = i + 1 #计时
reg, frame = camera.read()
frame = cv2.flip(frame, 1) # 图片左右调换
cv2.imshow('window', frame)
if i == 50: # 定时装置,定时截屏,可以修改。
filename = "a_video" + str(photoname) + '.png' # filename为图像名字将photoname作为编号命名保存的截图
cv2.imwrite('/home/cxg/BYSJ/aut-photo/' + filename, frame) # 截图 前面为放在桌面的路径 frame为此时的图像
print(filename + '保存成功') # 打印保存成功
i = 0 # 清零
m.hcsr_update("hcsr", "last_photo", str(photoname), str(id)) #保存最后一张照片的数据
m.hcsr_update("hcsr", "num", str(photoname - temp+1), str(id))
photoname += 1
if photoname - temp >= 20: # 最多截图20张 然后退出如果调用photoname = 1 不用break为不断覆盖图片
# photoname = 1
cv2.destroyAllWindows()
# 释放资源
camera.release()
return str(id),1
if cv2.waitKey(1) & 0xff == ord('q'):
cv2.destroyAllWindows()
# 释放资源
camera.release()
return str(id),0
cv2.destroyAllWindows()
# 释放资源
camera.release()
'''
============================================================================================
以上是关于监控的设计
============================================================================================
'''

@ -0,0 +1,150 @@
# 使用方法: 接线 DHT11的三个引脚(如果是四个引脚NC不连接) VCC接5V GND接地 数据D接任意树莓派GPIO口
# 并将数据D接的树莓派GPIO口BCM编号设置为下面的pin
# 然后你可以直接运行本文件得到数值也可以使用import dht11_drive导入模块
# 导入后使用 dht11_drive.dht11() 函数获取湿度与温度 返回值是一个元组
import RPi.GPIO as GPIO
import time
import jiqiren as jqr
import MySQL as m
def celiang(pin):
GPIO.setup(pin, GPIO.OUT) # 設置引腳輸出
GPIO.output(pin, True) # 高电平
# 树莓派下拉19ms
GPIO.output(pin, False)
time.sleep(0.02)
# GPIO.output(pin,True)
GPIO.setup(pin, GPIO.IN) # 设置引腳输入
time_data_list = [] # 保存数据(高电平持续)时间的列表
pan_duan = 1 # 判断数据是否正确
end_times = 0 # 数据是否接收完毕 决定是否结束大循环
start_time = 0 # 防止start_time未定义
end_time = 0 # 防止end_time未定义
while 1:
start_time_total = time.time() # 记录每次运行循环开始时间
if end_times == 1: # 数据接收完毕
break
while GPIO.input(pin) == 0: # 等待电平不为低
start_time = time.time() # 得到高电平开始时间
if (start_time - start_time_total) > 0.1: # 判断循环时间过长 电平已经不再变化
end_times = 1
break
while GPIO.input(pin) == 1: # 等待电平不为高
end_time = time.time() # 得到高电平结束时间
if (end_time - start_time_total) > 0.1: # 判断循环时间过长 电平已经不再变化
end_times = 1
break
time_data = end_time - start_time # 计算出高电平持续时间
time_data_list.append(time_data) # 高电平持续时间数据写入列表
# print(str(time_data)+', ', end='')
# print(time_data_list)
# print(len(time_data_list))
new_time_data_list = [] # 保存筛选过的时间
for i in range(len(time_data_list)):
if (time_data_list[i] > 1.9e-5) and (time_data_list[i] < 7.8e-5):
new_time_data_list.append(time_data_list[i])
# print(new_time_data_list)
# print(len(new_time_data_list))
if len(new_time_data_list) != 40:
pan_duan = 0 # 判断数据是否正确
return pan_duan, '接收到的数据位数不足,请重新尝试'
if pan_duan == 1: # 判断数据是否正确
Binary_list = [] # 保存二进制数据
for i in new_time_data_list:
if i > 5.0e-5:
Binary_list.append(1)
elif i < 5.0e-5:
Binary_list.append(0)
if len(Binary_list) != 40:
pan_duan = 0 # 判断数据是否正确
return pan_duan, '数据位数不足,请重新尝试'
# print(Binary_list)
# print(len(Binary_list))
if pan_duan == 1: # 判断数据是否正确
# 转字符串格式
str_Binary_list = []
for i in Binary_list:
str_Binary_list.append(str(i))
if pan_duan == 1: # 判断数据是否正确
# 分割数据 并合并成字符串
# 共40位数据 0-8为湿度整数 9-16为湿度小数 17-24为温度整数 25-32为湿度小数 33-40为校验和前四个数据直接相加
str_shidu_zhengshu = ''.join(str_Binary_list[0:8])
str_shidu_xiaoshu = ''.join(str_Binary_list[8:16])
str_wendu_zhengshu = ''.join(str_Binary_list[16:24])
str_wendu_xiaoshu = ''.join(str_Binary_list[24:32])
str_yanzheng = ''.join(str_Binary_list[32:40])
# 转换为十进制
int_shidu_zhengshu = int(str_shidu_zhengshu, 2)
int_shidu_xiaoshu = int(str_shidu_xiaoshu, 2)
int_wendu_zhengshu = int(str_wendu_zhengshu, 2)
int_wendu_xiaoshu = int(str_wendu_xiaoshu, 2)
int_yanzheng = int(str_yanzheng, 2)
if int_yanzheng != (int_shidu_zhengshu + int_shidu_xiaoshu + int_wendu_zhengshu + int_wendu_xiaoshu):
pan_duan = 0 # 数据不正确
return pan_duan, '校验和不匹配,请重新尝试'
if int_yanzheng == (int_shidu_zhengshu + int_shidu_xiaoshu + int_wendu_zhengshu + int_wendu_xiaoshu):
pan_duan = 1 # 数据正确
str_shidu = str(int_shidu_zhengshu) + '.' + str(int_shidu_xiaoshu)
str_wendu = str(int_wendu_zhengshu) + '.' + str(int_wendu_xiaoshu)
float_shidu = float(str_shidu)
float_wendu = float(str_wendu)
return pan_duan, (float_shidu, float_wendu) # 返回 湿度 温度
def dht11(pin): # 循环五次
for i in range(5):
pan_duan_01, return_data = celiang(pin)
if pan_duan_01 == 1:
return return_data
# time.sleep(0.5)
# print('错误')
return 0, 0
def wsd():
#############################################################
pin = 4 # 这里填写GPIO号BCM引脚编号模式 手动改变此行
#############################################################
GPIO.setmode(GPIO.BCM) # 设置使用BCM引脚编号模式
while True:
shidu, wendu = dht11(pin)
if shidu and wendu:
num = m.th_select("max(number)")[0][0]+1
t = time.strftime('%Y-%m-%d %H:%M:%S')
m.th_insert(str(num), t, str(wendu), str(shidu)) # 将数据保存至数据库
text = '湿度为' + str(shidu) + '%' + ',温度为' + str(wendu) + '℃。'
print("小溪说:", text)
txt = '湿度为百分之' + str(shidu) + ',温度为' + str(wendu) + '摄氏度。'
jqr.robotSay.say(txt)
GPIO.cleanup(pin)
break
else:
continue
GPIO.cleanup(pin)

@ -0,0 +1,88 @@
import wave
from pyaudio import PyAudio,paInt16 #导入音频处理库Pyaudio没有的话要pip 安装
from aip import AipSpeech
# 设置采样参数
NUM_SAMPLES = 2000
# 默认录音4s
TIME = 4
# 百度智能云平台语音技能密钥
# 请输入您的BaiduAPP_ID下面自行调用应用哦~
BaiduAPP_ID = '29635326'
# 请输入您的BaiduAPI_KEY
BaiduAPI_KEY = 'ge6RRX98jSIvwALymhE4iaiY'
# 请输入您的SECRET_KEY
SECRET_KEY = '6GiDv6A55fRZFNSC3Ds6kcUAGwFhrgPt'
client = AipSpeech(BaiduAPP_ID, BaiduAPI_KEY, SECRET_KEY)
# 保存录音文件
def save_wave_file(filename,data):
wf = wave.open(filename,'wb') # 打开WAV文档
wf.setnchannels(1) #配置声道数
wf.setsampwidth(2) #配置量化位数
wf.setframerate(16000) #采样频率
wf.writeframes(b"".join(data)) # 将wav_data转换为二进制数据写入文件
wf.close()
# 定义录音函数
def record():
print('Start recording.')
# 实例化PyAudio对象开启声音输入
pa = PyAudio()
# 打开输入流并设置音频采样参数 1 channel 16K framerate
stream = pa.open(format = paInt16,
channels = 1,
rate = 16000,
input = True,
frames_per_buffer = NUM_SAMPLES)
# 录音缓存数组
audioBuffer = []
# 循环采集音频 默认录制4s
count = 0
while count<TIME*10:
# 一次性录音采样字节的大小
string_audio_data = stream.read(NUM_SAMPLES)
audioBuffer.append(string_audio_data)
count +=1
# 加逗号不换行输出
print('.', end='')
print('')
print('End recording.')
# 保存录制的语音文件到audio.wav中并关闭输入流
save_wave_file('./audio.wav',audioBuffer)
stream.close()
# 语音识别函数
def asr_updata():
with open('./audio.wav', 'rb') as f:
audio_data = f.read()
result = client.asr(audio_data,
'wav', 16000, { # 采样频率16K
'dev_pid': 1537,
# 1536 普通话
# 1537 普通话(纯中文识别)
# 1737 英语
# 1637 粤语
# 1837 四川话
})
# print(result) #打印出来,报错的时候可以查看代码
val = 'result' in result.keys()
# print("val:",val)
if val == True:
result_text = result["result"][0]
else:
result_text = '语音未识别'
return result_text
def record_text():
record()
text = asr_updata()
print("本人说:", text) #输出文本信息
return text
'''
if __name__ == '__main__':
record() #调用录音函数
result_text = asr_updata()
print(result_text)
'''
Loading…
Cancel
Save