CTfLiteClass.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/kernels/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/version.h"
  11. #include "tensorflow/lite/micro/kernels/micro_ops.h"
  12. #include "esp_err.h"
  13. #include "esp_log.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::ops::micro::AllOpsResolver *resolver;
  36. int kTensorArenaSize;
  37. uint8_t *tensor_arena;
  38. float* input;
  39. int input_i;
  40. int im_height, im_width, im_channel;
  41. long GetFileSize(std::string filename);
  42. unsigned char* ReadFileToCharArray(std::string _fn);
  43. public:
  44. CTfLiteClass();
  45. ~CTfLiteClass();
  46. void LoadModel(std::string _fn);
  47. void MakeAllocate();
  48. void GetInputTensorSize();
  49. bool LoadInputImage(std::string _fn);
  50. void Invoke();
  51. void GetOutPut();
  52. int GetOutClassification();
  53. int GetClassFromImage(std::string _fn);
  54. float GetOutputValue(int nr);
  55. void GetInputDimension(bool silent);
  56. };