Skip to content

Commit 17e437a

Browse files
update naming so it matches RnExecutorchModule, added function to read image resource from base64, storage and external sources
1 parent 0187160 commit 17e437a

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

ios/RnExecutorch/StyleTransfer.mm

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@ - (void)forward:(NSString *)input
3434
resolve:(RCTPromiseResolveBlock)resolve
3535
reject:(RCTPromiseRejectBlock)reject {
3636
@try {
37-
NSURL *url = [NSURL URLWithString:input];
38-
cv::Mat image = cv::imread([[url path] UTF8String], cv::IMREAD_COLOR);
39-
if(image.empty()){
40-
reject(@"img_loading_error", [NSString stringWithFormat:@"%ld", (long)InvalidArgument], nil);
41-
return;
42-
}
43-
37+
cv::Mat image = [ImageProcessor readImage:input];
4438
cv::Mat resultImage = [model runModel:image];
4539

4640
NSString* tempFilePath = [ImageProcessor saveToTempFile:resultImage];

ios/RnExecutorch/utils/ImageProcessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
+ (NSArray *)matToNSArray:(const cv::Mat &)mat;
77
+ (cv::Mat)arrayToMat:(NSArray *)array width:(int)width height:(int)height;
88
+ (NSString *)saveToTempFile:(const cv::Mat &)image;
9+
+ (cv::Mat)readImage:(NSString *)source;
910

1011
@end

ios/RnExecutorch/utils/ImageProcessor.mm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,39 @@ + (NSString *)saveToTempFile:(const cv::Mat&)image {
5555
return outputPath;
5656
}
5757

58+
+ (cv::Mat)readImage:(NSString *)source {
59+
NSURL *url = [NSURL URLWithString:source];
60+
61+
cv::Mat inputImage;
62+
NSLog(@"%@", url.scheme);
63+
if([[url scheme] isEqualToString: @"data"]){
64+
//base64
65+
NSArray *parts = [source componentsSeparatedByString:@","];
66+
if ([parts count] < 2) {
67+
NSLog(@"Error: Data URI is not properly formatted");
68+
}
69+
NSString *encodedString = parts[1];
70+
NSData *data = [[NSData alloc] initWithBase64EncodedString:encodedString options:NSDataBase64DecodingIgnoreUnknownCharacters];
71+
cv::Mat encodedData(1, [data length], CV_8UC1, (void *)data.bytes);
72+
inputImage = cv::imdecode(encodedData, cv::IMREAD_COLOR);
73+
}
74+
if([[url scheme] isEqualToString: @"file"]){
75+
//local file
76+
inputImage = cv::imread([[url path] UTF8String], cv::IMREAD_COLOR);
77+
}
78+
else {
79+
//external file
80+
NSData *data = [NSData dataWithContentsOfURL:url];
81+
inputImage = cv::imdecode(cv::Mat(1, [data length], CV_8UC1, (void*)data.bytes), cv::IMREAD_COLOR);
82+
}
83+
84+
if(inputImage.empty()){
85+
@throw [NSException exceptionWithName:@"readImage_error"
86+
reason:[NSString stringWithFormat:@"%ld", (long)InvalidArgument]
87+
userInfo:nil];
88+
}
89+
90+
return inputImage;
91+
}
92+
5893
@end

src/StyleTransfer.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Props {
99

1010
interface StyleTransferModule {
1111
error: string | null;
12-
isModelLoading: boolean;
12+
isModelReady: boolean;
1313
isModelGenerating: boolean;
1414
forward: (input: string) => Promise<string>;
1515
}
@@ -18,7 +18,7 @@ export const useStyleTransfer = ({
1818
modulePath,
1919
}: Props): StyleTransferModule => {
2020
const [error, setError] = useState<null | string>(null);
21-
const [isModelLoading, setIsModelLoading] = useState(true);
21+
const [isModelReady, setIsModelReady] = useState(true);
2222
const [isModelGenerating, setIsModelGenerating] = useState(false);
2323

2424
useEffect(() => {
@@ -30,23 +30,27 @@ export const useStyleTransfer = ({
3030
}
3131

3232
try {
33-
setIsModelLoading(true);
33+
setIsModelReady(false);
3434
await StyleTransfer.loadModule(path);
3535
} catch (e) {
3636
setError(getError(e));
3737
} finally {
38-
setIsModelLoading(false);
38+
setIsModelReady(true);
3939
}
4040
};
4141

4242
loadModel();
4343
}, [modulePath]);
4444

4545
const forward = async (input: string) => {
46-
if (isModelLoading) {
46+
if (!isModelReady) {
4747
throw new Error(getError(ETError.ModuleNotLoaded));
4848
}
4949

50+
if (error) {
51+
throw new Error(error);
52+
}
53+
5054
try {
5155
setIsModelGenerating(true);
5256
const output = await StyleTransfer.forward(input);
@@ -58,5 +62,5 @@ export const useStyleTransfer = ({
5862
}
5963
};
6064

61-
return { error, isModelLoading, isModelGenerating, forward };
65+
return { error, isModelReady, isModelGenerating, forward };
6266
};

0 commit comments

Comments
 (0)