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 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-tensorflow

v0.3.2

Published

A React hook to use tensorflow.js easily

Downloads

21

Readme

use-tensorflow

A React hook for tensorflow.js to detect objects and poses easily:

Recognized output

import React, { useRef } from "react";
import { useObjects } from "use-tensorflow";    // This library :)
import { Container, Box } from './components';  // Find the implementation below

export default () => {
  const ref = useRef(null);
  const objects = useObjects(ref);
  return (
    <Container>
      <img ref={ref} src="/living-room.jpg" />
      {objects ? objects.map(({ left, top, width, height, label, score }) => (
        <Box
          left={left}
          top={top}
          width={width}
          height={height}
          label={label}
          color={score > 0.6 ? "blue" : "red"}
          score={score}
        />
      )) : 'Loading...'}
    </Container>
  );
};

API

Import one of the models:

// Available models right now
import { usePoses, useObjects } from 'use-tensorflow';

All the named imported models follow the same structure:

export default () => {
  const ref = useRef();
  const detected = use{Name}(ref, config);
  return ...;
};
  • ref: the reference for the image or video.
  • config: the configuration for the specific model in Tensorflow. See Coco SSD example.

There's also a default export useTensorflow, but it's experimental by now.

useObjects

Detect objects in the image, including the label, position and score:

import React, { useRef } from 'react';
import { useObjects } from 'use-tensorflow';

export default () => {
  const ref = useRef();
  const objects = useObjects(ref, { modelUrl: "/objects/model.json" });
  console.log(objects);
  return <img ref={ref} src="/living-room.jpg" />;
};

Output:

[
  {
    "label": "chair",
    "score": 0.8703018426895142,
    "left": 681,
    "top": 409,
    "width": 101,
    "height": 140
  },
  {
    "label": "couch",
    "score": 0.8127394318580627,
    "left": 160,
    "top": 335,
    "width": 308,
    "height": 140
  },
  ...
]

usePoses

Using poses as well:

import React, { useRef } from 'react';
import { usePoses } from 'use-tensorflow';

export default () => {
  const ref = useRef();
  const poses = usePoses(ref);
  console.log(poses);
  return <img ref={ref} src="/living-room.jpg" />;
};
[
  {
    "nose": {
      "label": "nose",
      "left": 762,
      "top": 299,
      "score": 0.22580526769161224
    },
    "leftEye": {
      "label": "leftEye",
      "left": 758,
      "top": 294,
      "score": 0.24668794870376587
    },
    ...
  },
  ...
]

All the keys are:

  • nose
  • leftEye
  • rightEye
  • leftEar
  • rightEar
  • leftShoulder
  • rightShoulder
  • leftElbow
  • rightElbow
  • leftWrist
  • rightWrist
  • leftHip
  • rightHip
  • leftKnee
  • rightKnee
  • leftAnkle
  • rightAnkle

useTensorflow

This is too experimental right now, please read the code to see how it works for now under src/useModel.js.

Examples

Some more examples. These examples unfortunately don't load on Codesandbox, so you'll have to install and load them locally for them to work (including loading the images locally to avoid CORS issues).

Loading local models

For local development you very likely want to download and load the model locally for speed. I haven't found an easy way of doing this, so let's get scrappy:

1. Open the network requests in the browser and load the library without the modelUrl:

useObjects(ref);

2. Copy the .json file and all the related files into your public folder. Create a folder called public/objects and put it all there:

Folder organization example

3. Point the use{Name} to these files from the public location:

const objects = useObjects(ref, { modelUrl: "/objects/model.json" });
const poses = usePoses(ref, { modelUrl: "/poses/model.json" });

Realtime camera recognition

To load a realtime video you can install my library use-camera and do:

import React from "react";
import useCamera from "use-camera";
import { useObjects } from "use-tensorflow";
import { Container, Box } from "./components";

export default () => {
  const ref = useCamera({ audio: false });
  const objects = useObjects(ref, { modelUrl: "/objects/model.json" });
  return (
    <Container>
      <video ref={ref} autoPlay width="640" height="480" />
      {objects && objects.map(({ left, top, width, height, label, score }) => (
        <Box
          left={left}
          top={top}
          width={width}
          height={height}
          label={label}
          color={score > 0.6 ? "blue" : "red"}
          score={score}
        />
      ))}
    </Container>
  );
};

Adding bounding boxes

An example with all the bounding boxes and the border color depending on the accuracy. We're using Styled Components here, but use any styling library you prefer:

import styled from "styled-components";

// See their implementations below
export const Container = styled.div`
  position: relative;
`;

export const Box = styled.div.attrs(props => ({
  style: {
    left: `${props.left}px`,
    top: `${props.top}px`,
    width: `${props.width}px`,
    height: `${props.height}px`
  }
}))`
  border: 2px solid ${({ color }) => color || "red"};
  position: absolute;
  border-radius: 4px;

  &::before,
  &::after {
    display: block;
    position: absolute;
    top: 0;
    color: white;
    background: ${({ color }) => color || "red"};
    padding: 3px 6px;
    font-size: 12px;
    font-family: monospace;
  }

  &::before {
    content: "${({ label }) => label}";
    left: 0;
    border-radius: 0 0 4px;
  }

  &::after {
    content: "${({ score }) => Math.round(score * 100)}%";
    right: 0;
    border-radius: 0 0 0 4px;
  }
`;

export const Circle = styled.div`
  content: "";
  background: ${({ color }) => color || "red"};
  position: absolute;
  width: 8px;
  height: 8px;
  transform: translate(-4px, -4px);
  border-radius: 8px;
  left: ${({ left }) => left}px;
  top: ${({ top }) => top}px;
  width: ${({ width }) => width}px;
  height: ${({ height }) => height}px;
`;

Credits

Unsplash picture by ROOM:

https://unsplash.com/photos/FZrn8fhqpp8