基于 Python 的人脸识别控制 ESP8266 灯光
环境准备
本项目使用 Python 脚本配合 face_recognition 库,通过 HTTP 协议控制 ESP8266 硬件。
安装依赖:
pip install face_recognition
注意如果安装过程中报错提示需要 Visual Studio C++ 构建工具,请先安装相关组件或下载 dlib 包。
核心代码
import requests
import cv2
import face_recognition
import os
import time
import threading
# ESP8266 的 IP 地址
ESP8266_IP = "192.168.1.12"
BASE_URL = f"http://{ESP8266_IP}"
TEST_URL = f"{BASE_URL}/text/plain"
LED_ON_URL = f"{BASE_URL}/led/on"
LED_OFF_URL = f"{BASE_URL}/led/off"
class FaceRecognitionSystem:
def __init__(self):
"""初始化人脸识别系统"""
self.known_faces_dir = r"D:\PythonProject\known_faces" # 图片存放地址
self.known_face_encodings = []
self.known_face_names = []
self.last_trigger_time = 0
self.led_cooldown = 3 # LED 冷却时间(秒)
self.led_is_on = False
# 加载已知人脸
self.load_known_faces()
def load_known_faces():
os.path.exists(.known_faces_dir):
()
all_files = os.listdir(.known_faces_dir)
filename all_files:
filename.lower().endswith((, , )):
image_path = os.path.join(.known_faces_dir, filename)
()
:
image = face_recognition.load_image_file(image_path)
Exception e:
()
face_encodings = face_recognition.face_encodings(image)
(face_encodings) > :
.known_face_encodings.append(face_encodings[])
.known_face_names.append(os.path.splitext(filename)[])
()
:
()
:
()
()
():
.known_face_encodings:
small_frame = cv2.resize(frame, (, ), fx=, fy=)
rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_small_frame)
face_locations:
face_encodings = face_recognition.face_encodings(
rgb_small_frame, face_locations
)
face_encoding face_encodings:
matches = face_recognition.compare_faces(
.known_face_encodings, face_encoding, tolerance=
)
matches:
()
:
()
():
current_time = time.time()
should_turn_on .led_is_on:
current_time - .last_trigger_time >= .led_cooldown:
()
response = requests.get(LED_ON_URL, timeout=)
response.status_code == :
()
.led_is_on =
.last_trigger_time = current_time
():
time.sleep()
response = requests.get(LED_OFF_URL, timeout=)
response.status_code == :
()
.led_is_on =
timer = threading.Thread(target=turn_off_led)
timer.daemon =
timer.start()
:
()
():
()
:
response = requests.get(, timeout=)
response.status_code == :
()
:
()
():
FaceRecognitionSystem().test_esp8266_connection():
()
face_system = FaceRecognitionSystem()
face_system.known_face_encodings:
()
()
cap = cv2.VideoCapture()
cap.isOpened():
()
()
:
:
ret, frame = cap.read()
ret:
()
face_detected = face_system.recognize_faces(frame)
face_system.control_led(face_detected)
status_text = face_detected
color = (, , ) face_detected (, , )
cv2.putText(frame, , (, ),
cv2.FONT_HERSHEY_SIMPLEX, , color, )
cv2.putText(frame, face_system.led_is_on ,
(, ), cv2.FONT_HERSHEY_SIMPLEX, ,
(, , ) face_system.led_is_on (, , ), )
cv2.putText(frame, , (, frame.shape[]-),
cv2.FONT_HERSHEY_SIMPLEX, , (, , ), )
cv2.imshow(, frame)
cv2.waitKey() & == ():
time.sleep()
Exception e:
()
:
cap.release()
cv2.destroyAllWindows()
:
face_system.led_is_on:
requests.get(LED_OFF_URL, timeout=)
()
:
()
__name__ == :
main()

