|
| 1 | +--- |
| 2 | +title: useObjectDetection |
| 3 | +sidebar_position: 1 |
| 4 | +--- |
| 5 | + |
| 6 | +`useObjectDetection` is a hook that lets you seamlessly integrate object detection into your React Native application. Currently, the SSDLite320Large model with MobileNetv3 backbone is supported. |
| 7 | + |
| 8 | +## Reference |
| 9 | +```jsx |
| 10 | +import { useObjectDetection } from 'react-native-executorch'; |
| 11 | + |
| 12 | +function App() { |
| 13 | + const ssdlite = useObjectDetection({ |
| 14 | + modelSource: require("./assets/ssdlite320large_mobilenetv3.pte"), |
| 15 | + }); |
| 16 | + |
| 17 | + ... |
| 18 | + for (const detection of await ssdlite.forward("https://url-to-image.jpg")) { |
| 19 | + console.log("Bounding box: ", detection.bbox); |
| 20 | + console.log("Bounding label: ", detection.label); |
| 21 | + console.log("Bounding score: ", detection.score); |
| 22 | + } |
| 23 | + ... |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +<details> |
| 28 | +<summary>Type definitions</summary> |
| 29 | + |
| 30 | +```typescript |
| 31 | +``` |
| 32 | +</details> |
| 33 | + |
| 34 | +### Arguments |
| 35 | + |
| 36 | +`modelSource` |
| 37 | + |
| 38 | +A String that specifies the path to the model file. You can download the model from our HuggingFace repository. |
| 39 | +For SSDLite, you can add it to your assets directory, and use `require()`. If you prefer to download the model |
| 40 | +the model in runtime instead of bundling it, you can use the constants that we ship with the library. |
| 41 | + |
| 42 | +### Returns |
| 43 | + |
| 44 | +The hook returns an object with the following properties: |
| 45 | + |
| 46 | + |
| 47 | +| Field | Type | Description | |
| 48 | +| ------------------- | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- | |
| 49 | +| `forward` | `(input: string) => Promise<Detection[]>` | Function that accepts an image and returns an array of Detection objects | |
| 50 | +| `error` | <code>string | null</code> | Contains the error message if the model failed to load or failed during generation process | |
| 51 | +| `isGenerating` | `boolean` | Indicates whether the model is processing the response | |
| 52 | +| `isReady` | `boolean` | Indicates whether the model has properly loaded and is ready for inference | |
| 53 | + |
| 54 | +### Detection object |
| 55 | +The detection object is specified as follows: |
| 56 | +```typescript |
| 57 | +interface Bbox { |
| 58 | + x1: number; |
| 59 | + y1: number; |
| 60 | + x2: number; |
| 61 | + y2: number; |
| 62 | +} |
| 63 | + |
| 64 | +interface Detection { |
| 65 | + bbox: Bbox; |
| 66 | + label: keyof typeof CocoLabels; |
| 67 | + score: number; |
| 68 | +} |
| 69 | +``` |
| 70 | +The `bbox` property contains information about the bounding box of detected objects. It is represented as two points, one on the left bottom part of the bounding box (x1, y1), the second one as the top right part (x2, y2). |
| 71 | +The label property contains the name of the detected object, which is one of `CocoLabels`. The `score` is a confidence score of the detected object. |
| 72 | + |
| 73 | +### Running the model |
| 74 | + |
| 75 | +To run the model, you can use the `forward` method. It accepts one argument, which is the image. It can be either a remote URL, |
| 76 | +a local file or base64 encoded image. The function returns an array of `Detection` objects. Each one contains coordinates |
| 77 | +of the bounding box, the label of the detected object and confidence score. For more information, please refer to the reference or example. |
| 78 | + |
| 79 | +### End to end example |
| 80 | +```tsx |
| 81 | +import { useObjectDetection, SSDLITE320LARGE_MOBILENETV3_WEIGHTS } from 'react-native-executorch'; |
| 82 | + |
| 83 | +function App() { |
| 84 | + const ssdlite = useObjectDetection({ |
| 85 | + modelSource: SSDLITE320LARGE_MOBILENETV3_WEIGHTS, // Can also use require('') as well |
| 86 | + }); |
| 87 | + |
| 88 | + const runModel = async () => { |
| 89 | + const detections = await ssdlite.forward("https://url-to-image.jpg"); |
| 90 | + for (const detection of detections) { |
| 91 | + console.log("Bounding box: ", detection.bbox); // [x, y, width, height] |
| 92 | + console.log("Bounding label: ", detection.label); |
| 93 | + console.log("Bounding score: ", detection.score); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Benchmarks |
| 100 | +TODO |
0 commit comments