CTfLiteClass.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "CTfLiteClass.h"
  2. #include "bitmap_image.hpp"
  3. #include <sys/stat.h>
  4. float CTfLiteClass::GetOutputValue(int nr)
  5. {
  6. TfLiteTensor* output2 = this->interpreter->output(0);
  7. int numeroutput = output2->dims->data[1];
  8. if ((nr+1) > numeroutput)
  9. return -1000;
  10. return output2->data.f[nr];
  11. }
  12. int CTfLiteClass::GetClassFromImage(std::string _fn)
  13. {
  14. // printf("Before Load image %s\n", _fn.c_str());
  15. if (!LoadInputImage(_fn))
  16. return -1000;
  17. // printf("After Load image %s\n", _fn.c_str());
  18. Invoke();
  19. printf("After Invoke %s\n", _fn.c_str());
  20. return GetOutClassification();
  21. // return 0;
  22. }
  23. int CTfLiteClass::GetOutClassification()
  24. {
  25. TfLiteTensor* output2 = interpreter->output(0);
  26. float zw_max = 0;
  27. float zw;
  28. int zw_class = -1;
  29. if (output2 == NULL)
  30. return -1;
  31. int numeroutput = output2->dims->data[1];
  32. for (int i = 0; i < numeroutput; ++i)
  33. {
  34. zw = output2->data.f[i];
  35. if (zw > zw_max)
  36. {
  37. zw_max = zw;
  38. zw_class = i;
  39. }
  40. }
  41. // printf("Result Ziffer: %d\n", zw_class);
  42. return zw_class;
  43. }
  44. void CTfLiteClass::GetInputDimension(bool silent = false)
  45. {
  46. TfLiteTensor* input2 = this->interpreter->input(0);
  47. int numdim = input2->dims->size;
  48. if (!silent) printf("NumDimension: %d\n", numdim);
  49. int sizeofdim;
  50. for (int j = 0; j < numdim; ++j)
  51. {
  52. sizeofdim = input2->dims->data[j];
  53. if (!silent) printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  54. if (j == 1) im_height = sizeofdim;
  55. if (j == 2) im_width = sizeofdim;
  56. if (j == 3) im_channel = sizeofdim;
  57. }
  58. }
  59. void CTfLiteClass::GetOutPut()
  60. {
  61. TfLiteTensor* output2 = this->interpreter->output(0);
  62. int numdim = output2->dims->size;
  63. printf("NumDimension: %d\n", numdim);
  64. int sizeofdim;
  65. for (int j = 0; j < numdim; ++j)
  66. {
  67. sizeofdim = output2->dims->data[j];
  68. printf("SizeOfDimension %d: %d\n", j, sizeofdim);
  69. }
  70. float fo;
  71. // Process the inference results.
  72. int numeroutput = output2->dims->data[1];
  73. for (int i = 0; i < numeroutput; ++i)
  74. {
  75. fo = output2->data.f[i];
  76. printf("Result %d: %f\n", i, fo);
  77. }
  78. }
  79. void CTfLiteClass::Invoke()
  80. {
  81. interpreter->Invoke();
  82. // printf("Invoke Done.\n");
  83. }
  84. bool CTfLiteClass::LoadInputImage(std::string _fn)
  85. {
  86. bitmap_image image(_fn);
  87. unsigned int w = image.width();
  88. unsigned int h = image.height();
  89. unsigned char red, green, blue;
  90. // printf("Image: %s size: %d x %d\n", _fn.c_str(), w, h);
  91. input_i = 0;
  92. float* input_data_ptr = (interpreter->input(0))->data.f;
  93. for (int y = 0; y < h; ++y)
  94. for (int x = 0; x < w; ++x)
  95. {
  96. red = image.red_channel(x, y);
  97. green = image.green_channel(x, y);
  98. blue = image.blue_channel(x, y);
  99. *(input_data_ptr) = (float) red;
  100. input_data_ptr++;
  101. *(input_data_ptr) = (float) green;
  102. input_data_ptr++;
  103. *(input_data_ptr) = (float) blue;
  104. input_data_ptr++;
  105. // printf("BMP: %f %f %f\n", (float) red, (float) green, (float) blue);
  106. }
  107. return true;
  108. }
  109. void CTfLiteClass::MakeAllocate()
  110. {
  111. static tflite::ops::micro::AllOpsResolver resolver;
  112. this->interpreter = new tflite::MicroInterpreter(this->model, resolver, this->tensor_arena, this->kTensorArenaSize, this->error_reporter);
  113. TfLiteStatus allocate_status = this->interpreter->AllocateTensors();
  114. if (allocate_status != kTfLiteOk) {
  115. TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
  116. this->GetInputDimension();
  117. return;
  118. }
  119. // printf("Allocate Done.\n");
  120. }
  121. void CTfLiteClass::GetInputTensorSize(){
  122. float *zw = this->input;
  123. int test = sizeof(zw);
  124. printf("Input Tensor Dimension: %d\n", test);
  125. printf("Input Tensor Dimension: %d\n", test);
  126. }
  127. long CTfLiteClass::GetFileSize(std::string filename)
  128. {
  129. struct stat stat_buf;
  130. long rc = stat(filename.c_str(), &stat_buf);
  131. return rc == 0 ? stat_buf.st_size : -1;
  132. }
  133. unsigned char* CTfLiteClass::ReadFileToCharArray(std::string _fn)
  134. {
  135. long size;
  136. size = this->GetFileSize(_fn);
  137. if (size == -1)
  138. {
  139. printf("\nFile existiert nicht.\n");
  140. return NULL;
  141. }
  142. unsigned char *result = (unsigned char*) malloc(size);
  143. if(result != NULL) {
  144. // printf("\nSpeicher ist reserviert\n");
  145. FILE* f = fopen(_fn.c_str(), "rb"); // vorher nur "r"
  146. fread(result, 1, size, f);
  147. fclose(f);
  148. }else {
  149. printf("\nKein freier Speicher vorhanden.\n");
  150. }
  151. return result;
  152. }
  153. void CTfLiteClass::LoadModel(std::string _fn){
  154. #ifdef SUPRESS_TFLITE_ERRORS
  155. this->error_reporter = new tflite::OwnMicroErrorReporter;
  156. #else
  157. this->error_reporter = new tflite::MicroErrorReporter;
  158. #endif
  159. unsigned char *rd;
  160. rd = this->ReadFileToCharArray(_fn.c_str());
  161. // printf("loadedfile: %d", (int) rd);
  162. this->model = tflite::GetModel(rd);
  163. free(rd);
  164. TFLITE_MINIMAL_CHECK(model != nullptr);
  165. // printf("tfile Loaded.\n");
  166. }
  167. CTfLiteClass::CTfLiteClass()
  168. {
  169. this->model = nullptr;
  170. this->interpreter = nullptr;
  171. this->input = nullptr;
  172. this->output = nullptr;
  173. this->kTensorArenaSize = 600 * 1024;
  174. this->tensor_arena = new uint8_t[kTensorArenaSize];
  175. }
  176. CTfLiteClass::~CTfLiteClass()
  177. {
  178. delete this->tensor_arena;
  179. }
  180. namespace tflite {
  181. int OwnMicroErrorReporter::Report(const char* format, va_list args) {
  182. return 0;
  183. }
  184. } // namespace tflite