Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Add Image rotation or something like cv::warpAffine for ov::preprocess::PrePostProcessor #28072

Open
1 task done
personball opened this issue Dec 15, 2024 · 2 comments
Labels
enhancement New feature or request feature New feature request

Comments

@personball
Copy link

personball commented Dec 15, 2024

Request Description

In my application, it needs to inference a 1920x1080 image and its 180-degree rotated copy.
After read all doc of ov::preprocess, I didn't found any step like image rotation.
It's better to run this rotation before model input on GPU than use opencv cv::warpAffine on CPU.

Feature Use Case

for now:

cv::Point2f center(1920 / 2.0, 1080 / 2.0);
cv::Mat rotation_matrix = cv::getRotationMatrix2D(center, 180, 1.0);
cv::Mat img_rotated;
cv::warpAffine(img, img_rotated, rotation_matrix, img.size());

something better:

ppp.input().preprocess()
    .convert_element_type(ov::element::f32)
    .scale({ 255.0, 255.0 , 255.0 })
    .rotate(rotation_matrix) // like this
    .warpAffine(rotation_matrix)// or like this, more affine effect
    .convert_color(ov::preprocess::ColorFormat::RGB);

Issue submission checklist

  • The feature request or improvement must be related to OpenVINO
@personball personball added enhancement New feature or request feature New feature request labels Dec 15, 2024
@ilya-lavrenov
Copy link
Contributor

It can be implemented via GridSample operation (see https://docs.openvino.ai/2024/documentation/openvino-ir-format/operation-sets/operation-specs/image/grid-sample-9.html)

#include <openvino/openvino.hpp>

using namespace ov;

// Step 1: Input Node
auto input_shape = ov::Shape{1, 3, 224, 224}; // Batch size, channels, height, width
auto input = std::make_shared<op::v0::Parameter>(element::f32, input_shape);

// Step 2: Affine Transformation Matrix
std::vector<float> affine_matrix_data = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0}; // Identity affine matrix
auto affine_matrix = op::v0::Constant::create(element::f32, {2, 3}, affine_matrix_data);

// Step 3: Convert to 4x4 Transformation Matrix
std::vector<float> matrix_4x4_data = {
    affine_matrix_data[0], affine_matrix_data[1], 0.0f, affine_matrix_data[2],
    affine_matrix_data[3], affine_matrix_data[4], 0.0f, affine_matrix_data[5],
    0.0f,                0.0f,                1.0f, 0.0f,
    0.0f,                0.0f,                0.0f, 1.0f,
};
auto matrix_4x4 = op::v0::Constant::create(element::f32, {4, 4}, matrix_4x4_data);

// Step 4: Grid Sample Operation
auto grid_sample = std::make_shared<op::v9::GridSample>(
    input, matrix_4x4,
    op::v9::GridSample::Attributes{
        op::v9::GridSample::InterpolationMode::BILINEAR,
        op::v9::GridSample::PaddingMode::ZEROS,
        false // align_corners
    });

// Step 5: Finalize and create the model
auto model = std::make_shared<Model>(NodeVector{grid_sample}, ParameterVector{input});

You can use OpenVINO Preprocessing custom operation https://docs.openvino.ai/2024/openvino-workflow/running-inference/optimize-inference/optimize-preprocessing/preprocessing-api-details.html#custom-operations guide to add warpAffine which is based on OpenVINO operations.

In case of success, could you please try to contribute it to OpenVINO preprocessing API?

@personball
Copy link
Author

@ilya-lavrenov thank you very much!

and, one more thing, do we have some post process sample code to impl nms(NonMaxSuppression) for model output?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature New feature request
Projects
None yet
Development

No branches or pull requests

2 participants