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

vision-camera-object-detector

v0.1.3

Published

Vision Camera plugin for detecting and tracking objects with simple classifications

Downloads

11

Readme

vision-camera-object-detector

Vision Camera plugin for detecting objects with MLKit.

This package is a plugin for react-native-vision-camera.

Installing

Using npm

$ npm i vision-camera-object-detector

Using yarn

$ yarn add vision-camera-object-detector

iOS installation

$ npx pod-install

Android installation

No additional steps

Requirements

Frame Processors require react-native-reanimated 2.2.0 or higher. Also make sure to add

import 'react-native-reanimated';

to the top of your index.js

Registering the plugin

Add react-native-reanimated plugin in babel.config.js

module.exports = {
  //...
  plugins: [
    [
      'react-native-reanimated/plugin',
      {
        globals: ['__detectObjects'], // add this line
      },
    ],
  ],
};

Usage

import * as React from 'react';
import { runOnJS } from 'react-native-reanimated';
import { StyleSheet, View, Text, Button, Dimensions } from 'react-native';
import { Camera } from 'react-native-vision-camera';
import { detectObjects, DetectedObject } from 'vision-camera-object-detector';
import {
  useCameraDevices,
  useFrameProcessor,
} from 'react-native-vision-camera';

const Label = ({ label, trackingId }) => {
  return (
    <Text style={styles.label}>
      {`TrackingId: ${trackingId}`}
      {!!label?.text && `\n${label.text}(index: ${label.index})`}
      {!!label?.confidence &&
        `\n${label.confidence * 100}%(index: ${label.index})`}
    </Text>
  );
};

const Rect = ({ object }) => {
  const label = object.labels[0] ?? null;

  return (
    <View
      style={{
        top: object.bounds.relativeOrigin.top + '%',
        left: object.bounds.relativeOrigin.left + '%',
        width: object.bounds.relativeSize.width + '%',
        height: object.bounds.relativeSize.height + '%',
        borderWidth: 0.5,
        borderColor: 'white',
      }}
    >
      <Label label={label} trackingId={object.trackingId} />
    </View>
  );
};

export default function App() {
  const [hasPermission, setHasPermission] = React.useState(false);
  const [objects, setObjects] = React.useState<DetectedObject[]>([]);
  const devices = useCameraDevices();
  const device = devices.back;
  const [enableClassification, setEnableClassification] = React.useState(false);
  const [enableMultipleObjects, setEnableMultipleObjects] =
    React.useState(false);

  React.useEffect(() => {
    (async () => {
      const status = await Camera.requestCameraPermission();
      setHasPermission(status === 'authorized');
    })();
  }, []);

  const frameProcessor = useFrameProcessor(
    (frame) => {
      'worklet';
      const detectedObjects = detectObjects(frame, {
        enableClassification,
        enableMultipleObjects,
      });
      runOnJS(setObjects)(detectedObjects);
    },
    [enableClassification, enableMultipleObjects]
  );

  return device != null && hasPermission ? (
    <View style={styles.container}>
      <Camera
        style={StyleSheet.absoluteFill}
        device={device}
        isActive={true}
        frameProcessor={frameProcessor}
        frameProcessorFps={25}
      />
      {objects.map((obj, index) => (
        <Rect key={index} object={obj} />
      ))}
      <View style={styles.footer}>
        <Button
          title={`enableClassifications: ${
            enableClassification ? 'yes' : 'no'
          }`}
          onPress={() => setEnableClassification((state) => !state)}
        />
        <Button
          title={`enableMultipleObjects: ${
            enableMultipleObjects ? 'yes' : 'no'
          }`}
          onPress={() => setEnableMultipleObjects((state) => !state)}
        />
      </View>
    </View>
  ) : null;
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    width: '100%',
  },
  label: {
    top: 0,
    left: 0,
    marginTop: -16,
    fontSize: 14,
    color: 'black',
    backgroundColor: 'white',
  },
  footer: {
    position: 'absolute',
    left: 0,
    bottom: 0,
    right: 0,
    justifyContent: 'center',
    alignItems: 'stretch',
    padding: 20,
  },
});

Developer notes

Currently react-native-vision-camera plugin made with swift won't work on XCode 14.

Apparently Objective-C works fine. I'm working on refactoring my code from Swift to Objective-C

New features

  • Option for enabling classifications(Android)
  • Option for enabling multiple object(Android)

Coming soon

  • Option for enabling classifications(iOS)
  • Option for enabling multiple object(iOS)

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library