oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.8k stars 2.56k forks source link

segmentation fault running @imgly/background-removal-node #6143

Closed gtrabanco closed 6 months ago

gtrabanco commented 9 months ago

What version of Bun is running?

1.0.4+387f1260c9dc0cea667b44ec0152fff0cd4def25

What platform is your computer?

Darwin 21.6.0 x86_64 i386

What steps can reproduce the bug?

mkdir bun-bg-removal && cd $_
bun init
bun add --exact @imgly/background-removal-node sharp@0.32.6 # Avoid version mismatch between background-removal and sharp

Code of index.ts

import { removeBackground } from '@imgly/background-removal-node';

const url = 'https://balonmano.isquad.es/images/afiliacion_clubs/2898/square_35723432687275366a39.jpg';

try {
  const imgBg = await removeBackground(url, {
    model: 'medium',
    progress: (key: any, current: any, total: any) => {
      console.log(`Downloading ${key}: ${current} of ${total}`);
    }
  });

  Bun.write('imgBg.png', imgBg);
} catch (error) {
  console.log(error);
}

Sharp

I installed sharp because I though @imgly/background-removal-node was only accepting pngs so I tried also with this code in a new file called index.js:

import { removeBackground } from "@imgly/background-removal-node";
import sharp from "sharp";

const url =
  "https://balonmano.isquad.es/images/afiliacion_clubs/2898/square_35723432687275366a39.jpg";

try {
  const data = await fetch(url).then((response) => response.arrayBuffer());
  const png = await sharp(data).png().toBuffer();
  const imgBg = await removeBackground(png, {
    model: "medium", // default but I want to be explicity with this
    progress: (key, current, total) => {
      console.log(`Downloading ${key}: ${current} of ${total}`);
    },
  });

  sharp(imgBg).toFile("output.png");
} catch (error) {
  console.log(error);
}

What is the expected behavior?

Create imgBg.png file with, at least, a try to remove background of the image or any issue with remove background package. If all goes right and model could remove the image background should be a transparent background png with the shield shape.

What do you see instead?

Downloading fetch:/models/medium: 16384 of 88188479
Downloading fetch:/models/medium: 88188479 of 88188479
[1]    69528 segmentation fault  bun index.ts

Additional information

This package requires onnyxruntime-node which has opened issues due it does not work currently on bun:

My package of using the code can be wrong but if thats the case I think segfault is not an expected error BTW. If segfault can be an expected error then this issue I opened in the package repository should be tracked as well:

gtrabanco commented 9 months ago

Same error with fixed code as expected.

Here the sample code that works with NodeJS (sharp package is not necessary):

import { removeBackground } from "@imgly/background-removal-node";

const url =
  "https://balonmano.isquad.es/images/afiliacion_clubs/2898/square_35723432687275366a39.jpg";

try {
  const imgBg = await removeBackground(url, {
    model: "medium", // default but I want to be explicity with this
    progress: (key, current, total) => {
      console.log(`Downloading ${key}: ${current} of ${total}`);
    },
  });

  sharp(await imgBg.arrayBuffer()).toFile("output.png");
} catch (error) {
  console.log(error);
}

Output with NodeJS

$ node index.mjs 
Downloading fetch:/models/medium: 88188479 of 88188479
Downloading compute:inference: 0 of 1

Output with Bun

$ bun index.mjs   
Downloading fetch:/models/medium: 16384 of 88188479
Downloading fetch:/models/medium: 88188479 of 88188479
[1]    78105 segmentation fault  bun index.mjs
krk commented 8 months ago

Segfault is at Napi::InstanceWrap<InferenceSessionWrap>::InstanceMethodCallbackWrapper(napi_env__*, napi_callback_info__*) when calling https://github.com/microsoft/onnxruntime/blob/785e2b1eaec2b2e69267cc9320f7749315c0e686/js/node/src/inference_session_wrap.cc#L51

gtrabanco commented 7 months ago

Same error with transformers.js due onnyxruntime with Bun 1.0.11+84414f8fe

sroussey commented 6 months ago

@imgly/background-removal-node examples here now work as expected for me.

The transformers.js one works for me as well on my fork. Hopefully fixed will move upstream soon.

gtrabanco commented 6 months ago

Yes, it is working now with Bun 1.0.21+837cbd60d. I am closing this issue :)

Thanks @sroussey