2021-12-07 10:57:55 +08:00
|
|
|
// Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
|
|
|
|
#include "compose.h"
|
|
|
|
|
|
|
|
#include "archive/json_archive.h"
|
|
|
|
#include "archive/value_archive.h"
|
|
|
|
#include "core/utils/formatter.h"
|
|
|
|
|
|
|
|
namespace mmdeploy {
|
|
|
|
|
|
|
|
Compose::Compose(const Value& args, int version) : Transform(args) {
|
|
|
|
assert(args.contains("context"));
|
|
|
|
|
|
|
|
Value context;
|
|
|
|
context = args["context"];
|
2021-12-16 13:51:22 +08:00
|
|
|
context["stream"].get_to(stream_);
|
2021-12-07 10:57:55 +08:00
|
|
|
for (auto cfg : args["transforms"]) {
|
|
|
|
cfg["context"] = context;
|
|
|
|
auto type = cfg.value("type", std::string{});
|
2022-02-24 20:08:44 +08:00
|
|
|
MMDEPLOY_DEBUG("creating transform: {} with cfg: {}", type, mmdeploy::to_json(cfg).dump(2));
|
2021-12-07 10:57:55 +08:00
|
|
|
auto creator = Registry<Transform>::Get().GetCreator(type, version);
|
|
|
|
if (!creator) {
|
2022-02-24 20:08:44 +08:00
|
|
|
MMDEPLOY_ERROR("unable to find creator: {}", type);
|
2021-12-07 10:57:55 +08:00
|
|
|
throw std::invalid_argument("unable to find creator");
|
|
|
|
}
|
|
|
|
auto transform = creator->Create(cfg);
|
|
|
|
if (!transform) {
|
2022-02-24 20:08:44 +08:00
|
|
|
MMDEPLOY_ERROR("failed to create transform: {}", type);
|
2021-12-07 10:57:55 +08:00
|
|
|
throw std::invalid_argument("failed to create transform");
|
|
|
|
}
|
|
|
|
transforms_.push_back(std::move(transform));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<Value> Compose::Process(const Value& input) {
|
|
|
|
Value output = input;
|
|
|
|
for (auto& transform : transforms_) {
|
|
|
|
auto t = transform->Process(output);
|
2021-12-16 13:51:22 +08:00
|
|
|
OUTCOME_TRY(stream_.Wait());
|
2021-12-07 10:57:55 +08:00
|
|
|
if (!t) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
output = std::move(t).value();
|
|
|
|
}
|
|
|
|
return std::move(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
class ComposeCreator : public Creator<Transform> {
|
|
|
|
public:
|
|
|
|
const char* GetName() const override { return "Compose"; }
|
|
|
|
int GetVersion() const override { return version_; }
|
|
|
|
ReturnType Create(const Value& args) override {
|
|
|
|
return std::make_unique<Compose>(args, version_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int version_{1};
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_MODULE(Transform, ComposeCreator);
|
|
|
|
} // namespace mmdeploy
|