Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix bug in ocrs, remove node_modules from cv example app #122

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class VerticalOCR(reactContext: ReactApplicationContext) :

resMap.putString("text", text)
resMap.putArray("bbox", box.toWritableArray())
resMap.putDouble("confidence", confidenceScore)
resMap.putDouble("score", confidenceScore)

predictions.pushMap(resMap)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class RecognitionHandler(

resMap.putString("text", decodedTexts[0])
resMap.putArray("bbox", box.toWritableArray())
resMap.putDouble("confidence", confidenceScore)
resMap.putDouble("score", confidenceScore)

res.pushMap(resMap)
}
Expand Down

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions examples/computer-vision/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"metro-config": "^0.81.0",
"react": "18.3.1",
"react-native": "0.76.3",
"react-native-audio-api": "^0.4.13",
"react-native-executorch": "^0.3.0",
"react-native-image-picker": "^7.2.2",
"react-native-loading-spinner-overlay": "^3.0.1",
Expand Down
13 changes: 13 additions & 0 deletions examples/computer-vision/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3889,6 +3889,7 @@ __metadata:
metro-config: ^0.81.0
react: 18.3.1
react-native: 0.76.3
react-native-audio-api: ^0.4.13
react-native-executorch: ^0.3.0
react-native-image-picker: ^7.2.2
react-native-loading-spinner-overlay: ^3.0.1
Expand Down Expand Up @@ -7486,6 +7487,18 @@ __metadata:
languageName: node
linkType: hard

"react-native-audio-api@npm:^0.4.13":
version: 0.4.13
resolution: "react-native-audio-api@npm:0.4.13"
peerDependencies:
react: "*"
react-native: "*"
bin:
setup-rn-audio-api-web: scripts/setup-rn-audio-api-web.js
checksum: 9a5db4626a0663224cdcdc957ed5176df9ed65df9f58bf06c5eae2a91895b7ee0f0a632ecaa79b23ef7536983485cea2720ca46201e2e690548f400b66970ff6
languageName: node
linkType: hard

"react-native-executorch@npm:^0.3.0":
version: 0.3.0
resolution: "react-native-executorch@npm:0.3.0"
Expand Down
1 change: 1 addition & 0 deletions examples/llama/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"metro-config": "^0.81.0",
"react": "18.3.1",
"react-native": "0.76.3",
"react-native-audio-api": "^0.4.13",
"react-native-executorch": "^0.3.0",
"react-native-loading-spinner-overlay": "^3.0.1",
"react-native-markdown-display": "^7.0.2",
Expand Down
13 changes: 13 additions & 0 deletions examples/llama/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6074,6 +6074,7 @@ __metadata:
metro-config: ^0.81.0
react: 18.3.1
react-native: 0.76.3
react-native-audio-api: ^0.4.13
react-native-executorch: ^0.3.0
react-native-loading-spinner-overlay: ^3.0.1
react-native-markdown-display: ^7.0.2
Expand Down Expand Up @@ -7555,6 +7556,18 @@ __metadata:
languageName: node
linkType: hard

"react-native-audio-api@npm:^0.4.13":
version: 0.4.13
resolution: "react-native-audio-api@npm:0.4.13"
peerDependencies:
react: "*"
react-native: "*"
bin:
setup-rn-audio-api-web: scripts/setup-rn-audio-api-web.js
checksum: 9a5db4626a0663224cdcdc957ed5176df9ed65df9f58bf06c5eae2a91895b7ee0f0a632ecaa79b23ef7536983485cea2720ca46201e2e690548f400b66970ff6
languageName: node
linkType: hard

"react-native-executorch@npm:^0.3.0":
version: 0.3.0
resolution: "react-native-executorch@npm:0.3.0"
Expand Down
13 changes: 5 additions & 8 deletions ios/RnExecutorch/OCR.mm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#import "OCR.h"
#import "models/ocr/Detector.h"
#import "models/ocr/RecognitionHandler.h"
#import "utils/ImageProcessor.h"
#import "models/ocr/utils/Constants.h"
#import "utils/ImageProcessor.h"
#import <ExecutorchLib/ETModel.h>
#import <React/RCTBridgeModule.h>

Expand Down Expand Up @@ -80,14 +80,11 @@ of different sizes (e.g. large - 512x64, medium - 256x64, small - 128x64).
@try {
cv::Mat image = [ImageProcessor readImage:input];
NSArray *result = [detector runModel:image];
cv::Size detectorSize = [detector getModelImageSize];
const CGFloat recognizerRatio = recognizerImageSize / detectorSize.width;
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
result = [self->recognitionHandler
recognize:result
imgGray:image
desiredWidth:detectorSize.width * recognizerRatio
desiredHeight:detectorSize.height * recognizerRatio];
result = [self->recognitionHandler recognize:result
imgGray:image
desiredWidth:recognizerImageSize
desiredHeight:recognizerImageSize];
resolve(result);
} @catch (NSException *exception) {
reject(@"forward_error",
Expand Down
2 changes: 1 addition & 1 deletion ios/RnExecutorch/models/ocr/Detector.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ @implementation Detector {
NSArray *inputShape = [module getInputShape:@0];
NSNumber *widthNumber = inputShape[inputShape.count - 2];
NSNumber *heightNumber = inputShape.lastObject;

const int height = [heightNumber intValue];
const int width = [widthNumber intValue];
modelSize = cv::Size(height, width);
Expand Down
8 changes: 4 additions & 4 deletions ios/RnExecutorch/models/ocr/Recognizer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ @implementation Recognizer {

- (cv::Size)getModelImageSize {
NSArray *inputShape = [module getInputShape:@0];
NSNumber *widthNumber = inputShape[inputShape.count - 2];
NSNumber *heightNumber = inputShape.lastObject;
NSNumber *widthNumber = inputShape.lastObject;
NSNumber *heightNumber = inputShape[inputShape.count - 2];

const int height = [heightNumber intValue];
const int width = [widthNumber intValue];
Expand All @@ -24,8 +24,8 @@ @implementation Recognizer {

- (cv::Size)getModelOutputSize {
NSArray *outputShape = [module getOutputShape:@0];
NSNumber *widthNumber = outputShape[outputShape.count - 2];
NSNumber *heightNumber = outputShape.lastObject;
NSNumber *widthNumber = outputShape.lastObject;
NSNumber *heightNumber = outputShape[outputShape.count - 2];

const int height = [heightNumber intValue];
const int width = [widthNumber intValue];
Expand Down
10 changes: 5 additions & 5 deletions ios/RnExecutorch/models/ocr/utils/RecognizerUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ + (double)computeConfidenceScore:(NSArray<NSNumber *> *)valuesArray

points.emplace_back(cv::Point2f(point.x, point.y));
}

cv::Rect rect = cv::boundingRect(points);
cv::Mat croppedImage = img(rect);
cv::cvtColor(croppedImage, croppedImage, cv::COLOR_BGR2GRAY);
cv::resize(croppedImage, croppedImage, cv::Size(smallVerticalRecognizerWidth, recognizerHeight), 0, 0,
cv::INTER_AREA);
cv::medianBlur(img, img, 1);
return croppedImage;
}

+ (cv::Mat)cropSingleCharacter:(cv::Mat)img {
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
cv::resize(img, img, cv::Size(smallVerticalRecognizerWidth, recognizerHeight), 0, 0,
cv::INTER_AREA);
cv::medianBlur(img, img, 1);

cv::Mat histogram;

Expand Down
Loading