Update Rolling

DELETE ALL
Restructure CTfLiteAll
This commit is contained in:
jomjol
2020-09-10 22:03:28 +02:00
parent 186a500a1e
commit 97adcec80e
16 changed files with 976 additions and 546 deletions

View File

@@ -140,13 +140,21 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath)
///////////////////////////////
std::string _zw = std::string(dirpath);
_zw = _zw.substr(8, _zw.length() - 8);
_zw = "/delete/" + _zw + "?task=deldircontent";
/* Send file-list table definition and column labels */
httpd_resp_sendstr_chunk(req,
"<table class=\"fixed\" border=\"1\">"
"<col width=\"800px\" /><col width=\"300px\" /><col width=\"300px\" /><col width=\"100px\" />"
"<thead><tr><th>Name</th><th>Type</th><th>Size (Bytes)</th><th>Delete<br><button id=\"deleteall\" type=\"button\" onclick=\"deleteall()\">DELETE ALL!</button></th></tr></thead>"
"<tbody>");
"<thead><tr><th>Name</th><th>Type</th><th>Size (Bytes)</th><th>Delete<br>"
"<form method=\"post\" action=\"");
httpd_resp_sendstr_chunk(req, _zw.c_str());
httpd_resp_sendstr_chunk(req,
"\"><button type=\"submit\">DELETE ALL!</button></form>"
"</th></tr></thead><tbody>\n");
/* Iterate over all files / folders and fetch their names and sizes */
while ((entry = readdir(dir)) != NULL) {
@@ -460,12 +468,19 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
}
zw = std::string(filename);
zw = zw.substr(0, zw.length()-1);
directory = "/fileserver" + zw + "/";
zw = "/sdcard" + zw;
printf("Directory to delete: %s\n", zw.c_str());
delete_all_in_directory(zw);
directory = std::string(filepath);
// directory = std::string(filepath);
// directory = "/fileserver" + directory;
printf("Location after delete directory content: %s\n", directory.c_str());
/* Redirect onto root to see the updated file list */
// httpd_resp_set_status(req, "303 See Other");
// httpd_resp_set_hdr(req, "Location", directory.c_str());
// httpd_resp_sendstr(req, "File deleted successfully");
// return ESP_OK;
}
else
{
@@ -544,10 +559,12 @@ void delete_all_in_directory(std::string _directory)
/* Iterate over all files / folders and fetch their names and sizes */
while ((entry = readdir(dir)) != NULL) {
if (!(entry->d_type == DT_DIR)){
filename = _directory + "/" + std::string(entry->d_name);
ESP_LOGI(TAG, "Deleting file : %s", filename.c_str());
/* Delete file */
unlink(filename.c_str());
if (strcmp("wlan.ini", entry->d_name) != 0){ // auf wlan.ini soll nicht zugegriffen werden !!!
filename = _directory + "/" + std::string(entry->d_name);
ESP_LOGI(TAG, "Deleting file : %s", filename.c_str());
/* Delete file */
unlink(filename.c_str());
}
};
}
closedir(dir);

View File

@@ -1,87 +0,0 @@
#pragma once
#ifndef __CFINDTEMPLATE
#define __CFINGTEMPLATE
#include <stdint.h>
#include <string>
#define _USE_MATH_DEFINES
#include <math.h>
#include <esp_heap_caps.h>
#include "stb_image.h"
#include "stb_image_write.h"
#include "stb_image_resize.h"
class CImageBasis
{
protected:
uint8_t* rgb_image;
int channels;
int width, height, bpp;
bool externalImage;
std::string filename;
void memCopy(uint8_t* _source, uint8_t* _target, int _size);
public:
int getWidth(){return this->width;};
int getHeight(){return this->width;};
int getChannels(){return this->channels;};
CImageBasis();
CImageBasis(std::string _image);
CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp);
uint8_t GetPixelColor(int x, int y, int ch);
~CImageBasis();
void SaveToFile(std::string _imageout);
};
class CFindTemplate : public CImageBasis
{
public:
CFindTemplate(std::string _image);
void FindTemplate(std::string _template, int* found_x, int* found_y, std::string _imageout);
void FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy, std::string _imageout);
void FindTemplate(std::string _template, int* found_x, int* found_y);
void FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy);
};
class CRotate: public CImageBasis
{
public:
CRotate(std::string _image) : CImageBasis(_image) {};
CRotate(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp) : CImageBasis(_rgb_image, _channels, _width, _height, _bpp) {};
void Rotate(float _angle);
void Rotate(float _angle, int _centerx, int _centery);
void Translate(int _dx, int _dy);
};
class CAlignAndCutImage : public CImageBasis
{
public:
CAlignAndCutImage(std::string _image) : CImageBasis(_image) {};
void Align(std::string _template0, int ref0_x, int ref0_y, std::string _template1, int ref1_x, int ref1_y, int deltax, int deltay);
void CutAndSave(std::string _template1, int x1, int y1, int dx, int dy);
};
class CResizeImage : public CImageBasis
{
public:
CResizeImage(std::string _image) : CImageBasis(_image) {};
void Resize(int _new_dx, int _new_dy);
};
#endif

