atlj / react-native-background-remover

A React Native lib that removes background from images
https://www.npmjs.com/package/react-native-background-remover
MIT License
33 stars 4 forks source link

Failed to remove background #2

Closed kuldip241995 closed 1 month ago

kuldip241995 commented 1 month ago

When I have run your react-native-background-remover-main GitHub repo example and try to check whether it was working fine or not, so

async function removeSelectionBackground() { if (!imageURI) { return; }

setIsLoading(true);

try {
  const backgroundRemovedImageURI = await removeBackground(imageURI);
  setImageURI(backgroundRemovedImageURI);
} catch (error) {
  console.error('Failed to remove background', error);
} finally {
  setIsLoading(false);
}

}

in this function catch (error) is executed and does not get any output, kindly check and provide your solution

atlj commented 1 month ago

Hey, which device do you use?

kuldip241995 commented 1 month ago

Hey, which device do you use?

Hello, iPhone 15 and iOS 17.0

atlj commented 1 month ago

And just to make sure, is it a simulator or a real device?

kuldip241995 commented 1 month ago

And just to make sure, is it a simulator or a real device?

As of now, I'm using the simulator, I have downloaded your GitHub repo project and setup using npm install && pod install then start the npm server and run the example project and pick any image from image picker and getting error

atlj commented 1 month ago

I think there was a problem on iOS with simulators, I have to check it again and update the README (and possibly the code to emit a warning). Is it possible for you to try it with a real device?

kuldip241995 commented 1 month ago

I think there was a problem on iOS with simulators, I have to check it again and update the README (and possibly the code to emit a warning). Is it possible for you to try it with a real device?

Let me check in the real device as well and get back to you, thank you

kuldip241995 commented 1 month ago

@atlj I have tried with a real device then removing the background is working fine, so what needs to be done if wanted to run in the simulator as well??

atlj commented 1 month ago

I was researching this back then and it turned out the selfie segmentation API (the one I use to remove background) doesn't work on the Simulator. One workaround we can implement is to return the original image when people call this library and warn them. One other option is to implement MLKit for iOS but honestly, it has worse results than selfie segmentation.

So in summary, this is an OS restriction, not a library code one.

kuldip241995 commented 1 month ago

I was researching this back then and it turned out the selfie segmentation API (the one I use to remove background) doesn't work on the Simulator. One workaround we can implement is to return the original image when people call this library and warn them. One other option is to implement MLKit for iOS but honestly, it has worse results than selfie segmentation.

So in summary, this is an OS restriction, not a library code one.

Will continue with the real device, meanwhile, if you find any solution then please commit your code.

Thank you so much for your response

atlj commented 1 month ago

Sure stuff! And I will close this issue once I update the README (and possibly add the warning)

kuldip241995 commented 1 month ago

Sure stuff! And I will close this issue once I update the README (and possibly add the warning)

Okay great

kuldip241995 commented 1 month ago

Hello @atlj, I Also checked with you to see if we can add functionality in this library if somebody passes backgroundColor props colour = 'red' then it will applied on the image URI red colour else remove the background of the image, something is possible?

atlj commented 1 month ago

Hey @kuldip241995 that's definitely possible to do on native side. And as an alternative you can use a Skia canvas or a regular React Native view to put a view at the back of the image. And then you can use Skia's own screenshotting functionality or react-native-view-shot to get the screenshot of that area. Downside is the view shotting mechanism is a bit flaky sometimes but this would give more flexibility so you could put any react native you want at the back of the image.

kuldip241995 commented 1 month ago

Hey @kuldip241995 that's definitely possible to do on native side. And as an alternative you can use a Skia canvas or a regular React Native view to put a view at the back of the image. And then you can use Skia's own screenshotting functionality or react-native-view-shot to get the screenshot of that area. Downside is the view shotting mechanism is a bit flaky sometimes but this would give more flexibility so you could put any react native you want at the back of the image.

Okay, Thank you so much for your suggestions, Appreciated!

atlj commented 1 month ago

f8af627 and 1aaf0bb adds a warning and documents this behavior.