segment-anything icon indicating copy to clipboard operation
segment-anything copied to clipboard

Mobile integration

Open rkz98 opened this issue 2 years ago • 3 comments

Is there any mobile integration for AR stuff? (React-Native, Kotlin or Swift)

rkz98 avatar Apr 22 '23 04:04 rkz98

I've tried to run onnx model on react native, it's still quite slow

hungtooc avatar Apr 22 '23 11:04 hungtooc

@hungtooc may you share the implementation code? I'd like to try it out.

rkz98 avatar Apr 24 '23 01:04 rkz98

@rkz98 here is react native code:

import { InferenceSession, Tensor, } from "onnxruntime-react-native";
// const IMAGE_PATH = "/assets/data/beach.jpg";
const IMAGE_EMBEDDING = "/assets/data/beach_embedding.npy";

const App = () => {
  const [model, setModel] = useState<InferenceSession | null>(null); // ONNX model
  const [tensor, setTensor] = useState<Tensor | null>(null); // Image embedding tensor

  useEffect(() => {
    // Initialize the ONNX model
    const initModel = async () => {
      try {
        const model = await InferenceSession.create('file://' + RNFS.DocumentDirectoryPath + '/sam.onnx');
        console.info(model.inputNames)
        setModel(model);
      } catch (e) {
        console.log(e);
      }
    };
    initModel();

    // Load the Segment Anything pre-computed embedding
    Promise.resolve(loadNpyTensor(IMAGE_EMBEDDING, "float32")).then((embedding) => {
      console.info("embedding.dims", embedding.dims)
      setTensor(embedding)
    });
  }, []);
const loadNpyTensor = async (tensorFile: string, dType: string) => {
    // notice I created fake data
    const data = new Float32Array(256 * 64 * 64);
    const tensor = new Tensor("float32", data, [1, 256, 64, 64]);
    return tensor;
  };
## end of init

useEffect(() => {
    console.info("runONNX once")
    if (model && tensor)
      runONNX(model, [{
        clickType: 1,
        x: 150,
        y: 150
      }], tensor, {
        samScale: 1.28,
        height: 640,
        width: 480
      });
  }, [state]);

}

hungtooc avatar Apr 24 '23 02:04 hungtooc

any way to also generate the embeddings on mobile?

eikaramba avatar Jun 16 '23 14:06 eikaramba

I am trying to integrate it on flutter plugin. is it possible?

creativekdev avatar Aug 21 '23 12:08 creativekdev

You can find the converter for the encoding part to the onnx format in the repository Segment Anything CPP Wrapper.

Here is the Segment Anything CPP Wrapper for macOS. This code is originated from Segment Anything CPP Wrapper and implemented on macOS app RectLabel. We customized the original code so that getMask() uses the previous mask result called as low_res_logits and retain the previous mask array for undo/redo actions.

We hope this macOS version would help to develop the iOS version. Please let us know your opinion.

sam_polygon

ryouchinsa avatar Sep 02 '23 16:09 ryouchinsa

Improved "Create polygon using SAM" feature so that you can label pixels using the pixels option. You can label Segment Anything 1 Billion (SA-1B) like dataset by yourself.

sam-pixels

ryouchinsa avatar Oct 23 '23 17:10 ryouchinsa