Apollo 10.0
自动驾驶开放平台
compress_component.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <exception>
20#include <vector>
21
22#include "opencv2/core/core.hpp"
23#include "opencv2/highgui/highgui.hpp"
24#include "opencv2/imgproc/imgproc.hpp"
25
26namespace apollo {
27namespace drivers {
28namespace smartereye {
29
31 if (!GetProtoConfig(&config_)) {
32 AERROR << "Parse config file failed: " << ConfigFilePath();
33 return false;
34 }
35 AINFO << "Camera config: \n" << config_.DebugString();
36 try {
37 image_pool_.reset(new CCObjectPool<CompressedImage>(
38 config_.compress_conf().image_pool_size()));
39 image_pool_->ConstructAll();
40 } catch (const std::bad_alloc& e) {
41 AERROR << e.what();
42 return false;
43 }
44
45 writer_ = node_->CreateWriter<CompressedImage>(
46 config_.compress_conf().output_channel());
47 return true;
48}
49
50bool CompressComponent::Proc(const std::shared_ptr<Image>& image) {
51 ADEBUG << "procing compressed";
52 auto compressed_image = image_pool_->GetObject();
53 compressed_image->mutable_header()->CopyFrom(image->header());
54 compressed_image->set_frame_id(image->frame_id());
55 compressed_image->set_measurement_time(image->measurement_time());
56 compressed_image->set_format(image->encoding() + "; jpeg compressed bgr8");
57
58 std::vector<int> params;
59 params.resize(3, 0);
60 params[0] = cv::IMWRITE_JPEG_QUALITY;
61 params[1] = 95;
62
63 try {
64 cv::Mat mat_image(image->height(), image->width(), CV_8UC3,
65 const_cast<char*>(image->data().data()), image->step());
66 cv::Mat tmp_mat;
67 cv::cvtColor(mat_image, tmp_mat, cv::COLOR_RGB2BGR);
68 std::vector<uint8_t> compress_buffer;
69 if (!cv::imencode(".jpg", tmp_mat, compress_buffer, params)) {
70 AERROR << "cv::imencode (jpeg) failed on input image";
71 return false;
72 }
73 compressed_image->set_data(compress_buffer.data(), compress_buffer.size());
74 writer_->Write(compressed_image);
75 } catch (std::exception& e) {
76 AERROR << "cv::imencode (jpeg) exception :" << e.what();
77 return false;
78 }
79 return true;
80}
81
82} // namespace smartereye
83} // namespace drivers
84} // namespace apollo
bool GetProtoConfig(T *config) const
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
bool Proc(const std::shared_ptr< Image > &image) override
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37