Skip to content

Commit bbf6aee

Browse files
chmjkbMateusz Kopciński
and
Mateusz Kopciński
authored
docs: Add object detection docs (#61)
## Description This PR adds useObjectDetection to the computer vision section of the docs ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] Documentation update (improves or adds clarity to existing documentation) ### Tested on - [ ] iOS - [ ] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues <!-- Link related issues here using #issue-number --> ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [ ] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. --> --------- Co-authored-by: Mateusz Kopciński <[email protected]>
1 parent 69802ee commit bbf6aee

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Computer Vision",
3+
"position": 3,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: useObjectDetection
3+
sidebar_position: 1
4+
---
5+
6+
Object detection is a computer vision technique that identifies and locates objects within images or video. It’s commonly used in applications like image recognition, video surveillance or autonomous driving.
7+
`useObjectDetection` is a hook that allows you to seamlessly integrate object detection into your React Native applications.
8+
9+
:::caution
10+
It is recommended to use models provided by us, which are available at our [Hugging Face repository](https://huggingface.co/software-mansion/react-native-executorch-ssdlite320-mobilenet-v3-large). You can also use [constants](https://github.com/software-mansion/react-native-executorch/blob/69802ee1ca161d9df00def1dabe014d36341cfa9/src/constants/modelUrls.ts#L28) shipped with our library.
11+
:::
12+
13+
## Reference
14+
```jsx
15+
import { useObjectDetection, SSDLITE_320_MOBILENET_V3_LARGE } from 'react-native-executorch';
16+
17+
function App() {
18+
const ssdlite = useObjectDetection({
19+
modelSource: SSDLITE_320_MOBILENET_V3_LARGE, // alternatively, you can use require(...)
20+
});
21+
22+
...
23+
for (const detection of await ssdlite.forward("https://url-to-image.jpg")) {
24+
console.log("Bounding box: ", detection.bbox);
25+
console.log("Bounding label: ", detection.label);
26+
console.log("Bounding score: ", detection.score);
27+
}
28+
...
29+
}
30+
```
31+
32+
<details>
33+
<summary>Type definitions</summary>
34+
35+
```typescript
36+
interface Bbox {
37+
x1: number;
38+
x2: number;
39+
y1: number;
40+
y2: number;
41+
}
42+
43+
interface Detection {
44+
bbox: Bbox;
45+
label: keyof typeof CocoLabel;
46+
score: number;
47+
}
48+
49+
interface ObjectDetectionModule {
50+
error: string | null;
51+
isReady: boolean;
52+
isGenerating: boolean;
53+
forward: (input: string) => Promise<Detection[]>;
54+
}
55+
```
56+
</details>
57+
58+
### Arguments
59+
60+
`modelSource`
61+
62+
A string that specifies the path to the model file. You can download the model from our [HuggingFace repository](https://huggingface.co/software-mansion/react-native-executorch-ssdlite320-mobilenet-v3-large/tree/main).
63+
For more information on that topic, you can check out the [Loading models](https://docs.swmansion.com/react-native-executorch/fundamentals/loading-models) page.
64+
65+
### Returns
66+
67+
The hook returns an object with the following properties:
68+
69+
70+
| **Field** | **Type** | **Description** |
71+
|-----------------------|---------------------------------------|------------------------------------------------------------------------------------------------------------------|
72+
| `forward` | `(input: string) => Promise<Detection[]>` | A function that accepts an image (url, b64) and returns an array of `Detection` objects. |
73+
| `error` | <code>string &#124; null</code> | Contains the error message if the model loading failed. |
74+
| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. |
75+
| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. |
76+
77+
78+
## Running the model
79+
80+
To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns an array of `Detection` objects. Each object contains coordinates of the bounding box, the label of the detected object, and the confidence score. For more information, please refer to the reference or type definitions.
81+
82+
## Detection object
83+
The detection object is specified as follows:
84+
```typescript
85+
interface Bbox {
86+
x1: number;
87+
y1: number;
88+
x2: number;
89+
y2: number;
90+
}
91+
92+
interface Detection {
93+
bbox: Bbox;
94+
label: keyof typeof CocoLabels;
95+
score: number;
96+
}
97+
```
98+
The `bbox` property contains information about the bounding box of detected objects. It is represented as two points: one at the bottom-left corner of the bounding box (`x1`, `y1`) and the other at the top-right corner (`x2`, `y2`).
99+
The `label` property contains the name of the detected object, which corresponds to one of the `CocoLabels`. The `score` represents the confidence score of the detected object.
100+
101+
102+
103+
## Example
104+
```tsx
105+
import { useObjectDetection, SSDLITE_320_MOBILENET_V3_LARGE } from 'react-native-executorch';
106+
107+
function App() {
108+
const ssdlite = useObjectDetection({
109+
modelSource: SSDLITE_320_MOBILENET_V3_LARGE,
110+
});
111+
112+
const runModel = async () => {
113+
const detections = await ssdlite.forward("https://url-to-image.jpg");
114+
for (const detection of detections) {
115+
console.log("Bounding box: ", detection.bbox);
116+
console.log("Bounding label: ", detection.label);
117+
console.log("Bounding score: ", detection.score);
118+
}
119+
}
120+
}
121+
```
122+
123+
## Supported Models
124+
125+
| Model | Number of classes | Class list |
126+
| --------------------------------------------------------------------------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
127+
| [SSDLite320 MobileNetV3 Large](https://pytorch.org/vision/main/models/generated/torchvision.models.detection.ssdlite320_mobilenet_v3_large.html#torchvision.models.detection.SSDLite320_MobileNet_V3_Large_Weights) | 91 | [COCO](https://github.com/software-mansion/react-native-executorch/blob/69802ee1ca161d9df00def1dabe014d36341cfa9/src/types/object_detection.ts#L14) |

0 commit comments

Comments
 (0)