12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #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 PreprocYolov5 : 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(0, 0, 0));
- 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::cvtColor(img, img, cv::COLOR_BGR2RGB);
-
- cv::Mat dst(dst_h, dst_w, CV_32FC3, net_inputs[0]);
- img.convertTo(dst, CV_32F);
- dst /= 255.0;
- return 0;
- }
- private:
- DECLARE_REFLEX_OBJECT_EX(PreprocYolov5, cnstream::Preproc);
- };
- IMPLEMENT_REFLEX_OBJECT_EX(PreprocYolov5, cnstream::Preproc);
|