Skip to content

Commit 3fe4d38

Browse files
docs: usage
1 parent 50b69a7 commit 3fe4d38

File tree

4 files changed

+129
-10
lines changed

4 files changed

+129
-10
lines changed

docs/pages/_meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"index": "Introduction",
33
"installation": "Installation",
44
"usage": "Usage",
5-
"apidetails": "API",
65
"availablefunctions": "Available functions",
6+
"apidetails": "API",
77
"---": {
88
"type": "separator"
99
},

docs/pages/installation/visioncamera.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ You can find a detailed description of the installation process [here](https://r
5151

5252
### Usage
5353

54-
The methods of use are [here](../usage.md).
54+
The methods of use are [here](../usage#mat-creation-from-vision-camera-frame).

docs/pages/installation/worklets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ You can find a detailed description of the installation process [here](https://g
3838

3939
### Usage
4040

41-
The methods of use are [here](../usage.md).
41+
The methods of use are [here](../usage#calculations-from-separated-thread).

docs/pages/usage.md

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,137 @@
11
# Usage
22

3-
cleaning of buffers
3+
> The library is in the early stages of development and not all functions or objects available in OpenCV are supported. Add an issue if you have any problems or questions.
44
5-
Objects
5+
## **Important**: Cleaning of buffers
66

7-
create object
7+
OpenCV objects during execution are held in internal memory and managed on the C++ side. Hence, after a calculation has been performed, it is necessary to release unused resources using a function:
88

9+
```js
10+
OpenCV.clearBuffers();
11+
```
912

10-
toJSValue
13+
You can do this e.g. after a calculation, or after a specific step. **Remember that not executing this function will result in data being held in memory continuously.**
1114

12-
copy object from vector
1315

16+
## Objects
1417

15-
Functions
18+
The library currently supports the following OpenCV objects: **Mat, MatVector, Point, Point Vector, Rect, RectVector, Size, Scalar and RotatedRect.**
1619

17-
invocation
20+
> A vector is simply an array in which individual objects are held.
1821
22+
### Object creation
23+
24+
To create an object, execute the createObject function, where the first argument will be the type of object (enum ObjectType) and the following parameters specific to a particular type of object.
25+
26+
```js
27+
const point = OpenCV.createObject(ObjectType.Point, 1, 2); // x, y
28+
```
29+
30+
A detailed list of parameters is available [here]().
31+
32+
#### Mat creation from Base64 image
33+
34+
To create a Mat object with image data, you can use a function that creates it from a Base64 image.
35+
36+
```js
37+
const mat = OpenCV.base64ToMat(base64string);
38+
```
39+
40+
To convert an image, such as from a gallery, you can use the [react-native-image-picker](https://github.com/react-native-image-picker/react-native-image-picker) library with selected `includeBase64` option.
41+
42+
#### Mat creation from Vision Camera Frame
43+
44+
To process real-time frames from the vision camera library frame processor, you can use the following method:
45+
46+
```js
47+
const { resize } = useResizePlugin();
48+
49+
const frameProcessor = useFrameProcessor((frame) => {
50+
'worklet';
51+
52+
const height = frame.height / 4;
53+
const width = frame.width / 4;
54+
55+
const resized = resize(frame, {
56+
scale: {
57+
width: width,
58+
height: height,
59+
},
60+
pixelFormat: 'bgr',
61+
dataType: 'uint8',
62+
});
63+
64+
const mat = OpenCV.frameBufferToMat(height, width, resized);
65+
66+
// calculations ...
67+
68+
OpenCV.clearBuffers(); // IMPORTANT! At the end.
69+
});
70+
```
71+
72+
It is good practice to scale the photo, such as using a frame processor from the [vision-camera-resize-plugin](https://github.com/mrousavy/vision-camera-resize-plugin) library.
73+
74+
75+
### Expose and copy object from vector
76+
77+
In some cases, you will need to pull a specific object from a vector, for example, a Mat object from MatVector. To get to it, use the function:
78+
79+
```js
80+
const object = OpenCV.copyObjectFromVector(vector, 3); // vector, item index
81+
```
82+
83+
### Object conversion to JavaScript value
84+
85+
Since the data is stored in memory and managed by C++, to get to know the OpenCV object data in the form of a JS object, use the function:
86+
87+
```js
88+
const data = OpenCV.toJSValue(object);
89+
```
90+
91+
92+
## Functions
93+
94+
Not all OpenCV features are currently available and ready for use. You can find how the function works on the official [OpenCV](https://opencv.org) website. The naming is consistent - all functions work in the same way as in the C++ version.
95+
96+
### Invocation
97+
To execute a function from OpenCV call the invoke method, where the first parameter is the name of the function, and the rest are the necessary parameters. Example:
98+
99+
```js
100+
OpenCV.invoke('cvtColor', srcMat, dstMat, ColorConversionCodes.COLOR_BGR2HSV);
101+
```
102+
103+
To check the list of available features, take a look [here](./availablefunctions.md).
104+
105+
106+
107+
## Calculations from separated thread
108+
109+
Operations on images can be time-consuming, and the library works with synchronous functions, hence it is a good idea to move the execution of the operation to another thread, so as not to block the JavaScript thread. Integration with this library allows tasks to be delegated to another thread.
110+
111+
112+
#### Example
113+
114+
```js
115+
import { useRunOnJS, useWorklet } from 'react-native-worklets-core';
116+
117+
// ...
118+
119+
const [base64, setBase64] = useState<string>('');
120+
121+
const setImage = useRunOnJS((data: string) => {
122+
setBase64(data);
123+
}, []);
124+
125+
const worklet = useWorklet('default', () => {
126+
'worklet';
127+
if (photo) {
128+
const src = OpenCV.base64ToMat(photo.base64);
129+
130+
const result = OpenCV.toJSValue(dst);
131+
setImage(result.base64);
132+
133+
OpenCV.clearBuffers(); // IMPORTANT
134+
}
135+
});
136+
137+
```

0 commit comments

Comments
 (0)