123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #include <algorithm>
- #include <memory>
- #include <vector>
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #if (CV_MAJOR_VERSION >= 3)
- #include "opencv2/imgcodecs/imgcodecs.hpp"
- #endif
- #include "cnstream_frame_va.hpp"
- #include "preproc.hpp"
- #include "cnstream_logging.hpp"
- class PreprocYolov3 : public cnstream::Preproc {
- public:
- int Execute(const std::vector<float*>& net_inputs, const std::shared_ptr<edk::ModelLoader>& model,
- const std::shared_ptr<cnstream::CNFrameInfo>& package) {
-
- auto input_shape = model->InputShape(0);
- if (net_inputs.size() != 1 || input_shape.C() != 3) {
- LOGE(DEMO) << "[PreprocCpu] model input shape not supported";
- return -1;
- }
- cnstream::CNDataFramePtr frame = package->collection.Get<cnstream::CNDataFramePtr>(cnstream::kCNDataFrameTag);
- int width = frame->width;
- int height = frame->height;
- int dst_w = input_shape.W();
- int dst_h = input_shape.H();
- cv::Mat img = frame->ImageBGR();
-
- if (height != dst_h || width != dst_w) {
- cv::Mat dst(dst_h, dst_w, CV_8UC3, cv::Scalar(128, 128, 128));
- const float scaling_factors = std::min(1.0 * dst_w / width, 1.0 * dst_h / height);
- cv::Mat resized(height * scaling_factors, width * 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, net_inputs[0]);
- img.convertTo(dst, CV_32F);
- return 0;
- }
- private:
- DECLARE_REFLEX_OBJECT_EX(PreprocYolov3, cnstream::Preproc);
- };
- IMPLEMENT_REFLEX_OBJECT_EX(PreprocYolov3, cnstream::Preproc);
|