From f9d4a0111018fa1b17041d8a293263005065d850 Mon Sep 17 00:00:00 2001 From: chmjkb Date: Thu, 12 Dec 2024 15:25:02 +0100 Subject: [PATCH] refactor: apply review suggestions --- ios/RnExecutorch/ObjectDetection.mm | 18 +++++++++--------- ios/RnExecutorch/utils/ImageProcessor.mm | 8 ++++---- src/models/object_detection/ObjectDetection.ts | 3 ++- src/models/object_detection/types.ts | 6 +++--- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/ios/RnExecutorch/ObjectDetection.mm b/ios/RnExecutorch/ObjectDetection.mm index dc88725..1776ca0 100644 --- a/ios/RnExecutorch/ObjectDetection.mm +++ b/ios/RnExecutorch/ObjectDetection.mm @@ -44,15 +44,15 @@ - (void)forward:(NSString *)input nil); return; } - cv::Mat decodedImage = cv::imdecode( - cv::Mat(1, [data length], CV_8UC1, (void *)data.bytes), cv::IMREAD_COLOR); - NSArray *result = [model runModel:decodedImage]; - resolve(result); -} -@catch (NSException *exception) { - reject(@"result_error", [NSString stringWithFormat:@"%@", exception.reason], - nil); -} + cv::Mat decodedImage = + cv::imdecode(cv::Mat(1, [data length], CV_8UC1, (void *)data.bytes), + cv::IMREAD_COLOR); + NSArray *result = [model runModel:decodedImage]; + resolve(result); + } @catch (NSException *exception) { + reject(@"result_error", [NSString stringWithFormat:@"%@", exception.reason], + nil); + } } - (std::shared_ptr)getTurboModule: diff --git a/ios/RnExecutorch/utils/ImageProcessor.mm b/ios/RnExecutorch/utils/ImageProcessor.mm index 505e676..c8595e8 100644 --- a/ios/RnExecutorch/utils/ImageProcessor.mm +++ b/ios/RnExecutorch/utils/ImageProcessor.mm @@ -14,8 +14,8 @@ + (NSArray *)matToNSArray:(const cv::Mat &)mat { int row = i / mat.cols; int col = i % mat.cols; cv::Vec3b pixel = mat.at(row, col); - floatArray[i] = @(pixel[2] / 255.0f); - floatArray[pixelCount + i] = @(pixel[1] / 255.0f); + floatArray[0 * pixelCount + i] = @(pixel[2] / 255.0f); + floatArray[1 * pixelCount + i] = @(pixel[1] / 255.0f); floatArray[2 * pixelCount + i] = @(pixel[0] / 255.0f); } @@ -31,8 +31,8 @@ + (NSArray *)matToNSArray:(const cv::Mat &)mat { int col = i % width; float r = 0, g = 0, b = 0; - r = [[array objectAtIndex: i] floatValue]; - g = [[array objectAtIndex: pixelCount + i] floatValue]; + r = [[array objectAtIndex: 0 * pixelCount + i] floatValue]; + g = [[array objectAtIndex: 1 * pixelCount + i] floatValue]; b = [[array objectAtIndex: 2 * pixelCount + i] floatValue]; cv::Vec3b color((uchar)(b * 255), (uchar)(g * 255), (uchar)(r * 255)); diff --git a/src/models/object_detection/ObjectDetection.ts b/src/models/object_detection/ObjectDetection.ts index 2b76f31..bf8a78e 100644 --- a/src/models/object_detection/ObjectDetection.ts +++ b/src/models/object_detection/ObjectDetection.ts @@ -53,8 +53,9 @@ export const useObjectDetection = ({ setIsModelGenerating(false); return output; } catch (e) { - setIsModelGenerating(false); throw new Error(getError(e)); + } finally { + setIsModelGenerating(false); } }; diff --git a/src/models/object_detection/types.ts b/src/models/object_detection/types.ts index bd33ad8..78aa324 100644 --- a/src/models/object_detection/types.ts +++ b/src/models/object_detection/types.ts @@ -6,7 +6,7 @@ export interface Bbox { } export interface Detection { - bbox: Bbox, + bbox: Bbox; label: keyof typeof CocoLabel; score: number; } @@ -101,5 +101,5 @@ enum CocoLabel { TEDDY_BEAR = 88, HAIR_DRIER = 89, TOOTHBRUSH = 90, - HAIR_BRUSH = 91 -}; \ No newline at end of file + HAIR_BRUSH = 91, +}