核心内容摘要
还在纠结?四川BBBB嗓VSXXXBBBB嗓,谁才是你的练声“天选之子”?
这里我没有给 ESP8266 刷入 MicroPython 固件而是用的Python和Mixly在Mixly上写的代码这个是一个可以图形化的软件也可以进行部分代码编写当然复杂程序还是用Arduino IDE这边两者都可实现以下仅展示用Mixly
0进行的操作且是HTTP服务器版本非MQTT版本。
此篇是在该篇基础上进行了优化可点此处直接跳转引入了人脸识别即校验到指定人脸才亮灯用了face_recognition人脸识别库需要先进行安装pipinstallface_recognition注意如果安装过程中如果有报错类似!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!You must use Visual Studio to build a python extension on windows. If you are getting this error it means you have not installed Visual C. Note that there are many flavors of Visual Studio, like Visual StudioforC#development. You need toinstallVisual StudioforC.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!可以先安装Visual Studio的C构建工具或者直接下载dlib库然后再pip install face_recognition。
dlib包链接dlib包链接以下是代码importrequestsimportcv2importface_recognitionimportosimporttimeimportthreading# ESP8266的IP地址ESP8266_IP
192.
168.
12BASE_URLfhttp://{ESP8266_IP}TEST_URLf{BASE_URL}/text/plainLED_ON_URLf{BASE_URL}/led/onLED_OFF_URLf{BASE_URL}/led/offclassFaceRecognitionSystem:def__init__(self):初始化人脸识别系统self.known_faces_dirrD:\PythonProject\known_faces# 图片存放地址self.known_face_encodings[]self.known_face_names[]self.last_trigger_time0self.led_cooldown3# LED冷却时间秒self.led_is_onFalse# 加载已知人脸self.load_known_faces()defload_known_faces(self):加载已知人脸图片ifnotos.path.exists(self.known_faces_dir):print(f已知人脸目录不存在:{self.known_faces_dir})returnall_filesos.listdir(self.known_faces_dir)forfilenameinall_files:# 检查文件后缀兼容大小写iffilename.lower().endswith((.jpg,.jpeg,.png)):image_pathos.path.join(self.known_faces_dir,filename)print(f图片路径:{image_path})# 检查文件是否为有效图片try:imageface_recognition.load_image_file(image_path)exceptExceptionase:print(f图片加载失败:{e})continue# 获取人脸编码face_encodingsface_recognition.face_encodings(image)iflen(face_encodings)0:self.known_face_encodings.append(face_encodings[0])self.known_face_names.append(os.path.splitext(filename)[0])print(f已加载人脸:{filename})else:print(f未在图片中检测到人脸:{filename})else:print(f非支持的图片格式:{filename})print(f最终加载人脸数量:{len(self.known_face_encodings)})defrecognize_faces(self,frame):识别人脸并返回是否匹配已知人脸ifnotself.known_face_encodings:returnFalse# 调整图像大小以加快处理速度small_framecv
resize(frame,(0,
,fx
25,fy
0.
# 转换颜色空间 BGR-RGBrgb_small_framecv
cvtColor(small_frame,cv
COLOR_BGR2RGB)# 检测所有人脸位置face_locationsface_recognition.face_locations(rgb_small_frame)ifnotface_locations:returnFalse# 获取所有人脸编码face_encodingsface_recognition.face_encodings(rgb_small_frame,face_locations)# 与已知人脸比较forface_encodinginface_encodings:matchesface_recognition.compare_faces(self.known_face_encodings,face_encoding,tolerance
0.
# 如果匹配到任意已知人脸ifTrueinmatches:print(匹配到已知人脸)returnTrueelse:print(未匹配到已知人脸)returnFalsedefcontrol_led(self,should_turn_on):控制LED开关current_timetime.time()ifshould_turn_onandnotself.led_is_on:# 检查冷却时间ifcurrent_time-self.last_trigger_timeself.led_cooldown:print(检测到已知人脸正在打开LED...)responserequests.get(LED_ON_URL,timeout
ifresponse.status_code200:print(LED已打开)self.led_is_onTrueself.last_trigger_timecurrent_time# 设置3秒后关闭的计时器defturn_off_led():time.sleep(
responserequests.get(LED_OFF_URL,timeout
ifresponse.status_code200:print(✓ LED已关闭)self.led_is_onFalsetimerthreading.Thread(targetturn_off_led)timer.daemonTruetimer.start()else:print(f无法打开LED:{response.status_code})deftest_esp8266_connection():测试ESP8266连接print(测试ESP8266连接...)try:responserequests.get(f{BASE_URL}/,timeout
ifresponse.status_code200:print(fESP8266连接成功 (IP:{ESP8266_IP}))returnTrueexcept:print(f无法连接到ESP8266 (IP:{ESP8266_IP}))returnFalsedefmain():主函数# 测试ESP8266连接ifnottest_esp8266_connection():print(连接测试失败请检查配置)return# 初始化人脸识别系统face_systemFaceRecognitionSystem()ifnotface_system.known_face_encodings:print(未找到已知人脸请将人脸图片放入 known_faces 文件夹)return# 打开摄像头print(正在打开摄像头...)capcv
VideoCapture(
ifnotcap.isOpened():print(无法打开摄像头)returnprint(摄像头已打开退出请按 q 键退出程序)try:whileTrue:# 读取摄像头画面ret,framecap.read()ifnotret:print(无法读取摄像头画面)break# 识别人脸face_detectedface_system.recognize_faces(frame)# 控制LEDface_system.control_led(face_detected)# 在画面上显示状态status_text已知人脸ifface_detectedelse未知人脸color(0,255,
ifface_detectedelse(0,0,
cv
putText(frame,fstatus:{status_text},(10,
,cv
FONT_HERSHEY_SIMPLEX,1,color,
cv
putText(frame,LED: ONifface_system.led_is_onelseLED: OFF,(10,
,cv
FONT_HERSHEY_SIMPLEX,1,(0,255,
ifface_system.led_is_onelse(128,128,
,
cv
putText(frame,Press q to quit,(10,frame.shape[0]-
,cv
FONT_HERSHEY_SIMPLEX,
6,(255,255,
,
# 显示画面cv
imshow(FaceRecognition,frame)# 按q退出ifcv
waitKey(
0xFFord(q):break# 控制处理频率避免过于频繁time.sleep(
0.
exceptExceptionase:print(f\n程序出错:{e})finally:# 释放资源cap.release()cv
destroyAllWindows()# 确保LED关闭try:ifface_system.led_is_on:requests.get(LED_OFF_URL,timeout
print(程序退出LED已关闭)except:passprint(程序已结束)if__name____main__:main()仍有可优化地方比如将图片上传到云从云获取然后上传时候就进行图片人脸编码摄像头识别到时可以直接获取。
以上是所有内容感谢观看。