text_det_recog.cxx 1.89 KB
Newer Older
limm's avatar
limm 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
79
80
81
82

#include "mmdeploy/archive/json_archive.h"
#include "mmdeploy/core/utils/formatter.h"
#include "mmdeploy/core/value.h"
#include "mmdeploy/pipeline.hpp"
#include "opencv2/imgcodecs.hpp"

const auto config_json = R"(
{
  "type": "Pipeline",
  "input": "img",
  "output": ["dets", "texts"],
  "tasks": [
    {
      "type": "Inference",
      "input": "img",
      "output": "dets",
      "params": { "model": "text_detection" }
    },
    {
      "type": "Pipeline",
      "input": ["bboxes=*dets", "imgs=+img"],
      "tasks": [
        {
          "type": "Task",
          "module": "WarpBbox",
          "scheduler": "thread_pool",
          "input": ["imgs", "bboxes"],
          "output": "patches"
        },
        {
          "type": "Inference",
          "input": "patches",
          "output": "texts",
          "params": { "model": "text_recognition" }
        }
      ],
      "output": "*texts"
    }
  ]
}
)"_json;

using namespace mmdeploy;

int main(int argc, char* argv[]) {
  if (argc != 5) {
    fprintf(stderr,
            "usage:\n\ttext_det_recog device_name det_model_path reg_model_path image_path\n");
    return -1;
  }

  auto device_name = argv[1];
  auto det_model_path = argv[2];
  auto reg_model_path = argv[3];
  auto image_path = argv[4];

  cv::Mat mat = cv::imread(image_path);
  if (!mat.data) {
    fprintf(stderr, "failed to open image %s\n", image_path);
    return -1;
  }

  auto config = from_json<Value>(config_json);

  Context context(Device(device_name, 0));

  auto thread_pool = Scheduler::ThreadPool(4);
  auto infer_thread = Scheduler::Thread();
  context.Add("thread_pool", thread_pool);
  context.Add("infer_thread", infer_thread);
  context.Add("text_detection", Model(det_model_path));
  context.Add("text_recognition", Model(reg_model_path));

  Pipeline pipeline(config, context);

  auto output = pipeline.Apply(mat);

  // MMDEPLOY_INFO("output:\n{}", output);

  return 0;
}