0.2.3 • Published 2 years ago

react-native-image-selfie-segmentation v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-native-image-selfie-segmentation

Uses MLKit Vision Selfie Segmentation to combine a selfie and background image.

Installation

npm install react-native-image-selfie-segmentation
cd ios && pod install

Usage

import { replaceBackground } from 'react-native-image-selfie-segmentation';

// ...

const response = await replaceBackground(inputImage, backgroundImage, 500);

Props

PropTypeDefinition
Input ImageStringRequired - File URI: The selfie image
Background ImageStringRequired - File URI: The background image
Max SizeNumberOptional - Default: 500. This will resize the input and background images to the required value.

Max Size Notes

  • The background image is resized to the max size value, then cropped from the center to fill the input image height/width.

  • On iOS Retina devices, Max Size is doubled, so a value of 250 with an input of 1000x800 will result in a 500x400 image. You can account for this by passing a value half of what is required.

Response

ResponseTypeDefinition
ImageStringA File URI of the new image

Example

const [image, setImage] = useState();
const [inputImage, setInputImageUri] = useState();
const [backgroundImage, setBackgroundImage] = useState();

// ... set the inputImageUri and backgroundImageUri
// ... check the example which uses react-native-image-picker

const onProcessImageHandler = async () => {
  if (inputImage && backgroundImage) {
    await replaceBackground(inputImage, backgroundImage, 400)
      .then((response) => {
        setImage(response);
      })
      .catch((error) => {
        console.log(error);
      });
  }
};

return (
  <>
    <TouchableOpacity onPress={onProcessImageHandler}>
      <Text>Process Image</Text>
    </TouchableOpacity>
    {image && (
      <Image source={{ uri: image }} />
    )}
  </>

);

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT