You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32-CAM集成Chirale_TensorFlowLite实现AI人体检测及设备控制的问题求助

ESP32-CAM集成Chirale_TensorFlowLite实现AI人体检测及设备控制的问题求助

我正在做一个基于ESP32-CAM的项目,想用它检测人体,再根据AI推理结果控制风扇、灯光这类家电。目前我计划用Chirale_TensorFlowLite库来运行TensorFlow Lite模型。

用到的硬件

  • ESP32-CAM
  • DHT22温湿度传感器
  • PIR运动传感器
  • 超声波传感器
  • 继电器(用来控制家电)

模型相关

我用的是TensorFlow的预训练模型,希望ESP32能在本地完成所有AI推理计算,不需要依赖外部设备。

遇到的问题

现在卡在两个地方:

  1. 明明all_ops_resolver.h文件存在,但编译时总是提示找不到这个文件
  2. 试着换成Kaist IoT库,结果出现了兼容性问题,没法正常运行

下面是我当前的代码:

#include "esp_camera.h"

#include "FS.h"

#include "SPIFFS.h"

#include "tensorflow/lite/micro/all_ops_resolver.h"

#include "tensorflow/lite/micro/kernels/all_ops_resolver.h"

#include "tensorflow/lite/schema/schema_generated.h"

#include "tensorflow/lite/version.h"

#include "DHT.h"

// Model-related variables

#define TENSOR_ARENA_SIZE 60 * 1024

uint8_t tensor_arena[TENSOR_ARENA_SIZE];

tflite::MicroInterpreter* interpreter;

TfLiteTensor* input;

TfLiteTensor* output;

// Relay and sensor pins

#define RELAY_LIGHT 15

#define RELAY_FAN 2

#define PIR_SENSOR 13

#define ULTRASONIC_TRIG 12

#define ULTRASONIC_ECHO 14

#define DHT_PIN 4

// DHT Sensor setup

DHT dht(DHT_PIN, DHT22);

// Timing variables

unsigned long previousMillis = 0;

const long interval = 300000; // 5 minutes in milliseconds

// Initialize Camera

void initCamera() {

camera_config_t config;

config.ledc_channel = LEDC_CHANNEL_0;

config.ledc_timer = LEDC_TIMER_0;

config.pin_d0 = Y2_GPIO_NUM;

config.pin_d1 = Y3_GPIO_NUM;

config.pin_d2 = Y4_GPIO_NUM;

config.pin_d3 = Y5_GPIO_NUM;

config.pin_d4 = Y6_GPIO_NUM;

config.pin_d5 = Y7_GPIO_NUM;

config.pin_d6 = Y8_GPIO_NUM;

config.pin_d7 = Y9_GPIO_NUM;

config.pin_xclk = XCLK_GPIO_NUM;

config.pin_pclk = PCLK_GPIO_NUM;

config.pin_vsync = VSYNC_GPIO_NUM;

config.pin_href = HREF_GPIO_NUM;

config.pin_sccb_sda = SIOD_GPIO_NUM;

config.pin_sccb_scl = SIOC_GPIO_NUM;

config.pin_pwdn = PWDN_GPIO_NUM;

config.pin_reset = RESET_GPIO_NUM;

config.pixel_format = PIXFORMAT_JPEG;

config.frame_size = FRAMESIZE_96X96; // Resize for MobileNet

config.jpeg_quality = 12;

config.fb_count = 1;

if (esp_camera_init(&config) != ESP_OK) {

Serial.println("Camera initialization failed");

return;

}

}

// Initialize TensorFlow Lite

