demo_bare_metal.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. #include <stdio.h>
  20. #include <tvm_runtime.h>
  21. #include <tvmgen_rec.h>
  22. #include "uart.h"
  23. // Header files generated by convert_image.py
  24. #include "inputs.h"
  25. #include "outputs.h"
  26. int main(int argc, char** argv) {
  27. char dict[]={"#0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~!\"#$%&'()*+,-./ "};
  28. int char_dict_nums = 97;
  29. uart_init();
  30. printf("Starting ocr rec inference\n");
  31. struct tvmgen_rec_outputs rec_outputs = {
  32. .output = output,
  33. };
  34. struct tvmgen_rec_inputs rec_inputs = {
  35. .x = input,
  36. };
  37. tvmgen_rec_run(&rec_inputs, &rec_outputs);
  38. // post process
  39. int char_nums = output_len / char_dict_nums;
  40. int last_index = 0;
  41. float score = 0.f;
  42. int count = 0;
  43. printf("text: ");
  44. for (int i = 0; i < char_nums; i++) {
  45. int argmax_idx = 0;
  46. float max_value = 0.0f;
  47. for (int j = 0; j < char_dict_nums; j++){
  48. if (output[i * char_dict_nums + j] > max_value){
  49. max_value = output[i * char_dict_nums + j];
  50. argmax_idx = j;
  51. }
  52. }
  53. if (argmax_idx > 0 && (!(i > 0 && argmax_idx == last_index))) {
  54. score += max_value;
  55. count += 1;
  56. // printf("%d,%f,%c\n", argmax_idx, max_value, dict[argmax_idx]);
  57. printf("%c", dict[argmax_idx]);
  58. }
  59. last_index = argmax_idx;
  60. }
  61. score /= count;
  62. printf(", score: %f\n", score);
  63. // The FVP will shut down when it receives "EXITTHESIM" on the UART
  64. printf("EXITTHESIM\n");
  65. while (1 == 1)
  66. ;
  67. return 0;
  68. }