第 4 章:人臉檢測
本章目標:讓 NanoCam 檢測畫面中的人臉、標註人臉框與關鍵點,並讀取座標用於外部控制。
原理
人臉檢測使用 ESP-DL 深度學習庫,基於 MobileNet 輕量級檢測模型。輸入 320x240 RGB565 圖像,輸出人臉邊界框列表(位置+大小+置信度)。推理在 ESP32-S3 上完成,無需聯網。
檢測結果格式
座標: 左上角(x,y) + 寬高(w,h)
置信度: 0-1 之間的浮點數
多人臉時返回多個框
步驟
4.1 切換模式
Plain
ai_mode:2完整指令見串口協議手冊。
4.2 觀察效果
瀏覽器 http://<IP> 看到人臉檢測框。
4.3 獲取座標
串口輸出格式:
Plain
I (xxxxx) detection_result: [ 0]: ( 45, 30, 180, 210)
I (xxxxx) detection_result: left eye: ( 90, 80), right eye: (150, 80), nose: (120, 120), mouth left: ( 95, 150), mouth right: (145, 150)第一行:
[序號] (x, y, w, h)— 人臉框座標第二行:5 個關鍵點 — 左眼、右眼、鼻子、左嘴角、右嘴角
程式碼
Arduino 讀取座標控制舵機
C++
// 解析 $face:x,y,w,h# 格式
if (nanoSerial.available()) {
String line = nanoSerial.readStringUntil('\n');
if (line.startsWith("$face:")) {
int x = line.substring(6).toInt();
int y = line.substring(line.indexOf(',')+1).toInt();
servoX.write(map(x, 0, 320, 0, 180));
}
}Python 讀取
Python
ser = serial.Serial("COM3", 115200)
line = ser.readline().decode()
if line.startswith("$face:"):
parts = line[6:-1].split(",")
x, y, w, h = map(int, parts)效果
攝像頭前出現人臉→畫面標註綠框→串口輸出座標。
下一章:第 5 章:貓臉檢測