void initTFLite() {

if (!SPIFFS.begin(true)) {

Serial.println("Failed to initialize SPIFFS");

return;

}

File modelFile = SPIFFS.open("/mobilenet_v2_quant.tflite", "r");

if (!modelFile) {

Serial.println("Failed to open model file"); 

return; 

}

size_t modelSize = modelFile.size();

uint8_t* modelData = (uint8_t*)malloc(modelSize);

modelFile.read(modelData, modelSize);

modelFile.close();

const tflite::Model* model = tflite::GetModel(modelData);

if (model->version() != TFLITE_SCHEMA_VERSION) {

Serial.println("Model version mismatch"); 

return; 

}

static tflite::AllOpsResolver resolver;

static tflite::MicroInterpreter static_interpreter(

  model, resolver, tensor_arena, TENSOR_ARENA_SIZE); 

interpreter = &static_interpreter;

if (interpreter->AllocateTensors() != kTfLiteOk) {

Serial.println("AllocateTensors() failed"); 

return; 

}

input = interpreter->input(0);

output = interpreter->output(0);

}

// Read distance from ultrasonic sensor

float getDistance() {

digitalWrite(ULTRASONIC_TRIG, LOW);

delayMicroseconds(2);

digitalWrite(ULTRASONIC_TRIG, HIGH);

delayMicroseconds(10);

digitalWrite(ULTRASONIC_TRIG, LOW);

long duration = pulseIn(ULTRASONIC_ECHO, HIGH);

float distance = duration * 0.034 / 2; // Convert to cm

return distance;

}

void setup() {

Serial.begin(115200);

// Initialize pins

pinMode(RELAY_LIGHT, OUTPUT);

pinMode(RELAY_FAN, OUTPUT);

pinMode(PIR_SENSOR, INPUT);

pinMode(ULTRASONIC_TRIG, OUTPUT);

pinMode(ULTRASONIC_ECHO, INPUT);

digitalWrite(RELAY_LIGHT, HIGH); // Default off

digitalWrite(RELAY_FAN, HIGH);  // Default off

// Initialize DHT

dht.begin();

// Initialize camera and TensorFlow Lite

initCamera();

initTFLite();

Serial.println("Setup complete");

}

void loop() {

unsigned long currentMillis = millis();

// Perform periodic checks every 5 minutes

if (currentMillis - previousMillis >= interval) {

previousMillis = currentMillis; 



// Check PIR sensor for motion 

if (digitalRead(PIR_SENSOR) == HIGH) { 

  Serial.println("Motion detected"); 



  // Check ultrasonic sensor to confirm room occupancy 

  float distance = getDistance(); 

  if (distance < 200) { // Adjust threshold as needed 

    Serial.printf("Detected object at %.2f cm\n", distance); 



    // Get temperature and humidity 

    float temperature = dht.readTemperature(); 

    float humidity = dht.readHumidity(); 



    // Capture image and process with TensorFlow Lite 

    camera_fb_t* fb = esp_camera_fb_get(); 

    if (!fb) { 

      Serial.println("Camera capture failed"); 

      return; 

    } 



    // Prepare input tensor 

    memcpy(input->data.uint8, fb->buf, fb->len); 

    esp_camera_fb_return(fb); 



    // Run inference 

    if (interpreter->Invoke() != kTfLiteOk) { 

      Serial.println("Invoke failed"); 

      return; 

    } 



    // Get output (assuming class 1 = "Person") 

    float personProbability = output->data.f[1]; 

    Serial.printf("Person probability: %.2f\n", 

personProbability);

    // Control relay based on detection 

    if (personProbability > 0.5) { 

      Serial.printf("Temp: %.2f, Humidity: %.2f\n", temperature, 

humidity);

      // Control lights and fans based on environmental conditions 

      if (temperature > 25.0) { // Example threshold 

        digitalWrite(RELAY_FAN, LOW); // Turn on fan 

        Serial.println("Fan turned on"); 

      } 

      digitalWrite(RELAY_LIGHT, LOW); // Turn on light 

      Serial.println("Light turned on"); 

    } else { 

      digitalWrite(RELAY_FAN, HIGH); // Turn off fan 

      digitalWrite(RELAY_LIGHT, HIGH); // Turn off light 

      Serial.println("No human detected, appliances off"); 

    } 

  } else { 

    Serial.println("No occupancy detected"); 

  } 

} else { 

  Serial.println("No motion detected"); 

} 

}

}

备注:内容来源于stack exchange,提问作者Kamal Medhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 16:34:33