Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit cebfc22

Browse files
Merge pull request #708 from psrcek/issue699-mlkit-support
Created better text recognition interfaces + implemented them on android
2 parents 674d0ae + ae3fbda commit cebfc22

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

src/mlkit/textrecognition/index.android.ts

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { MLKitTextRecognition as MLKitTextRecognitionBase } from "./textrecognit
55
import {
66
MLKitRecognizeTextCloudOptions,
77
MLKitRecognizeTextCloudResult,
8-
MLKitRecognizeTextResultFeature
8+
MLKitRecognizeTextResultBlock,
9+
MLKitRecognizeTextResultLine,
10+
MLKitRecognizeTextResultElement,
11+
MLKitRecognizeTextResultBounds
912
} from "./index";
1013

1114
declare const com: any;
@@ -31,45 +34,56 @@ export class MLKitTextRecognition extends MLKitTextRecognitionBase {
3134
}
3235
}
3336

34-
function getOnDeviceResult(blocks: any): MLKitRecognizeTextOnDeviceResult {
35-
const result = <MLKitRecognizeTextOnDeviceResult>{
36-
features: []
37-
};
37+
function boundingBoxToBounds(rect: any): MLKitRecognizeTextResultBounds {
38+
return {
39+
x: rect.left,
40+
y: rect.top,
41+
width: rect.width(),
42+
height: rect.height()
43+
}
44+
}
3845

39-
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
46+
// see https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/textrecognition/TextRecognitionProcessor.java#L62
47+
function getOnDeviceResult(blocks: any): MLKitRecognizeTextOnDeviceResult {
48+
const blks: MLKitRecognizeTextResultBlock[] = [];
49+
4050
for (let i = 0; i < blocks.size(); i++) {
41-
const textBlock = blocks.get(i);
42-
const blockResult = <MLKitRecognizeTextResultFeature>{
43-
text: textBlock.getText(),
44-
elements: []
45-
};
51+
const block = blocks.get(i);
52+
const lines = block.getLines();
53+
54+
const lns: MLKitRecognizeTextResultLine[] = [];
4655

47-
const lines = textBlock.getLines();
4856
for (let j = 0; j < lines.size(); j++) {
49-
const elements = lines.get(j).getElements();
57+
const line = lines.get(j);
58+
const elements = line.getElements();
59+
60+
const elms: MLKitRecognizeTextResultElement[] = [];
61+
5062
for (let k = 0; k < elements.size(); k++) {
5163
const element = elements.get(k);
52-
const rect = element.getBoundingBox();
53-
const blockElement = {
64+
elms.push({
5465
text: element.getText(),
55-
bounds: {
56-
origin: {
57-
x: rect.left,
58-
y: rect.top
59-
},
60-
size: {
61-
width: rect.width(),
62-
height: rect.height()
63-
}
64-
}
65-
};
66-
blockResult.elements.push(blockElement);
66+
bounds: boundingBoxToBounds(element.getBoundingBox())
67+
});
6768
}
69+
70+
lns.push({
71+
text: line.getText(),
72+
bounds: boundingBoxToBounds(line.getBoundingBox()),
73+
elements: elms
74+
});
6875
}
6976

70-
result.features.push(blockResult);
77+
blks.push({
78+
text: block.getText(),
79+
bounds: boundingBoxToBounds(block.getBoundingBox()),
80+
lines: lns
81+
});
7182
}
72-
return result;
83+
84+
return {
85+
blocks: blks
86+
};
7387
}
7488

7589
export function recognizeTextOnDevice(options: MLKitRecognizeTextOnDeviceOptions): Promise<MLKitRecognizeTextOnDeviceResult> {

src/mlkit/textrecognition/index.d.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
import { MLKitCameraView, MLKitCloudOptions, MLKitOptions, MLKitResult } from "../index";
22

3-
export interface MLKitRecognizeTextResultFeature {
4-
text: string;
5-
elements: Array<{
6-
text: string;
7-
bounds: {
8-
origin: {
9-
x: number;
10-
y: number;
11-
},
12-
size: {
13-
width: number;
14-
height: number;
15-
}
16-
};
17-
}>
3+
export interface MLKitRecognizeTextResultBounds {
4+
x: number;
5+
y: number;
6+
width: number;
7+
height: number;
8+
}
9+
10+
export interface MLKitRecognizeTextResultElement {
11+
text: string;
12+
bounds: MLKitRecognizeTextResultBounds;
13+
}
14+
15+
export interface MLKitRecognizeTextResultLine {
16+
text: string;
17+
bounds: MLKitRecognizeTextResultBounds;
18+
elements: MLKitRecognizeTextResultElement[];
19+
}
20+
21+
export interface MLKitRecognizeTextResultBlock {
22+
text: string;
23+
bounds: MLKitRecognizeTextResultBounds;
24+
lines: MLKitRecognizeTextResultLine[];
1825
}
1926

2027
export interface MLKitRecognizeTextOnDeviceResult extends MLKitResult {
21-
features: Array<MLKitRecognizeTextResultFeature>;
28+
blocks: MLKitRecognizeTextResultBlock[];
2229
}
2330

2431
export interface MLKitRecognizeTextCloudResult extends MLKitResult {

0 commit comments

Comments
 (0)