Skip to main content

Run Segment Anything with ONNX models

Project description

segment-anything-onnx

Use ONNX models for Segment Anything inference.

Special thanks to:

Usage from Source

  1. Clone Segment Anything ONNX from Github
    git clone git@github.com:whatstyle-ai/segment-anything-onnx.git
    cd segment-anything-onnx
    pip install -e .
    
  2. Use the SAM Exporter to generate the ONNX models
  3. Copy the ONNX models to the segment-anything-onnx/models directory
  4. Predict some masks
    cd segment-anything-onnx
    ./demo.sh
    

Usage from pip install

  1. Use the SAM Exporter to generate the ONNX models, or obtain the ONNX models from another source
  2. Copy the ONNX models to a "models" directory, such as:
    models/sam_vit_l_0b3195.encoder.onnx
    models/sam_vit_l_0b3195.decoder.onnx
    
  3. Install Segment Anything ONNX using pip:
    pip install segment-anything-onnx
    
  4. 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

segment_anything_onnx-0.0.4.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

segment_anything_onnx-0.0.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file segment_anything_onnx-0.0.4.tar.gz.

File metadata

  • Download URL: segment_anything_onnx-0.0.4.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

Hashes for segment_anything_onnx-0.0.4.tar.gz
Algorithm Hash digest
SHA256 cd967ebc1d94d4fe548198e87d9e993505d71e7d62b2a44ec08c21ae3571cff6
MD5 20cc96bfefbb298e6bb4016cfb5a790c
BLAKE2b-256 be4098b88d38778d0c5a3108971216b1a650318ce0e067b1594101e2d28e5616

See more details on using hashes here.

File details

Details for the file segment_anything_onnx-0.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for segment_anything_onnx-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dfc1b27caaf64398ca2f9b7e0382f2f63424bccc16475e8cbcd307a8dd68cbc6
MD5 bc4f0a8614c791f6f866422d1aca8729
BLAKE2b-256 9ca3bf6b35d2aa2730187507e04981842aef75e5dd6c12ea83e31c2ec154ced1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page