Loading Image From SD Card

tahseenabbas52
Posts: 4
Joined: Thu Oct 24, 2024 6:24 am

Loading Image From SD Card

Postby tahseenabbas52 » Thu Oct 24, 2024 8:19 am

I have tested the SD Card Example(SDMMC) on my ESP32 Cam and it is working fine. My objective is to load the jpg image from the SD Card and count the white pixels in it. I have written a script to load the image but when i count the number of white pixels it print out different number each time on the same image. Can any one help me debug this issue ?
Here is the script i am using in C language.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include "esp_log.h"
#include "esp_err.h"
#include "sdmmc_cmd.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"

#define TAG "football_detection"
#define WHITE_PIXEL_THRESHOLD 5600
#define MOUNT_POINT "/sdcard"

// Function to detect football in the zoomed-in ROI
bool detect_football_in_zoomed_roi(uint8_t* frame, int width, int height, int white_pixel_threshold) {
    int white_pixel_count = 0;

    // Calculate zoomed-in ROI (30% from each side)
    int x_crop = width * 0.30;
    int y_crop = height * 0.30;
    int roi_width = width - 2 * x_crop;
    int roi_height = height - 2 * y_crop;

    // Debugging info: Print image size and calculated ROI
    ESP_LOGI(TAG, "Image width: %d, height: %d", width, height);
    ESP_LOGI(TAG, "Zoomed-in ROI: x=%d, y=%d, width=%d, height=%d", x_crop, y_crop, roi_width, roi_height);

    // Traverse through the ROI and count white pixels
    for (int y = y_crop; y < y_crop + roi_height; ++y) {
        for (int x = x_crop; x < x_crop + roi_width; ++x) {
            int i = (y * width + x) * 3;  // Position in the RGB frame buffer (3 bytes per pixel)

            uint8_t r = frame[i];         // Red component
            uint8_t g = frame[i + 1];     // Green component
            uint8_t b = frame[i + 2];     // Blue component

            // Detect white pixels: Using a high RGB value as white (e.g., R > 200, G > 200, B > 200)
            if (r > 200 && g > 200 && b > 200) {
                white_pixel_count++;
            }
        }
    }

    // Debugging info: Print the total number of white pixels detected
    ESP_LOGI(TAG, "Total white pixels detected: %d", white_pixel_count);

    // Return true if the number of white pixels exceeds the threshold
    return white_pixel_count > white_pixel_threshold;
}

// Function to read the image file and ensure it loads perfectly into memory
static esp_err_t load_image_file(const char *path, uint8_t **image_buffer, size_t *image_size) {
    FILE *file = fopen(path, "rb");
    if (!file) {
        ESP_LOGE(TAG, "Failed to open image file: %s", path);
        return ESP_FAIL;
    }

    // Get the file size
    fseek(file, 0, SEEK_END);
    *image_size = ftell(file);
    fseek(file, 0, SEEK_SET);

    ESP_LOGI(TAG, "Image file size: %zu bytes", *image_size);

    // Allocate memory for the image buffer
    *image_buffer = (uint8_t*)malloc(*image_size);
    if (!(*image_buffer)) {
        ESP_LOGE(TAG, "Failed to allocate memory for image buffer.");
        fclose(file);
        return ESP_ERR_NO_MEM;
    }

    // Read the image file into the buffer
    size_t bytes_read = fread(*image_buffer, 1, *image_size, file);
    fclose(file);

    // Ensure the file was read fully
    if (bytes_read != *image_size) {
        ESP_LOGE(TAG, "Failed to read the complete image file. Bytes read: %zu", bytes_read);
        free(*image_buffer);
        return ESP_FAIL;
    }

    ESP_LOGI(TAG, "Image loaded successfully.");
    return ESP_OK;
}

// Function to load image and detect football
static esp_err_t process_image(const char *image_path) {
    uint8_t *image_buffer = NULL;
    size_t image_size = 0;

    // Load the image from SD card
    esp_err_t err = load_image_file(image_path, &image_buffer, &image_size);
    if (err != ESP_OK) {
        ESP_LOGE(TAG, "Error loading image.");
        return err;
    }

    // Assuming 320x240 RGB image for simplicity (adjust based on your image)
    int width = 320;
    int height = 240;

    // Detect football in the zoomed-in ROI
    bool football_detected = detect_football_in_zoomed_roi(image_buffer, width, height, WHITE_PIXEL_THRESHOLD);

    if (football_detected) {
        ESP_LOGI(TAG, "football detected.");
    } else {
        ESP_LOGI(TAG, "No football detected.");
    }

    // Free the image buffer after use
    free(image_buffer);
    return ESP_OK;
}

