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 2 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.

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