#include <car_detection_inferencing.h>
#include "edge-impulse-sdk/dsp/image/image.hpp"
#include "esp_camera.h"
// Define the camera model
#define CAMERA_MODEL_XIAO_ESP32S3
#if defined(CAMERA_MODEL_XIAO_ESP32S3)
#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 10
#define SIOD_GPIO_NUM 40
#define SIOC_GPIO_NUM 39
#define Y9_GPIO_NUM 48
#define Y8_GPIO_NUM 11
#define Y7_GPIO_NUM 12
#define Y6_GPIO_NUM 14
#define Y5_GPIO_NUM 16
#define Y4_GPIO_NUM 18
#define Y3_GPIO_NUM 17
#define Y2_GPIO_NUM 15
#define VSYNC_GPIO_NUM 38
#define HREF_GPIO_NUM 47
#define PCLK_GPIO_NUM 13
#else
#error "Camera model not selected"
#endif
/* Constant defines -------------------------------------------------------- */
#define EI_CAMERA_RAW_FRAME_BUFFER_COLS 320
#define EI_CAMERA_RAW_FRAME_BUFFER_ROWS 240
#define EI_CAMERA_FRAME_BYTE_SIZE 3
/* Private variables ------------------------------------------------------- */
static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal
static bool is_initialised = false;
static uint8_t *snapshot_buf = nullptr; // points to the output of the capture
static camera_config_t camera_config = {
.pin_pwdn = PWDN_GPIO_NUM,
.pin_reset = RESET_GPIO_NUM,
.pin_xclk = XCLK_GPIO_NUM,
.pin_sscb_sda = SIOD_GPIO_NUM,
.pin_sscb_scl = SIOC_GPIO_NUM,
.pin_d7 = Y9_GPIO_NUM,
.pin_d6 = Y8_GPIO_NUM,
.pin_d5 = Y7_GPIO_NUM,
.pin_d4 = Y6_GPIO_NUM,
.pin_d3 = Y5_GPIO_NUM,
.pin_d2 = Y4_GPIO_NUM,
.pin_d1 = Y3_GPIO_NUM,
.pin_d0 = Y2_GPIO_NUM,
.pin_vsync = VSYNC_GPIO_NUM,
.pin_href = HREF_GPIO_NUM,
.pin_pclk = PCLK_GPIO_NUM,
.xclk_freq_hz = 20000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_QVGA,
.jpeg_quality = 12,
.fb_count = 1,
.fb_location = CAMERA_FB_IN_PSRAM,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
};
/* Function definitions ------------------------------------------------------- */
bool ei_camera_init(void);
void ei_camera_deinit(void);
bool ei_camera_capture(uint32_t img_width, uint32_t img_height, uint8_t *out_buf);
static int ei_camera_get_data(size_t offset, size_t length, float *out_ptr);
/**
* @brief Arduino setup function
*/
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Edge Impulse Inferencing Demo");
if (ei_camera_init() == false) {
Serial.println("Failed to initialize Camera!");
} else {
Serial.println("Camera initialized.");
}
Serial.println("\nStarting continuous inference in 2 seconds...");
delay(2000);
}
/**
* @brief Get data and run inferencing
*
* @param[in] debug Get debug info if true
*/
void loop() {
Serial.println("Entering loop");
if (ei_sleep(5) != EI_IMPULSE_OK) {
Serial.println("Sleep failed");
return;
}
Serial.println("Allocating buffer");
snapshot_buf = (uint8_t*)malloc(EI_CAMERA_RAW_FRAME_BUFFER_COLS * EI_CAMERA_RAW_FRAME_BUFFER_ROWS * EI_CAMERA_FRAME_BYTE_SIZE);
// check if allocation was successful
if (snapshot_buf == nullptr) {
Serial.println("ERR: Failed to allocate snapshot buffer!");
return;
}
ei::signal_t signal;
signal.total_length = EI_CLASSIFIER_INPUT_WIDTH * EI_CLASSIFIER_INPUT_HEIGHT;
signal.get_data = &ei_camera_get_data;
Serial.println("Capturing image");
if (ei_camera_capture((size_t)EI_CLASSIFIER_INPUT_WIDTH, (size_t)EI_CLASSIFIER_INPUT_HEIGHT, snapshot_buf) == false) {
Serial.println("Failed to capture image");
free(snapshot_buf);
snapshot_buf = nullptr;
return;
}
Serial.println("Running classifier");
// Run the classifier
ei_impulse_result_t result = { 0 };
EI_IMPULSE_ERROR err = run_classifier(&signal, &result, debug_nn);
if (err != EI_IMPULSE_OK) {
Serial.printf("ERR: Failed to run classifier (%d)\n", err);
free(snapshot_buf);
snapshot_buf = nullptr;
return;
}
// print the predictions
Serial.printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n",
result.timing.dsp, result.timing.classification, result.timing.anomaly);
#if EI_CLASSIFIER_OBJECT_DETECTION == 1
Serial.println("Object detection bounding boxes:");
for (uint32_t i = 0; i < result.bounding_boxes_count; i++) {
ei_impulse_result_bounding_box_t bb = result.bounding_boxes[i];
if (bb.value == 0) {
continue;
}
Serial.printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n",
bb.label,
bb.value,
bb.x,
bb.y,
bb.width,
bb.height);
}
#else
Serial.println("Predictions:");
for (uint16_t i = 0; i < EI_CLASSIFIER_LABEL_COUNT; i++) {
Serial.printf(" %s: %.5f\n", ei_classifier_inferencing_categories[i], result.classification[i].value);
}
#endif
#if EI_CLASSIFIER_HAS_ANOMALY
Serial.printf("Anomaly prediction: %.3f\n", result.anomaly);
#endif
#if EI_CLASSIFIER_HAS_VISUAL_ANOMALY
Serial.println("Visual anomalies:");
for (uint32_t i = 0; i < result.visual_ad_count; i++) {
ei_impulse_result_bounding_box_t bb = result.visual_ad_grid_cells[i];
if (bb.value == 0) {
continue;
}
Serial.printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\n",
bb.label,
bb.value,
bb.x,
bb.y,
bb.width,
bb.height);
}
#endif
free(snapshot_buf);
snapshot_buf = nullptr;
Serial.println("Loop done");
delay(2000); // Add a delay to prevent rapid looping
}
/**
* @brief Setup image sensor & start streaming
*
* @retval false if initialization failed
*/
bool ei_camera_init(void) {
if (is_initialised) return true;
//initialize the camera
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x\n", err);
return false;
}
sensor_t * s = esp_camera_sensor_get();
// initial sensors are flipped vertically and colors are a bit saturated
if (s->id.PID == OV3660_PID) {
s->set_vflip(s, 1); // flip it back
s->set_brightness(s, 1); // up the brightness just a bit
s->set_saturation(s, 0); // lower the saturation
}
is_initialised = true;
return true;
}
/**
* @brief Stop streaming of sensor data
*/
void ei_camera_deinit(void) {
//deinitialize the camera
esp_err_t err = esp_camera_deinit();
if (err != ESP_OK) {
Serial.println("Camera deinit failed");
return;
}
is_initialised = false;
}
/**
* @brief Capture, rescale and crop image
*
* @param[in] img_width width of output image
* @param[in] img_height height of output image
* @param[in] out_buf pointer to store output image, NULL may be used
* if ei_camera_frame_buffer is to be used for capture and resize/cropping.
*
* @retval false if not initialised, image captured, rescaled or cropped failed
*
*/
bool ei_camera_capture(uint32_t img_width, uint32_t img_height, uint8_t *out_buf) {
bool do_resize = false;
if (!is_initialised) {
Serial.println("ERR: Camera is not initialized");
return false;
}
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return false;
}
bool converted = fmt2rgb888(fb->buf, fb->len, fb->format, snapshot_buf);
esp_camera_fb_return(fb);
if (!converted) {
Serial.println("to rgb888 conversion failed");
return false;
}
// get size of the data
size_t snapshot_size = EI_CAMERA_RAW_FRAME_BUFFER_COLS * EI_CAMERA_RAW_FRAME_BUFFER_ROWS * EI_CAMERA_FRAME_BYTE_SIZE;
Serial.printf("Snapshot size: %d\n", snapshot_size);
if (img_width == EI_CAMERA_RAW_FRAME_BUFFER_COLS && img_height == EI_CAMERA_RAW_FRAME_BUFFER_ROWS) {
Serial.println("No resize needed");
do_resize = false;
}
else {
do_resize = true;
}
if (do_resize) {
Serial.println("Resizing image");
ei::image::processing::crop_and_interpolate_image(
snapshot_buf,
EI_CAMERA_RAW_FRAME_BUFFER_COLS,
EI_CAMERA_RAW_FRAME_BUFFER_ROWS,
out_buf,
img_width,
img_height,
EI_CAMERA_FRAME_BYTE_SIZE);
}
else {
memcpy(out_buf, snapshot_buf, snapshot_size);
}
return true;
}
/**
* @brief Get image from frame buffer
*
* @param[in] offset offset in the frame buffer to get data
* @param[in] length amount of bytes to get
* @param[out] out_ptr pointer to store output data
*
* @retval 0 if success
*
*/
static int ei_camera_get_data(size_t offset, size_t length, float *out_ptr) {
uint8_t *image = snapshot_buf + offset;
for (size_t i = 0; i < length; i++) {
out_ptr[i] = static_cast<float>(image[i]) / 255.0f;
}
return 0;
}
This a Arduino code generated by edge impulse for car detection model.
I am using esp32s3 camera for this project .
When i try to upload this code into my device after uploading this error is occurring please help me to solve this error.