Hello,
I want to classify a photo taken by a ESP32-CAM, but I want to add a physical pushbutton in a input to “Run Inference”
To get there I use the Github shared by luisomoreau: https://github.com/edgeimpulse/example-esp32-cam
I am able to classify a photo, but to do that I have to push “Run Inference” button in the web (I use my mobile for that). After pushing the button I can see the result in the web.
But I want to run the inference without using the web, I want to use a pushbutton to run the inference and some leds to see the result. Right now, I am able to turn on leds depending on the result of the inference. So when it classifies a cat turns on one led and when it clasifies a dog turns on another led.
But my issue is that I am not able to Run Inference when I push a physical pushbutton connected in an input pin of the ESP32-CAM.
To turn on the leds I modified two files:
A - The first one is “Advanced-image-Classification.ino” just to add the two outputs:
void setup() {
...
pinMode(12, OUTPUT); // sets the digital pin 12 as output
pinMode(13, OUTPUT); // sets the digital pin 13 as output
...
}
B - The second file is “app_httpd.cpp” file I modified one of the functions there (I comment inside the code the modified part)
static esp_err_t inference_results_handler(httpd_req_t *req)
{
static char json_response[1024] = "";
char *p = json_response;
*p++ = '{';
if (result.classification[0].label)
{
p += sprintf(p, "\"success\": true,");
p += sprintf(p, "\"classification\": [");
for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++)
{
*p++ = '{';
p += sprintf(p, "\"label\": \"%s\",", result.classification[ix].label);
p += sprintf(p, "\"value\":%.5f", result.classification[ix].value);
*p++ = '}';
if (ix != EI_CLASSIFIER_LABEL_COUNT - 1)
{
*p++ = ',';
}
//p+=sprintf(p, "\"%s\":%.5f,", result.classification[ix].label, result.classification[ix].value);
}
*p++ = ']';
*p++ = ',';
}
else
{
p += sprintf(p, "\"success\": false,");
}
// human-readable predictions
p += sprintf(p, "\"timing\": {");
p += sprintf(p, "\"timing_dsp\":%d,", result.timing.dsp);
p += sprintf(p, "\"timing_classification\":%d,", result.timing.classification);
p += sprintf(p, "\"timing_anomaly\":%d", result.timing.anomaly);
*p++ = '}';
*p++ = '}';
*p++ = 0;
Serial.println(json_response);
//*****************************************************************************************************
//I MODIFIED THIS PART - BEGINNING
//*****************************************************************************************************
float value_tmp = 0.0; // to save the porcentage
String label_tmp; // to save the name
if (result.classification[0].label)
{
for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++)
{
if (result.classification[ix].value > value_tmp) // to choose the class with the biggest value
{
value_tmp = result.classification[ix].value;
label_tmp = result.classification[ix].label;
}
}
}
if (value_tmp >= 0.85) // If the classification porcentage is less than 0.85 we do nothing
{
if (label_tmp == "Dog") // If it classifises a Dog activates a led
{
digitalWrite(12, HIGH); // sets the digital pin 12 on
delay(1000); // waits for a second
digitalWrite(12, LOW); // sets the digital pin 12 off
}
if (label_tmp == "Cat") // If it classifises a Cat activates a led
{
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
}
}
//*****************************************************************************************************
//I MODIFIED THIS PART - END
//*****************************************************************************************************
httpd_resp_set_type(req, "application/json");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
ei_impulse_result_t result = {0};
return httpd_resp_send(req, json_response, strlen(json_response));
}
I HAVE TWO QUESTIONS:
1 - I don’t know where to add and input to Run the Inference to classify a image when I push a pushbutton. I want to know which part of the code I have to change. That way I would be able to Run Inference and see the result without the web, that way the ESP32-CAM would be completely independant.
2 - This is beginner level: the function: esp_err_t inference_results_handler()
it is not call anywhere in the code. When it is called? By who?
If I didn’t make myself clear enough, feel free to ask.
Thank you for your time.