View File

@@ -1,366 +0,0 @@
#include "CFindTemplate.h"
#include "Helper.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <algorithm>
#define _ESP32_PSRAM
using namespace std;
#define GET_MEMORY malloc
uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
{
stbi_uc* p_source;
p_source = this->rgb_image + (this->channels * (y * this->width + x));
return p_source[channels];
}
void CResizeImage::Resize(int _new_dx, int _new_dy)
{
int memsize = _new_dx * _new_dy * this->channels;
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
stbir_resize_uint8(this->rgb_image, this->width, this->height, 0, odata, _new_dx, _new_dy, 0, this->channels);
stbi_image_free(this->rgb_image);
this->rgb_image = (unsigned char*)GET_MEMORY(memsize);
this->memCopy(odata, this->rgb_image, memsize);
this->width = _new_dx;
this->height = _new_dy;
stbi_image_free(odata);
}
void CRotate::Rotate(float _angle, int _centerx, int _centery)
{
float m[2][3];
float x_center = _centerx;
float y_center = _centery;
_angle = _angle / 180 * M_PI;
m[0][0] = cos(_angle);
m[0][1] = sin(_angle);
m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
m[1][0] = -m[0][1];
m[1][1] = m[0][0];
m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
int memsize = this->width * this->height * this->channels;
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
int x_source, y_source;
stbi_uc* p_target;
stbi_uc* p_source;
for (int x = 0; x < this->width; ++x)
for (int y = 0; y < this->height; ++y)
{
p_target = odata + (this->channels * (y * this->width + x));
x_source = int(m[0][0] * x + m[0][1] * y);
y_source = int(m[1][0] * x + m[1][1] * y);
x_source += int(m[0][2]);
y_source += int(m[1][2]);
if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
{
p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
for (int channels = 0; channels < this->channels; ++channels)
p_target[channels] = p_source[channels];
}
else
{
for (int channels = 0; channels < this->channels; ++channels)
p_target[channels] = 255;
}
}
// memcpy(this->rgb_image, odata, memsize);
this->memCopy(odata, this->rgb_image, memsize);
stbi_image_free(odata);
}
void CRotate::Rotate(float _angle)
{
this->Rotate(_angle, this->width / 2, this->height / 2);
}
void CRotate::Translate(int _dx, int _dy)
{
int memsize = this->width * this->height * this->channels;
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
int x_source, y_source;
stbi_uc* p_target;
stbi_uc* p_source;
for (int x = 0; x < this->width; ++x)
for (int y = 0; y < this->height; ++y)
{
p_target = odata + (this->channels * (y * this->width + x));
x_source = x - _dx;
y_source = y - _dy;
if ((x_source >= 0) && (x_source < this->width) && (y_source >= 0) && (y_source < this->height))
{
p_source = this->rgb_image + (this->channels * (y_source * this->width + x_source));
for (int channels = 0; channels < this->channels; ++channels)
p_target[channels] = p_source[channels];
}
else
{
for (int channels = 0; channels < this->channels; ++channels)
p_target[channels] = 255;
}
}
// memcpy(this->rgb_image, odata, memsize);
this->memCopy(odata, this->rgb_image, memsize);
stbi_image_free(odata);
}
CFindTemplate::CFindTemplate(std::string _image)
{
this->channels = 1;
this->rgb_image = stbi_load(_image.c_str(), &(this->width), &(this->height), &(this->bpp), this->channels);
}
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y)
{
this->FindTemplate(_template, found_x, found_y, 0, 0);
}
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy)
{
int tpl_width, tpl_height, tpl_bpp;
uint8_t* rgb_template = stbi_load(_template.c_str(), &tpl_width, &tpl_height, &tpl_bpp, this->channels);
int ow, ow_start, ow_stop;
int oh, oh_start, oh_stop;
if (_dx == 0)
{
_dx = this->width;
*found_x = 0;
}
if (_dy == 0)
{
_dy = this->height;
*found_y = 0;
}
ow_start = *found_x - _dx;
ow_start = std::max(ow_start, 0);
ow_stop = *found_x + _dx;
if ((ow_stop + tpl_width) > this->width)
ow_stop = this->width - tpl_width;
ow = ow_stop - ow_start + 1;
oh_start = *found_y - _dy;
oh_start = std::max(oh_start, 0);
oh_stop = *found_y + _dy;
if ((oh_stop + tpl_height) > this->height)
oh_stop = this->height - tpl_height;
oh = oh_stop - oh_start + 1;
uint8_t* odata = (unsigned char*)GET_MEMORY(ow * oh * this->channels);
double aktSAD;
double minSAD = pow(tpl_width * tpl_height * 255, 2);
for (int xouter = ow_start; xouter <= ow_stop; xouter++)
for (int youter = oh_start; youter <= oh_stop; ++youter)
{
aktSAD = 0;
for (int tpl_x = 0; tpl_x < tpl_width; tpl_x++)
for (int tpl_y = 0; tpl_y < tpl_height; tpl_y++)
{
stbi_uc* p_org = this->rgb_image + (this->channels * ((youter + tpl_y) * this->width + (xouter + tpl_x)));
stbi_uc* p_tpl = rgb_template + (this->channels * (tpl_y * tpl_width + tpl_x));
aktSAD += pow(p_tpl[0] - p_org[0], 2);
}
stbi_uc* p_out = odata + (this->channels * ((youter - oh_start) * ow + (xouter - ow_start)));
p_out[0] = int(sqrt(aktSAD / (tpl_width * tpl_height)));
if (aktSAD < minSAD)
{
minSAD = aktSAD;
*found_x = xouter;
*found_y = youter;
}
}
stbi_image_free(odata);
stbi_image_free(rgb_template);
}
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, std::string _imageout)
{
this->FindTemplate(_template, found_x, found_y);
this->SaveToFile(_imageout);
}
void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy, std::string _imageout)
{
this->FindTemplate(_template, found_x, found_y, _dx, _dy);
this->SaveToFile(_imageout);
}
void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
{
#ifdef _ESP32_PSRAM
for (int i = 0; i < _size; ++i)
*(_target + i) = *(_source + i);
#else
memcpy(_target, _source, _size);
#endif
}
CImageBasis::CImageBasis()
{
this->externalImage = false;
}
CImageBasis::CImageBasis(std::string _image)
{
// printf("Start CImageBasis\n");
channels = 3;
externalImage = false;
filename = _image;
// printf("CImageBasis before load\n");
// printf(_image.c_str()); printf("\n");
rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
if (!rgb_image)
{
printf("Datei konnte nicht geoeffnet werden\n");
return;
}
// printf("CImageBasis after load\n");
// printf("w %d, h %d, b %d, c %d\n", width, height, bpp, channels);
}
CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
{
rgb_image = _rgb_image;
channels = _channels;
width = _width;
height = _height;
bpp = _bpp;
externalImage = true;
}
CImageBasis::~CImageBasis()
{
if (!externalImage)
stbi_image_free(rgb_image);
}
void CImageBasis::SaveToFile(std::string _imageout)
{
string typ = getFileType(_imageout);
if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
{
stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
}
if ((typ == "bmp") || (typ == "BMP"))
{
stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
}
// stbi_write_jpg(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image, 0);
// stbi_write_bmp(_imageout.c_str(), this->width, this->height, this->channels, this->rgb_image);
}
void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, std::string _template1, int ref1_x, int ref1_y, int deltax = 40, int deltay = 40)
{
int dx, dy;
int r0_x, r0_y, r1_x, r1_y;
CFindTemplate* ft = new CFindTemplate(this->filename);
r0_x = ref0_x;
r0_y = ref0_y;
ft->FindTemplate(_template0, &r0_x, &r0_y, deltax, deltay);
r1_x = ref1_x;
r1_y = ref1_y;
ft->FindTemplate(_template1, &r1_x, &r1_y, deltax, deltay);
delete ft;
dx = ref0_x - r0_x;
dy = ref0_y - r0_y;
r0_x += dx;
r0_y += dy;
r1_x += dx;
r1_y += dy;
float w_org, w_ist, d_winkel;
w_org = atan2(ref1_y - ref0_y, ref1_x - ref0_x);
w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
d_winkel = -(w_org - w_ist) * 180 / M_PI;
CRotate rt(this->rgb_image, this->channels, this->width, this->height, this->bpp);
rt.Translate(dx, dy);
rt.Rotate(d_winkel, ref0_x, ref0_y);
printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
}
void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
{
int x2, y2;
x2 = x1 + dx;
y2 = y1 + dy;
x2 = min(x2, this->width - 1);
y2 = min(y2, this->height - 1);
dx = x2 - x1;
dy = y2 - y1;
int memsize = dx * dy * this->channels;
uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
int x_source, y_source;
stbi_uc* p_target;
stbi_uc* p_source;
for (int x = x1; x < x2; ++x)
for (int y = y1; y < y2; ++y)
{
p_target = odata + (this->channels * ((y - y1) * dx + (x - x1)));
p_source = this->rgb_image + (this->channels * (y * this->width + x));
for (int channels = 0; channels < this->channels; ++channels)
p_target[channels] = p_source[channels];
}
// stbi_write_jpg(_template1.c_str(), dx, dy, this->channels, odata, 0);
stbi_write_bmp(_template1.c_str(), dx, dy, this->channels, odata);
stbi_image_free(odata);
}

