CTfLiteClass.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #define TFLITE_MINIMAL_CHECK(x) \
  2. if (!(x)) { \
  3. fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
  4. exit(1); \
  5. }
  6. #include "tensorflow/lite/micro/all_ops_resolver.h"
  7. #include "tensorflow/lite/micro/micro_error_reporter.h"
  8. #include "tensorflow/lite/micro/micro_interpreter.h"
  9. #include "tensorflow/lite/schema/schema_generated.h"
  10. #include "tensorflow/lite/micro/kernels/micro_ops.h"
  11. #include "esp_err.h"
  12. #include "esp_log.h"
  13. #include "CImageBasis.h"
  14. #define SUPRESS_TFLITE_ERRORS // use, to avoid error messages from TFLITE
  15. #ifdef SUPRESS_TFLITE_ERRORS
  16. #include "tensorflow/lite/core/api/error_reporter.h"
  17. #include "tensorflow/lite/micro/compatibility.h"
  18. #include "tensorflow/lite/micro/debug_log.h"
  19. ///// OwnErrorReporter to prevent printing of Errors (especially unavoidable in CalculateActivationRangeQuantized@kerne_util.cc)
  20. namespace tflite {
  21. class OwnMicroErrorReporter : public ErrorReporter {
  22. public:
  23. int Report(const char* format, va_list args) override;
  24. };
  25. } // namespace tflite
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. #endif
  28. class CTfLiteClass
  29. {
  30. protected:
  31. tflite::ErrorReporter *error_reporter;
  32. const tflite::Model* model;
  33. tflite::MicroInterpreter* interpreter;
  34. TfLiteTensor* output = nullptr;
  35. static tflite::AllOpsResolver resolver;
  36. int kTensorArenaSize;
  37. uint8_t *tensor_arena;
  38. unsigned char *modelload = NULL;
  39. float* input;
  40. int input_i;
  41. int im_height, im_width, im_channel;
  42. long GetFileSize(std::string filename);
  43. unsigned char* ReadFileToCharArray(std::string _fn);
  44. public:
  45. CTfLiteClass();
  46. ~CTfLiteClass();
  47. bool LoadModel(std::string _fn);
  48. void MakeAllocate();
  49. void GetInputTensorSize();
  50. bool LoadInputImageBasis(CImageBasis *rs);
  51. void Invoke();
  52. int GetAnzOutPut(bool silent = true);
  53. int GetOutClassification(int _von = -1, int _bis = -1);
  54. int GetClassFromImageBasis(CImageBasis *rs);
  55. std::string GetStatusFlow();
  56. float GetOutputValue(int nr);
  57. void GetInputDimension(bool silent);
  58. int ReadInputDimenstion(int _dim);
  59. };