feature_output.cc 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/******************************************************************************
 * Copyright 2017 The Apollo Authors. All Rights Reserved.
 *
 * Licensed 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 "modules/prediction/common/feature_output.h"

19 20
#include <string>

21
#include "modules/common/log.h"
22 23
#include "modules/common/util/file.h"
#include "modules/prediction/common/prediction_gflags.h"
24 25 26 27

namespace apollo {
namespace prediction {

28
FeatureOutput::FeatureOutput() {}
K
kechxu 已提交
29

30
FeatureOutput::~FeatureOutput() {
K
kechxu 已提交
31 32 33 34 35
  Clear();
}

bool FeatureOutput::Open() {
  return true;
36
}
37

38 39 40
void FeatureOutput::Close() {
  ADEBUG << "Close feature output";
  Write();
K
kechxu 已提交
41 42 43 44 45 46
  Clear();
}

void FeatureOutput::Clear() {
  count_ = 0;
  features_.Clear();
47
}
48

49
bool FeatureOutput::Ready() { return Open(); }
K
kechxu 已提交
50

51 52 53 54 55
void FeatureOutput::Insert(const Feature& feature) {
  features_.add_feature()->CopyFrom(feature);
}

void FeatureOutput::Write() {
K
kechxu 已提交
56 57 58 59
  if (features_.feature_size() <= 0) {
    ADEBUG << "Skip writing empty feature.";
    return;
  }
60 61 62 63 64 65
  std::string file_name = FLAGS_prediction_data_file_prefix +
                          std::to_string(count_) + ".bin";
  apollo::common::util::SetProtoToBinaryFile(features_, file_name);
  features_.Clear();
  ++count_;
}
66 67 68

}  // namespace prediction
}  // namespace apollo