123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include <algorithm>
- #include <memory>
- #include <string>
- #include <vector>
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #if (CV_MAJOR_VERSION >= 3)
- #include "opencv2/imgcodecs/imgcodecs.hpp"
- #endif
- #include "cnis/contrib/video_helper.h"
- #include "cnstream_frame_va.hpp"
- #include "cnstream_logging.hpp"
- #include "video_preproc.hpp"
- #include "video_preprocess_common.hpp"
- class VideoPreprocYolov5 : public cnstream::VideoPreproc {
- public:
-
- bool Execute(infer_server::ModelIO* model_input, const infer_server::InferData& input_data,
- const infer_server::ModelInfo& model_info) {
-
- uint32_t input_num = model_info.InputNum();
- if (input_num != 1) {
- LOGE(DEMO) << "[VideoPreprocYolov5] model input number not supported. It should be 1, but " << input_num;
- return false;
- }
- infer_server::Shape input_shape;
- input_shape = model_info.InputShape(0);
- int c_idx = 3;
- int w_idx = 2;
- int h_idx = 1;
- if (model_info.InputLayout(0).order == infer_server::DimOrder::NCHW) {
- c_idx = 1;
- w_idx = 3;
- h_idx = 2;
- }
- if (input_shape[c_idx] != 3) {
- LOGE(DEMO) << "[VideoPreprocYolov5] model input shape not supported, `c` should be 3, but " << input_shape[c_idx];
- return false;
- }
-
- const infer_server::video::VideoFrame& frame = input_data.GetLref<infer_server::video::VideoFrame>();
- size_t src_w = frame.width;
- size_t src_h = frame.height;
- uint32_t dst_w = input_shape[w_idx];
- uint32_t dst_h = input_shape[h_idx];
- uint8_t* img_data = new (std::nothrow) uint8_t[frame.GetTotalSize()];
- if (!img_data) {
- LOGE(DEMO) << "[VideoPreprocYolov5] Failed to alloc memory, size: " << frame.GetTotalSize();
- return false;
- }
- uint8_t* img_data_tmp = img_data;
- for (auto plane_idx = 0u; plane_idx < frame.plane_num; ++plane_idx) {
- memcpy(img_data_tmp, frame.plane[plane_idx].Data(), frame.GetPlaneSize(plane_idx));
- img_data_tmp += frame.GetPlaneSize(plane_idx);
- }
-
- cv::Mat dst_cvt_color_img;
- if (!ConvertColorSpace(src_w, src_h, frame.format, model_input_pixel_format_, img_data, &dst_cvt_color_img)) {
- LOGW(DEMO) << "[VideoPreprocYolov5] Unsupport pixel format. src: " << static_cast<int>(frame.format)
- << " dst: " << static_cast<int>(model_input_pixel_format_);
- delete[] img_data;
- return false;
- }
- cv::Mat img = dst_cvt_color_img;
-
- if (src_h != dst_h || src_w != dst_w) {
- cv::Mat dst(dst_h, dst_w, CV_8UC3, cv::Scalar(0, 0, 0));
- const float scaling_factors = std::min(1.0 * dst_w / src_w, 1.0 * dst_h / src_h);
- cv::Mat resized(src_h * scaling_factors, src_w * scaling_factors, CV_8UC3);
- cv::resize(img, resized, cv::Size(resized.cols, resized.rows));
- cv::Rect roi;
- roi.x = (dst.cols - resized.cols) / 2;
- roi.y = (dst.rows - resized.rows) / 2;
- roi.width = resized.cols;
- roi.height = resized.rows;
- resized.copyTo(dst(roi));
- img = dst;
- }
-
- cv::Mat dst(dst_h, dst_w, CV_32FC3, model_input->buffers[0].MutableData());
- img.convertTo(dst, CV_32FC3);
- dst /= 255.0;
- delete[] img_data;
- return true;
- }
- private:
- DECLARE_REFLEX_OBJECT_EX(VideoPreprocYolov5, cnstream::VideoPreproc);
- };
- IMPLEMENT_REFLEX_OBJECT_EX(VideoPreprocYolov5, cnstream::VideoPreproc);
|