Skip to content

Commit

Permalink
- fix image pixel format messing up ONNX inference on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-hart committed Nov 8, 2024
1 parent 56eb7f9 commit 17b70d1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion currentGitHash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8448dfb1de3d35b54e3738b7663f60ea454ba729
30ce85b09b2317bee09226450daf778890865c2e
2 changes: 1 addition & 1 deletion hi_backend/backend/currentGit.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define PREVIOUS_HISE_COMMIT "8448dfb1de3d35b54e3738b7663f60ea454ba729"
#define PREVIOUS_HISE_COMMIT "30ce85b09b2317bee09226450daf778890865c2e"
7 changes: 5 additions & 2 deletions hi_scripting/scripting/api/ScriptingApiObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7626,13 +7626,16 @@ var ScriptingObjects::ScriptFFT::getSpectrum2DParameters() const
Image ScriptingObjects::ScriptFFT::getRescaledAndRotatedSpectrum(bool getOutput, int numFreqPixels, int numTimePixels)
{
auto thisImg = getSpectrum(getOutput).rescaled(numFreqPixels, numTimePixels, Graphics::ResamplingQuality::highResamplingQuality);
Image rotated(Image::PixelFormat::RGB, thisImg.getHeight(), thisImg.getWidth(), false);
Image rotated(Image::PixelFormat::ARGB, thisImg.getHeight(), thisImg.getWidth(), false);
Image::BitmapData r(rotated, Image::BitmapData::writeOnly);

for(int y = 0; y < rotated.getHeight(); y++)
{
for(int x = 0; x < rotated.getWidth(); x++)
rotated.setPixelAt(x, y, thisImg.getPixelAt(rotated.getHeight() - y - 1, x));
{
auto p = thisImg.getPixelAt(rotated.getHeight() - y - 1, x);
rotated.setPixelAt(x, y, p.withAlpha(1.0f));
}
}

return rotated;
Expand Down
8 changes: 8 additions & 0 deletions hi_tools/hi_neural/onnx_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ ONNXLoader::ONNXLoader(const String& rootDir):
{
auto dllFile = File(rootDir);

dllFile = dllFile.getChildFile("Builds/VisualStudio2022/x64/Debug/Dynamic Library");

#if JUCE_WINDOWS
dllFile = dllFile.getChildFile("onnx_hise_library.dll");
#elif JUCE_MAC
Expand Down Expand Up @@ -85,6 +87,12 @@ bool ONNXLoader::run(const Image& img, std::vector<float>& outputValues, bool is
{
if(auto f = getFunction<run_f>("runModel"))
{
if(img.getFormat() != Image::ARGB)
{
ok = Result::fail("The image must have ARGB pixel format to be consistent between Windows / macOS");
return false;
}

MemoryOutputStream mos;
PNGImageFormat().writeImageToStream(img, mos);
mos.flush();
Expand Down
16 changes: 11 additions & 5 deletions tools/onnx_lib/Source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ struct ONNXRuntime::Pimpl
Ort::Value input_tensor = Ort::Value::CreateTensor<float>(
memory_info, input_data.data(), input_data.size(), input_shape.data(), input_shape.size());

auto copy = image;

Image::BitmapData bp(copy, 0, 0, image.getWidth(), image.getHeight(), Image::BitmapData::ReadWriteMode::readOnly);
Image::BitmapData bp(image, 0, 0, image.getWidth(), image.getHeight());

auto v = input_data.data();

Expand All @@ -109,7 +107,10 @@ struct ONNXRuntime::Pimpl
for(int y = 0; y < bp.height; y++)
{
for(int x = 0; x < bp.width; x++)
*v++ = bp.getPixelColour(x, y).getBrightness();
{
auto c = bp.getPixelColour(x, y);
*v++ = c.getBrightness();
}
}
}

Expand Down Expand Up @@ -316,7 +317,12 @@ struct Data
{
outputValues.resize(numOutputs);
auto image = ImageFileFormat::loadFrom(imageData, numBytes);
ok = rt.run(image, outputValues, isGreyScale);

if(image.getFormat() == Image::PixelFormat::ARGB)
ok = rt.run(image, outputValues, isGreyScale);
else
ok = Result::fail("Image must have pixel format ARGB");

return ok.wasOk();
}

Expand Down

0 comments on commit 17b70d1

Please sign in to comment.