View File

@@ -0,0 +1,254 @@
#include "CTfLiteClass.h"
#include "bitmap_image.hpp"
#include <sys/stat.h>
float CTfLiteClass::GetOutputValue(int nr)
{
TfLiteTensor* output2 = this->interpreter->output(0);
int numeroutput = output2->dims->data[1];
if ((nr+1) > numeroutput)
return -1000;
return output2->data.f[nr];
}
int CTfLiteClass::GetClassFromImage(std::string _fn)
{
// printf("Before Load image %s\n", _fn.c_str());
if (!LoadInputImage(_fn))
return -1000;
// printf("After Load image %s\n", _fn.c_str());
Invoke();
printf("After Invoke %s\n", _fn.c_str());
return GetOutClassification();
// return 0;
}
int CTfLiteClass::GetOutClassification()
{
TfLiteTensor* output2 = interpreter->output(0);
float zw_max = 0;
float zw;
int zw_class = -1;
if (output2 == NULL)
return -1;
int numeroutput = output2->dims->data[1];
for (int i = 0; i < numeroutput; ++i)
{
zw = output2->data.f[i];
if (zw > zw_max)
{
zw_max = zw;
zw_class = i;
}
}
// printf("Result Ziffer: %d\n", zw_class);
return zw_class;
}
void CTfLiteClass::GetInputDimension(bool silent = false)
{
TfLiteTensor* input2 = this->interpreter->input(0);
int numdim = input2->dims->size;
if (!silent) printf("NumDimension: %d\n", numdim);
int sizeofdim;
for (int j = 0; j < numdim; ++j)
{
sizeofdim = input2->dims->data[j];
if (!silent) printf("SizeOfDimension %d: %d\n", j, sizeofdim);
if (j == 1) im_height = sizeofdim;
if (j == 2) im_width = sizeofdim;
if (j == 3) im_channel = sizeofdim;
}
}
void CTfLiteClass::GetOutPut()
{
TfLiteTensor* output2 = this->interpreter->output(0);
int numdim = output2->dims->size;
printf("NumDimension: %d\n", numdim);
int sizeofdim;
for (int j = 0; j < numdim; ++j)
{
sizeofdim = output2->dims->data[j];
printf("SizeOfDimension %d: %d\n", j, sizeofdim);
}
float fo;
// Process the inference results.
int numeroutput = output2->dims->data[1];
for (int i = 0; i < numeroutput; ++i)
{
fo = output2->data.f[i];
printf("Result %d: %f\n", i, fo);
}
}
void CTfLiteClass::Invoke()
{
interpreter->Invoke();
// printf("Invoke Done.\n");
}
bool CTfLiteClass::LoadInputImage(std::string _fn)
{
bitmap_image image(_fn);
unsigned int w = image.width();
unsigned int h = image.height();
unsigned char red, green, blue;
input_i = 0;
float* input_data_ptr = (interpreter->input(0))->data.f;
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x)
{
red = image.red_channel(x, y);
green = image.green_channel(x, y);
blue = image.blue_channel(x, y);
*(input_data_ptr) = (float) red;
input_data_ptr++;
*(input_data_ptr) = (float) green;
input_data_ptr++;
*(input_data_ptr) = (float) blue;
input_data_ptr++;
// printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
}
return true;
}
void CTfLiteClass::MakeAllocate()
{
/*
this->micro_op_resolver.AddBuiltin(
tflite::BuiltinOperator_RESHAPE,
tflite::ops::micro::Register_RESHAPE());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
tflite::ops::micro::Register_CONV_2D());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_FULLY_CONNECTED,
tflite::ops::micro::Register_FULLY_CONNECTED());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_SOFTMAX,
tflite::ops::micro::Register_SOFTMAX());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
tflite::ops::micro::Register_DEPTHWISE_CONV_2D());
this->interpreter = new tflite::MicroInterpreter(this->model, this->micro_op_resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
*/
static tflite::ops::micro::AllOpsResolver resolver;
this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
if (allocate_status != kTfLiteOk) {
TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
this->GetInputDimension();
return;
}
printf("Allocate Done.\n");
}
void CTfLiteClass::GetInputTensorSize(){
float *zw = this->input;
int test = sizeof(zw);
printf("Input Tensor Dimension: %d\n", test);
printf("Input Tensor Dimension: %d\n", test);
}
long CTfLiteClass::GetFileSize(std::string filename)
{
struct stat stat_buf;
long rc = stat(filename.c_str(), &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
{
long size;
size = this->GetFileSize(_fn);
if (size == -1)
{
printf("\nFile existiert nicht.\n");
return NULL;
}
unsigned char *result = (unsigned char*) malloc(size);
if(result != NULL) {
// printf("\nSpeicher ist reserviert\n");
FILE* f = fopen(_fn.c_str(), "rb"); // vorher nur "r"
fread(result, 1, size, f);
fclose(f);
}else {
printf("\nKein freier Speicher vorhanden.\n");
}
return result;
}
void CTfLiteClass::LoadModel(std::string _fn){
this->error_reporter = new tflite::MicroErrorReporter;
unsigned char *rd;
rd = this->ReadFileToCharArray(_fn.c_str());
// printf("loadedfile: %d", (int) rd);
this->model = tflite::GetModel(rd);
free(rd);
TFLITE_MINIMAL_CHECK(model != nullptr);
printf("tfile Loaded.\n");
}
CTfLiteClass::CTfLiteClass()
{
// this->accessSD = _accessSD;
this->model = nullptr;
this->interpreter = nullptr;
this->input = nullptr;
this->output = nullptr;
this->kTensorArenaSize = 600 * 1024;
this->tensor_arena = new uint8_t[kTensorArenaSize];
// micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
// tflite::ops::micro::Register_CONV_2D());
}
CTfLiteClass::~CTfLiteClass()
{
delete this->tensor_arena;
}

View File

@@ -0,0 +1,72 @@
#pragma once
#ifndef __CFINDTEMPLATE
#define __CFINGTEMPLATE
#define TFLITE_MINIMAL_CHECK(x) \
if (!(x)) { \
fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
exit(1); \
}
//#include "CAccessSD.h"
#include "CFindTemplate.h"
#include "tensorflow/lite/micro/kernels/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"
#include "tensorflow/lite/micro/kernels/micro_ops.h"
#include "esp_err.h"
#include "esp_log.h"
//extern CAccessSDClass accessSD;
class CTfLiteClass
{
protected:
// CAccessSDClass *accessSD;
tflite::ErrorReporter* error_reporter;
const tflite::Model* model;
tflite::MicroInterpreter* interpreter;
// TfLiteTensor* input = nullptr;
TfLiteTensor* output = nullptr;
static tflite::ops::micro::AllOpsResolver *resolver;
tflite::MicroOpResolver<5> micro_op_resolver;
int kTensorArenaSize;
uint8_t *tensor_arena;
float* input;
int input_i;
int im_height, im_width, im_channel;
long GetFileSize(std::string filename);
unsigned char* ReadFileToCharArray(std::string _fn);
public:
// CTfLiteClass(CAccessSDClass *_accessSD);
CTfLiteClass();
~CTfLiteClass();
void LoadModel(std::string _fn);
void MakeAllocate();
void GetInputTensorSize();
bool LoadInputImage(std::string _fn);
void Invoke();
void GetOutPut();
int GetOutClassification();
int GetClassFromImage(std::string _fn);
float GetOutputValue(int nr);
void GetInputDimension(bool silent);
};
#endif

View File

@@ -114,6 +114,8 @@ bool CTfLiteClass::LoadInputImage(std::string _fn)
unsigned int h = image.height();
unsigned char red, green, blue;
// printf("Image: %s size: %d x %d\n", _fn.c_str(), w, h);
input_i = 0;
float* input_data_ptr = (interpreter->input(0))->data.f;
@@ -139,24 +141,6 @@ bool CTfLiteClass::LoadInputImage(std::string _fn)
void CTfLiteClass::MakeAllocate()
{
/*
this->micro_op_resolver.AddBuiltin(
tflite::BuiltinOperator_RESHAPE,
tflite::ops::micro::Register_RESHAPE());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
tflite::ops::micro::Register_CONV_2D());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_FULLY_CONNECTED,
tflite::ops::micro::Register_FULLY_CONNECTED());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_SOFTMAX,
tflite::ops::micro::Register_SOFTMAX());
this->micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_DEPTHWISE_CONV_2D,
tflite::ops::micro::Register_DEPTHWISE_CONV_2D());
this->interpreter = new tflite::MicroInterpreter(this->model, this->micro_op_resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
*/
static tflite::ops::micro::AllOpsResolver resolver;
this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
@@ -167,7 +151,7 @@ void CTfLiteClass::MakeAllocate()
return;
}
printf("Allocate Done.\n");
// printf("Allocate Done.\n");
}
void CTfLiteClass::GetInputTensorSize(){
@@ -216,8 +200,11 @@ unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
void CTfLiteClass::LoadModel(std::string _fn){
#ifdef SUPRESS_TFLITE_ERRORS
this->error_reporter = new tflite::OwnMicroErrorReporter;
#else
this->error_reporter = new tflite::MicroErrorReporter;
#endif
unsigned char *rd;
rd = this->ReadFileToCharArray(_fn.c_str());
@@ -226,7 +213,7 @@ void CTfLiteClass::LoadModel(std::string _fn){
this->model = tflite::GetModel(rd);
free(rd);
TFLITE_MINIMAL_CHECK(model != nullptr);
printf("tfile Loaded.\n");
// printf("tfile Loaded.\n");
}
@@ -234,16 +221,12 @@ void CTfLiteClass::LoadModel(std::string _fn){
CTfLiteClass::CTfLiteClass()
{
// this->accessSD = _accessSD;
this->model = nullptr;
this->interpreter = nullptr;
this->input = nullptr;
this->output = nullptr;
this->kTensorArenaSize = 600 * 1024;
this->tensor_arena = new uint8_t[kTensorArenaSize];
// micro_op_resolver.AddBuiltin(tflite::BuiltinOperator_CONV_2D,
// tflite::ops::micro::Register_CONV_2D());
this->tensor_arena = new uint8_t[kTensorArenaSize];
}
CTfLiteClass::~CTfLiteClass()
@@ -252,3 +235,12 @@ CTfLiteClass::~CTfLiteClass()
}
namespace tflite {
int OwnMicroErrorReporter::Report(const char* format, va_list args) {
return 0;
}
} // namespace tflite

View File

@@ -1,7 +1,3 @@
#pragma once
#ifndef __CFINDTEMPLATE
#define __CFINGTEMPLATE
#define TFLITE_MINIMAL_CHECK(x) \
if (!(x)) { \
@@ -9,9 +5,6 @@
exit(1); \
}
//#include "CAccessSD.h"
#include "CFindTemplate.h"
#include "tensorflow/lite/micro/kernels/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
@@ -21,37 +14,44 @@
#include "esp_err.h"
#include "esp_log.h"
//extern CAccessSDClass accessSD;
#define SUPRESS_TFLITE_ERRORS // use, to avoid error messages from TFLITE
#ifdef SUPRESS_TFLITE_ERRORS
#include "tensorflow/lite/core/api/error_reporter.h"
#include "tensorflow/lite/micro/compatibility.h"
#include "tensorflow/lite/micro/debug_log.h"
///// OwnErrorReporter to prevent printing of Errors (especially unavoidable in CalculateActivationRangeQuantized@kerne_util.cc)
namespace tflite {
class OwnMicroErrorReporter : public ErrorReporter {
public:
int Report(const char* format, va_list args) override;
};
} // namespace tflite
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif
class CTfLiteClass
{
protected:
// CAccessSDClass *accessSD;
tflite::ErrorReporter* error_reporter;
tflite::ErrorReporter *error_reporter;
const tflite::Model* model;
tflite::MicroInterpreter* interpreter;
// TfLiteTensor* input = nullptr;
TfLiteTensor* output = nullptr;
static tflite::ops::micro::AllOpsResolver *resolver;
tflite::MicroOpResolver<5> micro_op_resolver;
int kTensorArenaSize;
uint8_t *tensor_arena;
float* input;
int input_i;
int im_height, im_width, im_channel;
long GetFileSize(std::string filename);
unsigned char* ReadFileToCharArray(std::string _fn);
public:
// CTfLiteClass(CAccessSDClass *_accessSD);
CTfLiteClass();
~CTfLiteClass();
void LoadModel(std::string _fn);
@@ -65,8 +65,5 @@ class CTfLiteClass
float GetOutputValue(int nr);
void GetInputDimension(bool silent);
};
#endif