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

How to use in an html5+JS project? #74

Open aoteman44 opened 10 months ago

aoteman44 commented 10 months ago

I didn't find background-removal.js in the project and don't know how to use it

collinph commented 3 months ago

Any updates on this?

SaBenBurra commented 2 months ago

any update?

hhtuan206 commented 1 month ago

You can using cdn of jsdelivr https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.5.5/+esm Here is script using

<script type="module">
        import { removeBackground, preload } from 'https://cdn.jsdelivr.net/npm/@imgly/background-removal@1.5.5/+esm'

        const config = {
            // path contain assert, you can take all from here https://staticimgly.com/@imgly/background-removal-data/YOUR_PACKAGE_VERSION/package.tgz
            publicPath: 'http://127.0.0.1:5500/dist/', 
        };
       // load onnx model first
        preload(config).then(() => {
            console.log("Asset preloading succeeded")
        })
        let image = document.getElementById('resultImage') // image tag
        document.getElementById('imageInput').addEventListener('change', async function (event) {
            const file = event.target.files[0];
            if (file) {
                try {
                    // Remove background
                    removeBackground(file,).then((blob) => {
                        const url = URL.createObjectURL(blob);
                        // Display the result
                        image.src = url;
                    });

                } catch (error) {
                    console.error('Error removing background:', error);
                }
            }
        });
</script>