main_functions.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "main_functions.h"
  13. #include "detection_responder.h"
  14. #include "image_provider.h"
  15. #include "model_settings.h"
  16. #include "tensorflow/lite/micro/micro_error_reporter.h"
  17. #include "tensorflow/lite/micro/micro_interpreter.h"
  18. #include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
  19. #include "tensorflow/lite/micro/models/person_detect_model_data.h"
  20. #include "tensorflow/lite/micro/system_setup.h"
  21. #include "tensorflow/lite/schema/schema_generated.h"
  22. // Globals, used for compatibility with Arduino-style sketches.
  23. namespace {
  24. tflite::ErrorReporter* error_reporter = nullptr;
  25. const tflite::Model* model = nullptr;
  26. tflite::MicroInterpreter* interpreter = nullptr;
  27. TfLiteTensor* input = nullptr;
  28. // In order to use optimized tensorflow lite kernels, a signed int8_t quantized
  29. // model is preferred over the legacy unsigned model format. This means that
  30. // throughout this project, input images must be converted from unisgned to
  31. // signed format. The easiest and quickest way to convert from unsigned to
  32. // signed 8-bit integers is to subtract 128 from the unsigned value to get a
  33. // signed value.
  34. // An area of memory to use for input, output, and intermediate arrays.
  35. constexpr int kTensorArenaSize = 136 * 1024;
  36. static uint8_t tensor_arena[kTensorArenaSize];
  37. } // namespace
  38. // The name of this function is important for Arduino compatibility.
  39. void setup() {
  40. tflite::InitializeTarget();
  41. // Set up logging. Google style is to avoid globals or statics because of
  42. // lifetime uncertainty, but since this has a trivial destructor it's okay.
  43. // NOLINTNEXTLINE(runtime-global-variables)
  44. static tflite::MicroErrorReporter micro_error_reporter;
  45. error_reporter = &micro_error_reporter;
  46. // Map the model into a usable data structure. This doesn't involve any
  47. // copying or parsing, it's a very lightweight operation.
  48. model = tflite::GetModel(g_person_detect_model_data);
  49. if (model->version() != TFLITE_SCHEMA_VERSION) {
  50. TF_LITE_REPORT_ERROR(error_reporter,
  51. "Model provided is schema version %d not equal "
  52. "to supported version %d.",
  53. model->version(), TFLITE_SCHEMA_VERSION);
  54. return;
  55. }
  56. // Pull in only the operation implementations we need.
  57. // This relies on a complete list of all the ops needed by this graph.
  58. // An easier approach is to just use the AllOpsResolver, but this will
  59. // incur some penalty in code space for op implementations that are not
  60. // needed by this graph.
  61. //
  62. // tflite::AllOpsResolver resolver;
  63. // NOLINTNEXTLINE(runtime-global-variables)
  64. static tflite::MicroMutableOpResolver<5> micro_op_resolver;
  65. micro_op_resolver.AddAveragePool2D();
  66. micro_op_resolver.AddConv2D();
  67. micro_op_resolver.AddDepthwiseConv2D();
  68. micro_op_resolver.AddReshape();
  69. micro_op_resolver.AddSoftmax();
  70. // Build an interpreter to run the model with.
  71. // NOLINTNEXTLINE(runtime-global-variables)
  72. static tflite::MicroInterpreter static_interpreter(
  73. model, micro_op_resolver, tensor_arena, kTensorArenaSize, error_reporter);
  74. interpreter = &static_interpreter;
  75. // Allocate memory from the tensor_arena for the model's tensors.
  76. TfLiteStatus allocate_status = interpreter->AllocateTensors();
  77. if (allocate_status != kTfLiteOk) {
  78. TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
  79. return;
  80. }
  81. // Get information about the memory area to use for the model's input.
  82. input = interpreter->input(0);
  83. }
  84. // The name of this function is important for Arduino compatibility.
  85. void loop() {
  86. // Get image from provider.
  87. if (kTfLiteOk != GetImage(error_reporter, kNumCols, kNumRows, kNumChannels,
  88. input->data.int8)) {
  89. TF_LITE_REPORT_ERROR(error_reporter, "Image capture failed.");
  90. }
  91. // Run the model on this input and make sure it succeeds.
  92. if (kTfLiteOk != interpreter->Invoke()) {
  93. TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed.");
  94. }
  95. TfLiteTensor* output = interpreter->output(0);
  96. // Process the inference results.
  97. int8_t person_score = output->data.uint8[kPersonIndex];
  98. int8_t no_person_score = output->data.uint8[kNotAPersonIndex];
  99. RespondToDetection(error_reporter, person_score, no_person_score);
  100. }
  101. /**
  102. * @brief 进行人形识别
  103. *
  104. * @param in int8[96*96] 输入分辨率为96*96的灰度数据,每个像素1byte
  105. * @param out 输出结果, int8[2]
  106. * @return int 成功返回0, 否则返回负值
  107. */
  108. int do_person_detection(signed char* in, signed char* out) {
  109. if (input == NULL)
  110. return -1;
  111. memcpy(input->data.int8, in, kNumCols * kNumRows * kNumChannels);
  112. if (kTfLiteOk != interpreter->Invoke()) {
  113. return -2;
  114. }
  115. TfLiteTensor* output = interpreter->output(0);
  116. memcpy(out, output->data.uint8, 2);
  117. //int8_t person_score = output->data.uint8[kPersonIndex];
  118. //int8_t no_person_score = output->data.uint8[kNotAPersonIndex];
  119. return 0;
  120. }