imgly / background-removal-js

Remove backgrounds from images directly in the browser environment with ease and no additional costs or privacy concerns. Explore an interactive demo.
https://img.ly/showcases/cesdk/web/background-removal/web
GNU Affero General Public License v3.0
5.7k stars 357 forks source link

Suggestion to the library so that it can be executed in a worker context #32

Open mastrix opened 1 year ago

mastrix commented 1 year ago

Hi guys, First of all, it's a really nice lib that works like a charm :)

I have a suggestion to add/modify a few lines in utils.ts, that will allow to use this lib in worker.

https://github.com/imgly/background-removal-js/blob/main/src/utils.ts#L95-L101 Original

function ensureAbsoluteURL(url: string): string {
  if (isAbsoluteURL(url)) {
    return url;
  } else {
    return new URL(url, window.location.href).href;
  }
}

Option 1

function ensureAbsoluteURL(url: string): string {
  if (isAbsoluteURL(url)) {
    return url;
  } else {
    let executionContextUrl;
    if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
      executionContextUrl = new URL(url, self.location.href).href;
    } else {
       executionContextUrl new URL(url, window.location.href).href;
    }
    return executionContextUrl;
  }
}

Option 2

function ensureAbsoluteURL(url: string): string {
  if (isAbsoluteURL(url)) {
    return url;
  } else {
    return new URL(url, self.location.href).href;
  }
}
jamesopti commented 11 months ago

Came here looking for this! Happy to submit the pull request if you're onboard with it @DanielHauschildt .

joaomendoncaa commented 7 months ago

Stumbled upon the same problem.

I tried a lot of things (especially using the above mentioned patch, which makes total sence, and building/installing the package myself), but nothing worked.

I'm also simply trying to run removeBackground() inside a node worker. Any success on your guy's end?

PS: While writing this I noticed I completely disregarded the problem being with the runtime itself. I'm running bun workers and not nodejs itself. Will have to investigate more, but will appreciate any feedback on the issue nonetheless.