Update ESP32cam-demo.ino

master
Alan 2025-03-11 11:56:16 +00:00 zatwierdzone przez GitHub
rodzic 7b4242e43e
commit 7462501a9f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 47 dodań i 35 usunięć

Wyświetl plik

@ -1,3 +1,5 @@
// new esp32 manager - esp32cam - regular
/******************************************************************************************************************* /*******************************************************************************************************************
* *
* ESP32Cam development board demo sketch using Arduino IDE or PlatformIO * ESP32Cam development board demo sketch using Arduino IDE or PlatformIO
@ -101,8 +103,8 @@
// -SETTINGS // -SETTINGS
// --------------------------------------------------------------- // ---------------------------------------------------------------
char* stitle = "ESP32Cam"; // title of this sketch char* stitle = "BackCam"; // title of this sketch
char* sversion = "08Mar25"; // Sketch version char* sversion = "11Mar25"; // Sketch version
framesize_t FRAME_SIZE_IMAGE = FRAMESIZE_SVGA; // default camera resolution framesize_t FRAME_SIZE_IMAGE = FRAMESIZE_SVGA; // default camera resolution
// Resolutions available: // Resolutions available:
@ -554,12 +556,13 @@ bool cameraImageSettings() {
s->set_exposure_ctrl(s, 1); // auto exposure on s->set_exposure_ctrl(s, 1); // auto exposure on
s->set_saturation(s, -1); // Slightly decrease saturation s->set_saturation(s, -1); // Slightly decrease saturation
s->set_contrast(s, -1); // Slightly decrease contrast s->set_contrast(s, -1); // Slightly decrease contrast
s->set_whitebal(s, 1); // Enable auto white balanc s->set_whitebal(s, 1); // Enable white balance
s->set_awb_gain(s, 1); // Auto White Balance enable (0 or 1) s->set_awb_gain(s, 1); // Auto White Balance enable (0 or 1)
s->set_brightness(s, cameraImageBrightness); // (-2 to 2) - set brightness s->set_brightness(s, cameraImageBrightness); // (-2 to 2) - set brightness
} else { } else {
// Apply manual settings // Apply manual settings
s->set_gain_ctrl(s, 0); // auto gain off s->set_gain_ctrl(s, 0); // auto gain off
s->set_whitebal(s, 1); // Enable white balance
s->set_awb_gain(s, 1); // Auto White Balance enable (0 or 1) s->set_awb_gain(s, 1); // Auto White Balance enable (0 or 1)
s->set_saturation(s, -1); // Slightly decrease saturation s->set_saturation(s, -1); // Slightly decrease saturation
s->set_contrast(s, -1); // Slightly decrease contrast s->set_contrast(s, -1); // Slightly decrease contrast
@ -573,6 +576,12 @@ bool cameraImageSettings() {
//s->set_vflip(s, 1); // flip image vertically //s->set_vflip(s, 1); // flip image vertically
//s->set_hmirror(s, 1); // flip image horizontally //s->set_hmirror(s, 1); // flip image horizontally
// Discard initial frames to ensure new settings apply
for (int i = 0; i < 5; i++) {
camera_fb_t *fb = esp_camera_fb_get();
esp_camera_fb_return(fb);
}
return 1; return 1;
} // cameraImageSettings } // cameraImageSettings
@ -753,10 +762,7 @@ byte storeImage() {
} }
camera_fb_t * fb = NULL; camera_fb_t * fb = NULL;
fb = esp_camera_fb_get(); fb = esp_camera_fb_get();
// there is a bug where this buffer can be from previous capture so as workaround it is discarded and captured again
esp_camera_fb_return(fb); // dispose the buffered image
fb = NULL; // reset to capture errors
fb = esp_camera_fb_get(); // get fresh image
if (flashRequired){ if (flashRequired){
delay(100); delay(100);
analogWrite(brightLED, currentBrightness); // change LED brightness back to previous state analogWrite(brightLED, currentBrightness); // change LED brightness back to previous state
@ -851,7 +857,6 @@ void rootUserInput(WiFiClient &client) {
} }
} }
// if button1 was pressed (toggle io pin B) // if button1 was pressed (toggle io pin B)
if (server.hasArg("button1")) { if (server.hasArg("button1")) {
if (serialDebug) Serial.println("Button 1 pressed"); if (serialDebug) Serial.println("Button 1 pressed");
@ -1319,10 +1324,7 @@ void readRGBImage() {
tTimer = millis(); // store time that image capture started tTimer = millis(); // store time that image capture started
camera_fb_t * fb = NULL; camera_fb_t * fb = NULL;
fb = esp_camera_fb_get(); fb = esp_camera_fb_get();
// there is a bug where this buffer can be from previous capture so as workaround it is discarded and captured again
esp_camera_fb_return(fb); // dispose the buffered image
fb = NULL; // reset to capture errors
fb = esp_camera_fb_get(); // get fresh image
if (!fb) { if (!fb) {
sendText(client,"error: failed to capture image from camera"); sendText(client,"error: failed to capture image from camera");
client.write("<br><a href='/'>Return</a>\n"); // link back client.write("<br><a href='/'>Return</a>\n"); // link back
@ -1470,11 +1472,12 @@ bool handleJPG() {
camera_fb_t * fb = NULL; camera_fb_t * fb = NULL;
for (int attempts = 0; attempts < 3; attempts++) { for (int attempts = 0; attempts < 3; attempts++) {
delay(30); // help stabilize the frame?
fb = esp_camera_fb_get(); fb = esp_camera_fb_get();
if (fb && fb->buf && fb->len > 0) break; if (fb && fb->buf && fb->len > 0) break; // frame has been captured
esp_camera_fb_return(fb); if (fb) esp_camera_fb_return(fb);
fb = NULL; fb = NULL;
delay(100); // Delay before retry delay(100); // delay before retry
} }
if (!fb) { if (!fb) {
@ -1499,7 +1502,7 @@ bool handleJPG() {
} }
delay(3); delay(3);
client.flush(); client.clear();
client.stop(); client.stop();
esp_camera_fb_return(fb); esp_camera_fb_return(fb);
@ -1698,35 +1701,44 @@ void readGrayscaleImage() {
} }
camera_fb_t * fb = NULL; camera_fb_t * fb = NULL;
fb = esp_camera_fb_get(); fb = esp_camera_fb_get();
// there is a bug where this buffer can be from previous capture so as workaround it is discarded and captured again
esp_camera_fb_return(fb); // dispose the buffered image
fb = NULL; // reset to capture errors
fb = esp_camera_fb_get(); // get fresh image
if (flashRequired){ if (flashRequired){
delay(100); delay(100);
analogWrite(brightLED, currentBrightness); // change LED brightness back to previous state analogWrite(brightLED, currentBrightness); // change LED brightness back to previous state
} }
if (!fb) client.println("Error: Camera image capture failed"); if (!fb) client.println("Error: Camera image capture failed");
// read image data and calculate average pixel value (as demonstration of reading the image data) // // read image data and calculate average pixel value (as demonstration of reading the image data)
// note: image x = i % WIDTH, image y = floor(i / WIDTH) // // note: image x = i % WIDTH, image y = floor(i / WIDTH)
unsigned long dataSize = fb->width * fb->height; // unsigned long dataSize = fb->width * fb->height;
byte minV=255; byte maxV=0; // byte minV=255; byte maxV=0;
for (int y=0; y < fb->height; y++) { // for (int y=0; y < fb->height; y++) {
for (int x=0; x < fb->width; x++) { // for (int x=0; x < fb->width; x++) {
byte pixelVal = fb->buf[(y * fb->width) + x]; // byte pixelVal = fb->buf[(y * fb->width) + x];
if (pixelVal > maxV) maxV = pixelVal; // if (pixelVal > maxV) maxV = pixelVal;
if (pixelVal < minV) minV = pixelVal; // if (pixelVal < minV) minV = pixelVal;
} // }
} // }
client.println("grayscale Image: The lowest value pixel is " + String(minV) + ", the highest is " + String(maxV)); // client.print("Grayscale image: The lowest value pixel is " + String(minV) + ", the highest is " + String(maxV));
client.write("<br><br><a href='/'>Return</a>\n"); // link back // client.print("<br>");
// resize the image // resize the image
int newWidth = 115; int newHeight = 42; // much bigger than this seems to cause problems, possible web page is too large? int newWidth = 115; int newHeight = 42; // much bigger than this seems to cause problems, possible web page is too large?
byte newBuf[newWidth * newHeight]; byte newBuf[newWidth * newHeight];
resize_esp32cam_image_buffer(fb->buf, fb->width, fb->height, newBuf, newWidth, newHeight); resize_esp32cam_image_buffer(fb->buf, fb->width, fb->height, newBuf, newWidth, newHeight);
// Get the min and max values in the new resized image
byte newminV=255; byte newmaxV=0;
for (int y=0; y < newHeight; y++) {
for (int x=0; x < newWidth; x++) {
byte pixelVal = newBuf[(y * newWidth) + x];
if (pixelVal > newmaxV) newmaxV = pixelVal;
if (pixelVal < newminV) newminV = pixelVal;
}
}
client.println("Resized image: The lowest value pixel is " + String(newminV) + ", the highest is " + String(newmaxV));
client.write("<br><br><a href='/'>Return</a>\n"); // link back
// display image as asciiArt // display image as asciiArt
char asciiArt[] = {'@','#','S','%','?','*','+',';',':',',','.',' ',' '}; // characters to use char asciiArt[] = {'@','#','S','%','?','*','+',';',':',',','.',' ',' '}; // characters to use
int noAsciiChars = sizeof(asciiArt) / sizeof(asciiArt[0]); // number of characters available int noAsciiChars = sizeof(asciiArt) / sizeof(asciiArt[0]); // number of characters available
@ -1734,7 +1746,7 @@ void readGrayscaleImage() {
for (int y=0; y < newHeight; y++) { for (int y=0; y < newHeight; y++) {
client.write("\n"); // new line client.write("\n"); // new line
for (int x=0; x < newWidth; x++) { for (int x=0; x < newWidth; x++) {
int tpos = map(newBuf[y*newWidth+x], minV, maxV, 0, noAsciiChars - 1); // convert pixel brightness to ascii character int tpos = map(newBuf[y*newWidth+x], newminV, newmaxV, 0, noAsciiChars - 1); // convert pixel brightness to ascii character
client.write(asciiArt[tpos]); client.write(asciiArt[tpos]);
} }
} }