void app_main(void) {
    esp_err_t ret;

    // SD card mount configuration (adapted from Code 1)
    esp_vfs_fat_sdmmc_mount_config_t mount_config = {
        #ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
            .format_if_mount_failed = true, // Conditional formatting based on menuconfig
        #else
            .format_if_mount_failed = false, // Do not format if mounting fails
        #endif
            .max_files = 5, // Allow up to 5 files open simultaneously
            .allocation_unit_size = 16 * 1024 // Allocation unit size of 16 KB
    };

    // Configure SDMMC Host and Slot (adapted from Code 1)
    sdmmc_host_t host = SDMMC_HOST_DEFAULT();
    sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
    slot_config.width = 1;
    #ifdef CONFIG_EXAMPLE_SDMMC_BUS_WIDTH_4
        slot_config.width = 4; // Use 4-bit mode if configured
    #else
        slot_config.width = 1; // Default to 1-bit mode
    #endif
    slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP; // Enable internal pull-ups

    // Mount SD card filesystem
    sdmmc_card_t *card;
    ret = esp_vfs_fat_sdmmc_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &card);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to mount SD card filesystem.");
        return;
    }

    ESP_LOGI(TAG, "SD card mounted.");

    // Process the image (change the path if needed)
    const char *image_path = MOUNT_POINT "/noDB1.jpg";
    ret = process_image(image_path);

    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to process image.");
    }

    // Unmount SD card
    esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
    ESP_LOGI(TAG, "SD card unmounted.");
}

MicroController
Posts: 1669
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Loading Image From SD Card

Postby MicroController » Thu Oct 24, 2024 10:43 pm

1) What file/image format does the file on the card contain? If it's not exactly the raw pixel format you're interpreting it as this won't work.
2) The number/range of "pixels" you're looking at is unrelated to the number of bytes actually read from the file. You may be counting random data beyond the end of the loaded data.

tahseenabbas52
Posts: 4
Joined: Thu Oct 24, 2024 6:24 am

Re: Loading Image From SD Card

Postby tahseenabbas52 » Fri Oct 25, 2024 5:28 am

MicroController wrote:
Thu Oct 24, 2024 10:43 pm
1) What file/image format does the file on the card contain? If it's not exactly the raw pixel format you're interpreting it as this won't work.
2) The number/range of "pixels" you're looking at is unrelated to the number of bytes actually read from the file. You may be counting random data beyond the end of the loaded data.
My actual purpose is to load jpg/png/jpeg image from sd card and preprocess it. I have followed different steps, one of them is in the code i provided which you have mentioned, secondly i tried the examples of OpenCV but didn't get any idea how to mount SD Card with those examples though i tried to merge both apps i.e. SDMMC and Hello-World-OpenCV but card was not being mounted.
I have some queries regarding this if you can help on this
1. How to correctly load that image in jpg/png/jpeg format from SD Card?
2. If Point#1 is not possible then what is the appropriate way to load that and process it as an image (AS in the code).
3. Can we use cv2's imread function to load image ? If YES! then how can i mount SD Card on it? I need an example code only.

I would appreciate if you can address these issues, as i have researched a lot but couldn't get any idea.

MicroController
Posts: 1669
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Loading Image From SD Card

Postby MicroController » Sat Oct 26, 2024 6:49 am


tahseenabbas52
Posts: 4
Joined: Thu Oct 24, 2024 6:24 am

Re: Loading Image From SD Card

Postby tahseenabbas52 » Mon Oct 28, 2024 6:59 am

Can you please also answer either we can use cv2's imread function to load image ? If YES! then how can i mount SD Card on it? I need an example code only.
I am not getting how to use this component in my use case ?

MicroController
Posts: 1669
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Loading Image From SD Card

Postby MicroController » Mon Oct 28, 2024 8:28 am

No, I don't know OpenCV.

tahseenabbas52
Posts: 4
Joined: Thu Oct 24, 2024 6:24 am

Re: Loading Image From SD Card

Postby tahseenabbas52 » Tue Oct 29, 2024 5:49 am

MicroController wrote:
Mon Oct 28, 2024 8:28 am
No, I don't know OpenCV.
Any C++ example to mount SDCard ?

Who is online

Users browsing this forum: Bing [Bot] and 54 guests