pak
September 1, 2020, 8:23am
1
Hello,
I is use Libtorch for process image, OpenCV very large so try avoid.
This i try make use:
// Copyright 2020-present pytorch-cpp Authors
#include "image_io.h"
#include <torch/torch.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"
namespace image_io {
namespace {
void norm_ip(torch::Tensor img, double min, double max) {
img.clamp_(min, max);
img.sub_(min).div_(max - min + 1e-5);
}
This file has been truncated. show original
Not able to convert torch::Tensor to juce::image.
I want to read a Video file, split into images on the fly and apply Deep Learning algorithm on image in Libtorch, then turn it back to image JUCE can understand. Anyone is have example make like this?
Thanks you,
pak
September 1, 2020, 12:03pm
2
Really help is needed. I have this, first to oepncv and then to juce::image (still crash). Any better way?
cv::Mat toOpenCV(at::Tensor out_tensor, int h, int w, int c)
{
out_tensor = out_tensor.squeeze().permute({1, 2, 0});
cout << "out_tensor " << out_tensor << "\n";
out_tensor = out_tensor.mul(255).clamp(0, 255).to(torch::kU8);
// out_tensor = out_tensor.to(torch::kCPU);
cout << "out_tensor " << out_tensor << "\n";
cv::Mat resultImg(h, w, CV_8UC3);
// cv::Mat resultImg(h, w, CV_8UC1);
std::memcpy((void *) resultImg.data, out_tensor.data_ptr(), sizeof(torch::kU8) * out_tensor.numel());
return resultImg;
}
juce::Image toJUCEImage(cv::Mat original,int h, int w, int c)
{
juce::Image image(juce::Image::RGB, h, w, false);
const size_t number_of_bytes_to_copy = 3 * w; // times 3 since each pixel contains 3 bytes (RGB)
juce::Image::BitmapData bitmap_data(image, 0, 0, h,w, juce::Image::BitmapData::ReadWriteMode::writeOnly);
for (int row_index = 0; row_index < h; row_index ++)
{
uint8_t * src_ptr = original.ptr(row_index);
uint8_t * dst_ptr = bitmap_data.getLinePointer(row_index);
std::memcpy(dst_ptr, src_ptr, number_of_bytes_to_copy);
}
return image;
}
pak
September 3, 2020, 11:14am
3
Is have answer here, if anyone else is needed this:
opened 12:12PM - 01 Sep 20 UTC
Is your feature request related to a problem? Please describe.
Hello,
I is use Libtorch for process image, OpenCV very large so try...
enhancement