demo_bare_metal.c 2.24 KB
Newer Older
wangsen's avatar
wangsen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include <stdio.h>
#include <tvm_runtime.h>
#include <tvmgen_rec.h>

#include "uart.h"

// Header files generated by convert_image.py
#include "inputs.h"
#include "outputs.h"


int main(int argc, char** argv) {
  char dict[]={"#0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~!\"#$%&'()*+,-./  "};
  int char_dict_nums = 97;
  uart_init();
  printf("Starting ocr rec inference\n");
  struct tvmgen_rec_outputs rec_outputs = {
      .output = output,
  };
  struct tvmgen_rec_inputs rec_inputs = {
      .x = input,
  };

  tvmgen_rec_run(&rec_inputs, &rec_outputs);

  // post process
  int char_nums = output_len / char_dict_nums;
  
  int last_index = 0;
  float score = 0.f;
  int count = 0;
  
  printf("text: ");
  for (int i = 0; i < char_nums; i++) {
    int argmax_idx = 0;
    float max_value = 0.0f;
    for (int j = 0; j < char_dict_nums; j++){
      if (output[i * char_dict_nums + j] > max_value){
        max_value = output[i * char_dict_nums + j];
        argmax_idx = j;
      }
    }
    if (argmax_idx > 0 && (!(i > 0 && argmax_idx == last_index))) {
      score += max_value;
      count += 1;
      // printf("%d,%f,%c\n", argmax_idx, max_value, dict[argmax_idx]);
      printf("%c", dict[argmax_idx]);
    }
    last_index = argmax_idx;
  }
  score /= count;
  printf(", score: %f\n", score);
  
  // The FVP will shut down when it receives "EXITTHESIM" on the UART
  printf("EXITTHESIM\n");
  while (1 == 1)
    ;
  return 0;
}