This is a Next.js application that performs image segmentation using Meta's Segment Anything Model V2 (SAM2) and onnxruntime-web. All the processing is done on the client side.
Demo at sam2-seven.vercel.app
small_next-sam.mov
- Utilizes Meta's SAM2 model for segmentation
- onnxruntime-web for model inference
- webgpu accelerated if GPU available and supported by browser, cpu if not
- Model storage using OPFS (not working in Safari)
- Image upload
- Mask decoding based on point prompt
- Cropping
- Tested on macOS with Edge (webgpu, cpu), Chrome (webgpu, cpu), Firefox (cpu only), Safari (cpu only)
- Fails on iOS (17, iPhone SE), not sure why
Clone the repository:
git clone https://github.com/geronimi73/next-sam
cd next-sam
npm install
npm run dev
Open your browser and visit http://localhost:3000
- Upload an image or use the default image.
- Click the "Encode image" button to start encoding the image.
- Once the encoding is complete, click on the image to decode masks.
- Click the "Crop" button to crop the image using the decoded mask.
Last but not least!