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.5.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.5-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

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

Hashes for segment_anything_onnx-0.0.5.tar.gz
Algorithm Hash digest
SHA256 951f991a3c72c23465625ef1aae517f2f4df03101961b9417ebea6c376068493
MD5 1e9add83fafdb53da0ad7add27a5373b
BLAKE2b-256 af8fbddd0546c7049d2be683d28f2909b6ff3d9629ccb0391224ff13dbcec219

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for segment_anything_onnx-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ecff8c9d44571d0708ad6b754cec319150aff7ef80d210e02c2545598b500ad8
MD5 95eb57b8b4515fa8923025f01fdd2550
BLAKE2b-256 f5cc195c611adc24e5b39f36783f0ef40334423d5461a8e5c0eafa6806d13d7b

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