-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.ts
108 lines (95 loc) · 2.6 KB
/
index.ts
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
import { NativeModules, Platform } from 'react-native';
export interface Frame {
width: number;
height: number;
top: number;
left: number;
}
export interface Point {
x: number;
y: number;
}
export interface Language {
/** Language code of the language */
languageCode: string;
}
export type CornerPoints = readonly [Point, Point, Point, Point];
export interface TextElement {
/** Recognized text of the element (word) */
text: string;
/** Bonding box of the element (word) */
frame?: Frame;
/** Corner points of the element (word) */
cornerPoints?: CornerPoints;
}
export interface TextLine {
/** Recognized text in the line */
text: string;
/** Line bounding box */
frame?: Frame;
/** Line corner points */
cornerPoints?: CornerPoints;
/** Elements (words) in the line */
elements: TextElement[];
/** Languages recognized in the line */
recognizedLanguages: Language[];
}
export interface TextBlock {
/** Recognized text in the block */
text: string;
/** Block bounding box */
frame?: Frame;
/** Block corner points */
cornerPoints?: CornerPoints;
/** Lines of text in the block */
lines: TextLine[];
/** Languages recognized in the block */
recognizedLanguages: Language[];
}
export interface TextRecognitionResult {
/** Recognized text in the image */
text: string;
/** Block of text recognized in the image */
blocks: TextBlock[];
}
export enum TextRecognitionScript {
LATIN = 'Latin',
CHINESE = 'Chinese',
DEVANAGARI = 'Devanagari',
JAPANESE = 'Japanese',
KOREAN = 'Korean',
}
const LINKING_ERROR =
`The package '@react-native-ml-kit/text-recognition' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
const NativeTextRecognition = NativeModules.TextRecognition
? NativeModules.TextRecognition
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
const TextRecognition = {
/**
* Recognize text in the image.
*
* @param imageURL The URL/path of the image to process.
*
* @param [script=TextRecognitionScript.LATIN] The language script to recognize.
* Supported languages are Latin, Chinese, Devanagari, Japanese, and Korean.
*
* @returns Text recognition result
*/
recognize: (
imageURL: string,
script = TextRecognitionScript.LATIN
): Promise<TextRecognitionResult> => {
return NativeTextRecognition.recognize(imageURL, script);
},
};
export default TextRecognition;