-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathOCRTask.tsx
More file actions
130 lines (118 loc) · 4.01 KB
/
OCRTask.tsx
File metadata and controls
130 lines (118 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { Frame, useFrameOutput } from 'react-native-vision-camera';
import { scheduleOnRN } from 'react-native-worklets';
import { OCR_ENGLISH, OCRDetection, useOCR } from 'react-native-executorch';
import Svg, { Polygon, Text as SvgText } from 'react-native-svg';
import { TaskProps } from './types';
type Props = Omit<TaskProps, 'activeModel'>;
export default function OCRTask({
canvasSize,
cameraPositionSync,
frameKillSwitch,
onFrameOutputChange,
onReadyChange,
onProgressChange,
onGeneratingChange,
onFpsChange,
}: Props) {
const model = useOCR({ model: OCR_ENGLISH });
const [detections, setDetections] = useState<OCRDetection[]>([]);
const [imageSize, setImageSize] = useState({ width: 1, height: 1 });
const lastFrameTimeRef = useRef(Date.now());
useEffect(() => {
onReadyChange(model.isReady);
}, [model.isReady, onReadyChange]);
useEffect(() => {
onProgressChange(model.downloadProgress);
}, [model.downloadProgress, onProgressChange]);
useEffect(() => {
onGeneratingChange(model.isGenerating);
}, [model.isGenerating, onGeneratingChange]);
const ocrRof = model.runOnFrame;
const updateDetections = useCallback(
(p: { results: OCRDetection[]; frameW: number; frameH: number }) => {
setDetections(p.results);
setImageSize({ width: p.frameW, height: p.frameH });
const now = Date.now();
const diff = now - lastFrameTimeRef.current;
if (diff > 0) onFpsChange(Math.round(1000 / diff), diff);
lastFrameTimeRef.current = now;
},
[onFpsChange]
);
const frameOutput = useFrameOutput({
pixelFormat: 'rgb',
dropFramesWhileBusy: true,
enablePreviewSizedOutputBuffers: true,
onFrame: useCallback(
(frame: Frame) => {
'worklet';
if (frameKillSwitch.getDirty()) {
frame.dispose();
return;
}
try {
if (!ocrRof) return;
const isFrontCamera = cameraPositionSync.getDirty() === 'front';
const result = ocrRof(frame, isFrontCamera);
if (result) {
// Sensor frames are landscape-native, so width/height are swapped
// relative to portrait screen orientation.
scheduleOnRN(updateDetections, {
results: result,
frameW: frame.height,
frameH: frame.width,
});
}
} catch {
// Frame may be disposed before processing completes — transient, safe to ignore.
} finally {
frame.dispose();
}
},
[cameraPositionSync, frameKillSwitch, ocrRof, updateDetections]
),
});
useEffect(() => {
onFrameOutputChange(frameOutput);
}, [frameOutput, onFrameOutputChange]);
const scale = Math.max(
canvasSize.width / imageSize.width,
canvasSize.height / imageSize.height
);
const offsetX = (canvasSize.width - imageSize.width * scale) / 2;
const offsetY = (canvasSize.height - imageSize.height * scale) / 2;
if (!detections.length) return null;
return (
<View style={StyleSheet.absoluteFill} pointerEvents="none">
<Svg
width={canvasSize.width}
height={canvasSize.height}
style={StyleSheet.absoluteFill}
>
{detections.map((det, i) => {
const pts = det.bbox
.map((p) => `${p.x * scale + offsetX},${p.y * scale + offsetY}`)
.join(' ');
const labelX = det.bbox[0]!.x * scale + offsetX;
const labelY = det.bbox[0]!.y * scale + offsetY - 4;
return (
<React.Fragment key={i}>
<Polygon points={pts} fill="none" stroke="cyan" strokeWidth={2} />
<SvgText
x={labelX}
y={labelY}
fill="white"
fontSize={12}
fontWeight="bold"
>
{det.text}
</SvgText>
</React.Fragment>
);
})}
</Svg>
</View>
);
}