Coder Social home page Coder Social logo

Comments (6)

mfl28 avatar mfl28 commented on May 11, 2024 1

I got this to work with the following code, without using OpenCV:

torch::Tensor image_to_tensor(const juce::Image& image) {
    juce::Image::BitmapData image_data(image, juce::Image::BitmapData::readOnly);

    auto num_channels = format_to_num_channels(image.getFormat());

    // output tensor format: [num_channels, height, width], values in interval [0, 1]
    return torch::from_blob(image_data.data, {image.getHeight(), image.getWidth(), num_channels}, torch::kUInt8)
           .clone()
           .to(torch::kFloat32)
           .permute({2, 0, 1})
           .div_(255);
}

juce::Image tensor_to_image(const torch::Tensor& tensor) {
    // expected tensor format: [num_channels, height, width], values in interval [0, 1]
    auto image_format = num_channels_to_format(tensor.size(0));
    auto image_width = tensor.size(2);
    auto image_height = tensor.size(1);

    auto image_tensor = tensor.detach()
                              .permute({1, 2, 0})
                              .mul_(255)
                              .to(torch::kUInt8)
                              .cpu()
                              .contiguous();

    juce::Image image(image_format, image_width, image_height, true);
    juce::Image::BitmapData image_data(image, juce::Image::BitmapData::writeOnly);

    std::memcpy(image_data.data, image_tensor.data_ptr(), image_tensor.numel() * sizeof(torch::kUInt8));
    return image;
}

with the format <--> channels conversion functions:

juce::Image::PixelFormat num_channels_to_format(int num_channels) {
    switch(num_channels) {
        case 1:
            return juce::Image::SingleChannel;
        case 3:
            return juce::Image::RGB;
        case 4:
            return juce::Image::ARGB;
        default:
            throw std::runtime_error("Invalid number of channels");
    }
}

int format_to_num_channels(juce::Image::PixelFormat format) {
    switch(format) {
        case juce::Image::SingleChannel:
            return 1;
        case juce::Image::RGB:
            return 3;
        case juce::Image::ARGB:
            return 4;
        default:
            throw std::runtime_error("Invalid image format");
    }
}

Note that I just did a quick test with an RGB jpg file and did not check every possible format.

from pytorch-cpp.

mfl28 avatar mfl28 commented on May 11, 2024

Hi!
Can you tell us where you are getting errors? In your torch::tensor --> cv::Mat function or in the cv::Mat --> juce::Image function? Which are these errors?

from pytorch-cpp.

 avatar commented on May 11, 2024

Is make crash in middle of cv:mat to juce image. I dont want OpenCV at all.

from pytorch-cpp.

 avatar commented on May 11, 2024

Amazing will make test and tell if compile no crash.

from pytorch-cpp.

prabhuomkar avatar prabhuomkar commented on May 11, 2024

@paklau99988 any update?

from pytorch-cpp.

mfl28 avatar mfl28 commented on May 11, 2024

Closing this for now, will be reopened if there are further questions.

from pytorch-cpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.