npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-selfie-ai-background-remover

v1.0.3

Published

A React component that removes the background from images using TensorFlow.js and the MediaPipe Selfie Segmentation model.

Downloads

23

Readme

react-selfie-ai-background-remover

react-selfie-ai-background-remover is a React component that automatically removes the background from images using TensorFlow.js and the MediaPipe Selfie Segmentation model. It provides an easy-to-use interface for users to upload an image, process it to remove the background, and download the result.

Demo

The live demo for this component can be accessed at:

Table of Contents

Features

  • Automatic Background Removal: Utilizes the MediaPipe Selfie Segmentation model to remove backgrounds from images containing a single person.
  • Customizable Buttons: Allows you to provide custom upload, download, mask download, and clear buttons.
  • Flexible Styling: Offers CSS class names for extensive styling customization.
  • Configurable Properties: Supports various properties to control behavior and appearance.

Installation

Install the package via npm:

npm install react-selfie-ai-background-remover

or via yarn:

yarn add react-selfie-ai-background-remover

Usage

Here's how to use the BackgroundRemover component and OnProcessCompleteResult, BackgroundRemoverProps, ShowButtonsProps interfaces in your React application:

import React from 'react';
import BackgroundRemover, { OnProcessCompleteResult, BackgroundRemoverProps, ShowButtonsProps } from 'react-selfie-ai-background-remover';

function App() {
  const handleProcessComplete = ({ originalImage, processedImage, maskImage }: OnProcessCompleteResult) => {
    console.group('Process Complete')
    console.dir({ originalImage, processedImage, maskImage })
  };

  const handleError = (error: unknown) => {
    console.error('Error: ', error);
  };


  return (
    <div className="wrapper">
      <h1>Example of usage React Selfie Background Remover</h1>
      <BackgroundRemover
        onProcessComplete={handleProcessComplete}
        onError={handleError}
        allowDownload={true}
        uploadButton={<button className="custom-class-name-1">Custom Upload Image</button>}
        downloadButton={<button className="custom-class-name-2">Custom Download Image</button>}
        downloadMaskButton={<button className="custom-class-name-3">Custom Download Mask</button>}
        clearButton={<button className="custom-class-name-4">Custom Clear</button>}
      />
    </div>
  );
}

export default App;

Properties

BackgroundRemoverProps is the interface for the properties of the BackgroundRemover component.

Basic Properties

| Property | Type | Default | Description | |-----------------------|--------------------------------------------------------------------------------------|-----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | width | number | undefined | Width of the processed image displayed. | | height | number | undefined | Height of the processed image displayed. | | onProcessComplete | (result: { originalImage: string; processedImage: string; maskImage: string; }) => void | undefined | Callback function called when processing is complete. Receives an object containing URLs to the original image, processed image, and mask image. Implements OnProcessCompleteResult interface. | | onError | (error: any) => void | undefined | Callback function called when an error occurs during processing. Receives the error object. | | allowDownload | boolean | false | If true, enables the download buttons for the processed image and mask image. | | style | React.CSSProperties | {} | Inline styles applied to the root component. | | className | string | '' | Additional CSS class applied to the root component. |

Button Customization

| Property | Type | Default | Description | |------------------------|---------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | uploadButton | ReactNode | null | Custom element to replace the default upload button. If not provided, a default button with the text "Upload Image" is used. | | downloadButton | ReactNode | null | Custom element to replace the default download button for the processed image. If not provided, a default button with the text "Download Image" is used. | | downloadMaskButton | ReactNode | null | Custom element to replace the default download button for the mask image. If not provided, a default button with the text "Download Mask" is used. | | clearButton | ReactNode | null | Custom element to replace the default clear button to reset the component. If not provided, a default button with the text "Clear Image" is used. | | showButtons | object | { upload: true, download: true, downloadMask: true, clear: true } | Object to control the visibility of the buttons. Set upload, download, downloadMask, or clear to false to hide the respective button. Implements ShowButtonsProps interface. |

Processing Options

| Property | Type | Default | Description | |------------------|-----------|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------| | hasSmoothEdges | boolean | true | If true, applies smoothing to the edges of the processed image for a better visual result. | | isInverted | boolean | false | If true, inverts the mask, which can be useful if you want to remove the foreground instead of the background. | | imageName | string | 'processed-image' | The filename used when downloading the processed image. | | maskImageName | string | 'mask-image' | The filename used when downloading the mask image. |

Styling Customization

CSS Classes

You can customize the styles of the component by targeting the following CSS classes:

  • .ai-bg-remover: Root element of the component.
  • .ai-bg-remover__file-input: The hidden file input element.
  • .ai-bg-remover__upload-button: The upload button wrapper.
  • .ai-bg-remover__canvas: The hidden canvas element used for processing.
  • .ai-bg-remover__image: The processed image element.
  • .ai-bg-remover__download-buttons: Wrapper for the download buttons.
  • .ai-bg-remover__download-button: The download processed image button.
  • .ai-bg-remover__download-mask-button: The download mask image button.
  • .ai-bg-remover__clear-button: The clear image button.

Events

onProcessComplete

Called when the image processing is complete.

const handleProcessComplete = ({ originalImage, processedImage, maskImage }) => {
  console.group('Process Complete');
  console.dir({ originalImage, processedImage, maskImage });
};

<BackgroundRemover
  onProcessComplete={handleProcessComplete}
/>

onError

Called when an error occurs during image processing.

const handleError = (error) => {
  console.error('Error: ', error);
};

<BackgroundRemover
  onError={handleError}
/>

Dependencies

  • TensorFlow.js: Uses @tensorflow/tfjs and @tensorflow-models/body-segmentation for processing images.
  • React: Requires React 16.8 or higher for hooks support.

License

This project is licensed under the MIT License.

Author

Contribution

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Support

If you encounter any issues or have questions, feel free to open an issue on the GitHub repository.

Acknowledgements

Links


Feel free to customize the component further and adapt it to your specific needs!