I am currently capturing an image with the ESP32-Cam at 1ms intervals in RGB565 format in the framebuffer. My goal is to detect when a hand is held in front of the camera. I had the idea that the camera could filter for common skin colors. Unfortunately, this is not working as I had envisioned. Do you have any ideas on this? It should work on the ESP32-Cam itself.
Thanks in advance, every help is appreciated!
Below is my current code:
// Function to analyze the image and output the brightness, individual color components, and grayscale value of each pixel
void ImageRecognition() {
camera_fb_t *fb = esp_camera_fb_get(); //RGB565
// Calculate the average values
uint32_t pixelCount = fb->width * fb->height;
int skinColorCount = 0;
int totalPixels = fb->width * fb->height;
// Analyze the image
for (int i = 0; i < totalPixels * 2; i += 2) {
uint16_t pixel = fb->buf[i] << 8 | fb->buf[i + 1];
// Extract the RGB values from the 16-bit RGB565 value
uint8_t r = (pixel & 0xF800) >> 8; // Red (5 bits) to 8 bits
uint8_t g = (pixel & 0x07E0) >> 3; // Green (6 bits) to 8 bits
uint8_t b = (pixel & 0x001F) << 3; // Blue (5 bits) to 8 bits
// Scale the values to 8-bit
r = r | (r >> 5);
g = g | (g >> 6);
b = b | (b >> 5);
// Check for typical skin tones
if ((r > 95 && r < 255) && (g > 40 && g < 200) && (b > 20 && b < 150)) {
skinColorCount++;
}
}
// Calculate the percentage of skin color pixels
float skinColorPercentage = (float)skinColorCount / totalPixels * 100;
Serial2.print("Percentage of skin color pixels: ");
Serial2.print(skinColorPercentage);
Serial2.println("%");
// Release the framebuffer
esp_camera_fb_return(fb);
}