2019/06/30

自宅で動かしている物体認識サーバは TensorFlow を使って Go で書かれていたのだけど、CPU 負荷が高いので以前 go-tflite で書き換えた。その後 Raspberry Pi Zero W でそのまま使えるだろうと思っていたら結構リソースが厳しかったので C++ なウェブサーバの実装である crow と TensorFlow Lite を使って書き換える事にした。

GitHub - ipkn/crow: Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)

How to Build If you just want to use crow, copy amalgamate/crow_all.h and include it. Requirements C...

https://github.com/ipkn/crow/

ただし問題があって、crow は最新の boost を使ってビルドするとエラーが発生する。issue に書いたら親切な人がパッチを貼ってくれたのでローカルの amalgamate コードに適用した。

Cannot build on boost 1.70 · Issue #340 · ipkn/crow · GitHub

g++ -std=c++14 -c -I../include -I. helloworld.cpp -o helloworld.o In file included from ../include/c...

https://github.com/ipkn/crow/issues/340

これで実装できるだろうと思っていたら、なんと crow はマルチパートアップロードをサポートしていない事に気付く。他のウェブサーバ実装を探しても良かったのだけど、どうせ家で動かすウェブサーバだしアタックを受ける事もないので若干雑な実装ながらマルチパートアップロードを実装した。

typedef struct {
  std::map<std::string, std::string> header;
  std::string body;
} part;

static void
trim_string(std::string& s, const char* cutsel = \t\v\r\n") {
  auto left = s.find_first_not_of(cutsel);
  if (left != std::string::npos) {
    auto right = s.find_last_not_of(cutsel);
    s = s.substr(left, right - left + 1);
  }
}

static std::vector<std::string>
split_string(std::string& s, const std::string& sep, int n = -1) {
  std::vector<std::string> result;
  auto pos = std::string::size_type(0);
  while (n != 0) {
    auto next = s.find(sep, pos);
    if (next == std::string::npos) {
      result.push_back(s.substr(pos));
      break;
    }
    result.push_back(s.substr(pos, next - pos));
    pos = next + sep.length();
    --n;
  }
  return result;
}

static std::map<std::string, std::string>
parse_header(std::string& lines) {
  std::map<std::string, std::string> result;
  std::string::size_type pos;
  for (auto& line : split_string(lines, "\r\n")) {
    auto token = split_string(line, ":"2);
    if (token.size() != 2)
      break;
    trim_string(token[0]);
    std::transform(token[0].begin(), token[0].end(), token[0].begin(), ::tolower);
    trim_string(token[1]);
    result[token[0]] = token[1];
  }
  return result;
}

static std::vector<part>
parse_multipart(const crow::request& req, crow::response& res) {
  auto ct = req.get_header_value("content-type");
  auto pos = ct.find("boundary=");

  std::vector<part> result;
  if (pos != std::string::npos) {
    auto boundary = "\n--" + ct.substr(pos + 9);
    pos = boundary.find(";");
    if (pos != std::string::npos)
      boundary = boundary.substr(0, pos);
    pos = 0;
    auto& body = req.body;
    while (true) {
      auto next = body.find(boundary, pos);
      if (next == std::string::npos)
        break;
      auto data = body.substr(pos + boundary.size() + 2, next);
      auto eos = data.find("\r\n\r\n");
      if (eos == std::string::npos)
        break;
      auto lines = data.substr(0, eos);
      part p = {
        .header = parse_header(lines),
        .body = data = data.substr(eos + 4)
      };
      result.push_back(p);
      pos = next + boundary.size() + 1;
      if (body.at(pos) == '-' && body.at(pos + 1) == '-'
          && body.at(pos + 2) == '\n')
        break;
      else if (body.at(pos) != '\n')
        break;
    }
  }
  return result;
}

API 部分しか実装していないので使う場合は以下の様に curl コマンドを使う。

$ curl -X POST http://localhost:8888/upload -F "file=@grace_hopper.png;type=image/png"
[{"label":"bow tie","index":458,"probability":0.996078}]

OpenCV を使ってるので OpenCV がサポートしている画像フォーマットであれば使えるはず。適当なレベルではあるけど、こういうの欲しいと思ってる人もいるんじゃないかなと思ったので GitHub に置いておいた。

GitHub - mattn/crow-tflite-object-detect: Object detection API server using crow webserver and TensorFlow Lite.

crow-tflite-object-detect Object detection API server using crow webserver. Usage -1 curl -X POST htt...

https://github.com/mattn/crow-tflite-object-detect
Real World HTTP ―歴史とコードに学ぶインターネットとウェブ技術 Real World HTTP ―歴史とコードに学ぶインターネットとウェブ技術
渋川 よしき
オライリージャパン 単行本(ソフトカバー) / ¥3,199 (2017年06月14日)
 
発送可能時間:

Posted at 22:54 | WriteBacks () | Edit
Edit this entry...

wikieditish message: Ready to edit this entry.






















A quick preview will be rendered here when you click "Preview" button.