I am working on a project involving esp32 cam for object detection but this always pops up on my serial monitor "Failed to capture image E (32059) esp_jpg_decode"
#include <ESP32Servo.h>
#include <Final_research_inferencing.h>
#include "esp_camera.h"
// Define servo motor pins
#define SERVO1_PIN 13
#define SERVO2_PIN 12
Servo servo1;
Servo servo2;
// Define ESP32-CAM Camera Pins
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
// Function to initialize the camera
void setupCamera() {
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_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_GRAYSCALE;
config.frame_size = FRAMESIZE_96X96;
config.fb_count = 1;
if (esp_camera_init(&config) != ESP_OK) {
Serial.println("Camera Init Failed!");
return;
}
Serial.println("Camera Initialized Successfully!");
}
void setup() {
Serial.begin(115200);
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
servo1.write(90);
servo2.write(90);
setupCamera();
}
void loop() {
Serial.println("Capturing image...");
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
ei::signal_t signal;
int raw_image_size = fb->len;
int8_t* raw_image_buffer = (int8_t*) fb->buf;
float* float_image_buffer = new float[raw_image_size];
for (size_t i = 0; i < raw_image_size; i++) {
float_image_buffer[i] = raw_image_buffer[i] / 255.0f;
}
int err = numpy::signal_from_buffer(float_image_buffer, raw_image_size, &signal);
delete[] float_image_buffer; // Free allocated memory
if (err != 0) {
Serial.println("Failed to convert image to signal.");
esp_camera_fb_return(fb);
return;
}
ei_impulse_result_t result;
Serial.println("Running Edge Impulse Classification...");
if (run_classifier(&signal, &result, false) != EI_IMPULSE_OK) {
Serial.println("Classifier failed!");
esp_camera_fb_return(fb);
return;
}
bool objectDetected = false;
for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
Serial.print(result.classification[ix].label);
Serial.print(": ");
Serial.println(result.classification[ix].value, 5);
if (result.classification[ix].value > 0.7) {
objectDetected = true;
Serial.print("Detected Object: ");
Serial.println(result.classification[ix].label);
if (strstr(result.classification[ix].label, "plastic") != NULL) {
servo1.write(0);
delay(500);
servo1.write(90);
servo2.write(0);
delay(500);
servo2.write(90);
} else if (strcmp(result.classification[ix].label, "bottle") == 0) {
servo1.write(0);
delay(500);
servo1.write(90);
} else if (strstr(result.classification[ix].label, "paper") != NULL) {
servo2.write(0);
delay(500);
servo2.write(90);
}
}
}
if (!objectDetected) {
Serial.println("No object detected, resetting servos...");
servo1.write(90);
servo2.write(90);
}
esp_camera_fb_return(fb);
delay(1000);
}