Run Segment Anything with ONNX models
Project description
segment-anything-onnx
Use ONNX models for Segment Anything inference.
Special thanks to:
- Viet-Anh NGUYEN for creating the SAM Exporter libraries from which these files are derived.
- The Segment Anything team at Meta AI Research
Usage from Source
- Clone Segment Anything ONNX from Github
git clone git@github.com:whatstyle-ai/segment-anything-onnx.git cd segment-anything-onnx pip install -e .
- Use the SAM Exporter to generate the ONNX models
- Copy the ONNX models to the segment-anything-onnx/models directory
- Predict some masks
cd segment-anything-onnx ./demo.sh
Usage from pip install
- Use the SAM Exporter to generate the ONNX models, or obtain the ONNX models from another source
- Copy the ONNX models to a "models" directory, such as:
models/sam_vit_l_0b3195.encoder.onnx models/sam_vit_l_0b3195.decoder.onnx - Install Segment Anything ONNX using pip:
pip install segment-anything-onnx
- Predict a mask:
import cv2 import urllib.request import numpy as np from segment_anything_onnx.inference import predict_masks def load_image(uri): if( uri.startswith('https://') or uri.startswith('http://') ): req = urllib.request.urlopen(uri) arr = np.asarray(bytearray(req.read()), dtype=np.uint8) img = cv2.imdecode(arr, -1) # 'Load it as it is' return img else: return cv2.imread(uri) # uri is just a local file path encoder_model_path = './models/sam_vit_l_0b3195.encoder.onnx' decoder_model_path = './models/sam_vit_l_0b3195.decoder.onnx' image = load_image( 'https://raw.githubusercontent.com/whatstyle-ai/segment-anything-onnx/main/examples/laura.jpg' ) prompt = [ { 'type': 'point', 'data': [1750, 300], 'label': 0 }, { 'type': 'rectangle', 'data': [611, 655, 2712, 4500] } ] options = { 'show': True, 'output': './output/laura-L.png' } predict_masks( encoder_model_path, decoder_model_path, image, prompt, options )
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file segment_anything_onnx-0.0.5.tar.gz.
File metadata
- Download URL: segment_anything_onnx-0.0.5.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
951f991a3c72c23465625ef1aae517f2f4df03101961b9417ebea6c376068493
|
|
| MD5 |
1e9add83fafdb53da0ad7add27a5373b
|
|
| BLAKE2b-256 |
af8fbddd0546c7049d2be683d28f2909b6ff3d9629ccb0391224ff13dbcec219
|
File details
Details for the file segment_anything_onnx-0.0.5-py3-none-any.whl.
File metadata
- Download URL: segment_anything_onnx-0.0.5-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecff8c9d44571d0708ad6b754cec319150aff7ef80d210e02c2545598b500ad8
|
|
| MD5 |
95eb57b8b4515fa8923025f01fdd2550
|
|
| BLAKE2b-256 |
f5cc195c611adc24e5b39f36783f0ef40334423d5461a8e5c0eafa6806d13d7b
|