Reputation: 11
I am working on a project using an ESP32-CAM to detect humans and control appliances like a fan and a light based on Al inference. For this, I am using the Chirale_TensorFlowLite library to run a TensorFlow Lite model
Hardware: ESP32-CAM , DHT22 (temperature and humidity sensor) , PIR sensor , Ultrasonic sensor ,Relays to control
I'm using pretrained model from TensorFlow
i need that Esp32 should run the AI feature on board compute the process
Problem i face like all ops resolver.h no such directly even if exist ,also if work with kaist iot library its showing compatibility issues...how can i get this
#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");
}
}
}
Upvotes: 1
Views: 38