npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@codait/max-image-segmenter

v0.3.0

Published

Identify objects in an image, additionally assigning each pixel of the image to a particular object.

Downloads

78

Readme

MAX for TensorFlow.js: Image Segmenter

This is a TensorFlow.js port of the MAX Image Segmenter pre-trained model. The Image Segmenter was trained to identify objects in an image and assigns each pixel of the image to a particular object.

Install

Browser

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@codait/max-image-segmenter"></script>

By default when the @codait/max-image-segmenter JavaScript module is loaded, the model is automatically loaded and the cache warmed up (by running inference against an all zero input). To change this default behavior (and prevent the model from being automatically initialized) set attribute data-init-model="false" in the script tag for the @codait/max-image-segmenter.

Node.js

npm install --save @codait/max-image-segmenter

Usage

The complete examples for browser and Node.js environments are in the /examples directory.

Browser

Note: When loaded in a browser, the global variable imageSegmenter will be available to access the API.

let image = document.getElementById('my-image')

imageSegmenter
  .predict(image)
  .then(prediction => {
    console.log(prediction.segmentationMap)
    console.log(prediction.objectsDetected)
  })

Node.js

const { predict } = require('@codait/max-image-segmenter')
const { read, MIME_PNG } = require('jimp')
const { createCanvas, loadImage } = require('canvas')

const createCanvasElement = function (imageInput) {
  return new Promise(async (resolve, reject) => {
    const img = await loadImage(imageInput)
    let canvas = createCanvas(img.width, img.height)
    let ctx = canvas.getContext('2d')
    await ctx.drawImage(img, 0, 0)
    resolve(canvas)
  })
}

const imagePath = `file://${ __dirname}/my-image.jpg`

read(imagePath)
  .then(imageData => imageData.scaleToFit(512, 512).getBufferAsync(MIME_PNG))
  .then(imageBuffer => createCanvasElement(imageBuffer))
  .then(imageElement => predict(imageElement))
  .then(prediction => {
    // console.log(prediction.segmentationMap)
    console.log(prediction.objectsDetected)
  })

API

  • loadModel(init)

    Loads the model files.

    init - if true, a prediction will be triggered using an all zero Tensor to warm up the model (helps increase speed of subsequent predictions when running in a browser). Default is true.

    Returns the TensorFlow.js model.

  • processInput(image)

    Processes the input image to the shape and format expected by the model. The image is resized and converted to a 4D Tensor.

    image - an instance of HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement.

    Returns a 4D Tensor that can be passed to the model.

  • runInference(inputTensor)

    Runs inference on the input Tensor passed. The output is 2D Tensor with an object ID assigned to each index of the input Tensor.

    inputTensor - a 4D Tensor representing an ImageData

    Returns the inference results.

  • processOutput(inferenceResults)

    Processes the inference output replacing the output Tensor with an 2D array.

    inferenceResults - the model output from running inference.

    Returns an object containing

    • segmentationMap: a 2D array with an object ID assigned to each pixel of the image
    • objectsDetected: an array of objects detected in the image
    • imageSize: an object with the width and height of the resized image (corresponds to the size of the segmentationMap)
  • predict(image)

    Loads the model (if not loaded), processes the input image, runs inference, processes the inference output, and returns a prediction object. This is a convenience function to avoid having to call each of the functions (loadModel, processInput, runInference, processOutput) individually.

    image - an instance of HTMLImageElement, HTMLCanvasElement, or HTMLVideoElement.

    Returns an object containing

    • segmentationMap: a 2D array with an object ID assigned to each pixel of the image
    • objectsDetected: an array of objects detected in the image
    • imageSize: an object with the width and height of the resized image (corresponds to the size of the segmentationMap)
  • labelsMap()

    An array of object labels where the label's index corresponds to its ID. It can be used to map the IDs in the segmentationMap to its corresponding label.

  • colorsMap()

    An array of RGB color values that can be used to map each object to a specific color.

  • version

    Returns the version

Model

The model assets produced by converting the pre-trained model to the TensorFlow.js format can be found in the /model directory.

Resources

License

Apache-2.0