@@ -42,50 +42,48 @@ - (instancetype)init {
42
42
}
43
43
44
44
UIImage* imageFromImageSourceWithData (NSData *data) {
45
- CGImageSourceRef imageSource = CGImageSourceCreateWithData ((__bridge CFDataRef)data, NULL );
46
- CGImageRef imageRef = CGImageSourceCreateImageAtIndex (imageSource, 0 , NULL );
47
- CFRelease (imageSource);
48
- UIImage *image = [UIImage imageWithCGImage: imageRef];
49
- CGImageRelease (imageRef);
50
- return image;
45
+ CGImageSourceRef imageSource = CGImageSourceCreateWithData ((__bridge CFDataRef)data, NULL );
46
+ CGImageRef imageRef = CGImageSourceCreateImageAtIndex (imageSource, 0 , NULL );
47
+ CFRelease (imageSource);
48
+ UIImage *image = [UIImage imageWithCGImage: imageRef];
49
+ CGImageRelease (imageRef);
50
+ return image;
51
51
}
52
52
53
53
- (void )handleMethodCall : (FlutterMethodCall*)call result : (FlutterResult)result {
54
54
FIRVision *vision = [FIRVision vision ];
55
55
NSMutableArray *ret = [NSMutableArray array ];
56
56
UIImage* uiImage = NULL ;
57
-
57
+ FIRVisionImage *image = NULL ;
58
+
58
59
if ([call.method hasSuffix: @" #detectFromPath" ]) {
59
60
NSString *path = call.arguments [@" filepath" ];
60
61
uiImage = [UIImage imageWithContentsOfFile: path];
62
+ image = [[FIRVisionImage alloc ] initWithImage: uiImage];
61
63
} else if ([call.method hasSuffix: @" #detectFromBinary" ]) {
62
64
FlutterStandardTypedData* typedData = call.arguments [@" binary" ];
63
65
uiImage = [UIImage imageWithData: typedData.data];
64
- } else {
65
- result (FlutterMethodNotImplemented);
66
- return ;
66
+ image = [[FIRVisionImage alloc ] initWithImage: uiImage];
67
67
}
68
-
69
- FIRVisionImage *image = [[FIRVisionImage alloc ] initWithImage: uiImage];
70
-
68
+
71
69
if ([call.method hasPrefix: @" FirebaseVisionTextDetector#detectFrom" ]) {
72
70
textDetector = [vision onDeviceTextRecognizer ];
73
71
[textDetector processImage: image
74
72
completion: ^(FIRVisionText *_Nullable resultText,
75
73
NSError *_Nullable error) {
76
- if (error != nil ) {
77
- [ret addObject: error.localizedDescription];
78
- result (ret);
79
- return ;
80
- } else if (resultText != nil ) {
81
- // Recognized text
82
- for (FIRVisionTextBlock *block in resultText.blocks ) {
83
- [ret addObject: visionTextBlockToDictionary (block)];
84
- }
85
- }
86
- result (ret);
87
- return ;
88
- }];
74
+ if (error != nil ) {
75
+ [ret addObject: error.localizedDescription];
76
+ result (ret);
77
+ return ;
78
+ } else if (resultText != nil ) {
79
+ // Recognized text
80
+ for (FIRVisionTextBlock *block in resultText.blocks ) {
81
+ [ret addObject: visionTextBlockToDictionary (block)];
82
+ }
83
+ }
84
+ result (ret);
85
+ return ;
86
+ }];
89
87
} else if ([call.method hasPrefix: @" FirebaseVisionBarcodeDetector#detectFrom" ]) {
90
88
barcodeDetector = [vision barcodeDetector ];
91
89
[barcodeDetector detectInImage: image
@@ -159,7 +157,104 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
159
157
result (ret);
160
158
return ;
161
159
}];
162
- }else {
160
+ } else if ([call.method hasPrefix: @" FirebaseModelManager#registerCloudModelSource" ]) {
161
+ if (call.arguments [@" source" ] != [NSNull null ] ){
162
+ NSString *modeName = call.arguments [@" source" ][@" modelName" ];
163
+ BOOL enableModelUpdates = call.arguments [@" source" ][@" enableModelUpdates" ];
164
+ FIRModelDownloadConditions *initialDownloadConditions = [[FIRModelDownloadConditions alloc ] initWithIsWiFiRequired: YES
165
+ canDownloadInBackground: YES ];
166
+ FIRModelDownloadConditions *updatesDownloadConditions = [[FIRModelDownloadConditions alloc ] initWithIsWiFiRequired: YES
167
+ canDownloadInBackground: YES ];
168
+ if (call.arguments [@" source" ][@" initialDownloadConditions" ] != [NSNull null ] ){
169
+ BOOL requireWifi = call.arguments [@" source" ][@" initialDownloadConditions" ][@" requireWifi" ];
170
+ BOOL requireDeviceIdle = call.arguments [@" source" ][@" initialDownloadConditions" ][@" requireDeviceIdle" ];
171
+ initialDownloadConditions =
172
+ [[FIRModelDownloadConditions alloc ] initWithIsWiFiRequired: requireWifi
173
+ canDownloadInBackground: requireDeviceIdle];
174
+ }
175
+ if (call.arguments [@" source" ][@" updatesDownloadConditions" ] != [NSNull null ] ){
176
+ BOOL requireWifi = call.arguments [@" source" ][@" initialDownloadConditions" ][@" requireWifi" ];
177
+ BOOL requireDeviceIdle = call.arguments [@" source" ][@" initialDownloadConditions" ][@" requireDeviceIdle" ];
178
+ initialDownloadConditions =
179
+ updatesDownloadConditions =
180
+ [[FIRModelDownloadConditions alloc ] initWithIsWiFiRequired: requireWifi
181
+ canDownloadInBackground: requireDeviceIdle];
182
+ }
183
+ FIRCloudModelSource *cloudModelSource =
184
+ [[FIRCloudModelSource alloc ] initWithModelName: modeName
185
+ enableModelUpdates: enableModelUpdates
186
+ initialConditions: initialDownloadConditions
187
+ updateConditions: updatesDownloadConditions];
188
+ BOOL registrationSuccess =
189
+ [[FIRModelManager modelManager ] registerCloudModelSource: cloudModelSource];
190
+ }
191
+ } else if ([call.method hasPrefix: @" FirebaseModelInterpreter#run" ]) {
192
+ NSString *cloudModelName = call.arguments [@" cloudModelName" ];
193
+ // TODO local model
194
+ FIRModelOptions *options = [[FIRModelOptions alloc ] initWithCloudModelName: cloudModelName localModelName: nil ];
195
+ FIRModelInterpreter *interpreter = [FIRModelInterpreter modelInterpreterWithOptions: options];
196
+ FIRModelInputOutputOptions *ioOptions = [[FIRModelInputOutputOptions alloc ] init ];
197
+ NSError *error;
198
+ NSNumber *inputIndex = call.arguments [@" inputOutputOptions" ][@" inputIndex" ];
199
+ NSNumber *inputDataType = call.arguments [@" inputOutputOptions" ][@" inputDataType" ];
200
+ FIRModelElementType inputType = (FIRModelElementType)[inputDataType intValue ];
201
+ NSArray <NSNumber *> *inputDims = call.arguments [@" inputOutputOptions" ][@" inputDims" ];
202
+ [ioOptions setInputFormatForIndex: [inputIndex unsignedIntegerValue ]
203
+ type: inputType
204
+ dimensions: inputDims
205
+ error: &error];
206
+ if (error != nil ) {
207
+ NSLog (@" Failed setInputFormatForIndex with error: %@ " , error.localizedDescription );
208
+ return ;
209
+ }
210
+
211
+ NSNumber *outputIndex = call.arguments [@" inputOutputOptions" ][@" outputIndex" ];
212
+ NSNumber *outputDataType = call.arguments [@" inputOutputOptions" ][@" outputDataType" ];
213
+ FIRModelElementType outputType = (FIRModelElementType)[outputDataType intValue ];
214
+ NSArray <NSNumber *> *outputDims = call.arguments [@" inputOutputOptions" ][@" outputDims" ];
215
+ [ioOptions setOutputFormatForIndex: [outputIndex unsignedIntegerValue ]
216
+ type: outputType
217
+ dimensions: outputDims
218
+ error: &error];
219
+ if (error != nil ) {
220
+ NSLog (@" Failed setOutputFormatForIndex with error: %@ " , error.localizedDescription );
221
+ return ;
222
+ }
223
+ FIRModelInputs *inputs = [[FIRModelInputs alloc ] init ];
224
+ FlutterStandardTypedData* typedData = call.arguments [@" inputBytes" ];
225
+ // ...
226
+ [inputs addInput: typedData.data error: &error]; // Repeat as necessary.
227
+ if (error != nil ) {
228
+ NSLog (@" Failed addInput with error: %@ " , error);
229
+ return ;
230
+ }
231
+ [interpreter runWithInputs: inputs
232
+ options: ioOptions
233
+ completion: ^(FIRModelOutputs * _Nullable outputs,
234
+ NSError * _Nullable error) {
235
+ if (error != nil || outputs == nil ) {
236
+ NSLog (@" Failed runWithInputs with error: %@ " , error.localizedDescription );
237
+ return ;
238
+ }
239
+
240
+ NSArray <NSArray <NSNumber *> *>*outputArrayOfArrays = [outputs outputAtIndex: 0 error: &error];
241
+ if (error) {
242
+ NSLog (@" Failed to process detection outputs with error: %@ " , error.localizedDescription );
243
+ return ;
244
+ }
245
+
246
+ // Get the first output from the array of output arrays.
247
+ if (!outputArrayOfArrays || !outputArrayOfArrays.firstObject || ![outputArrayOfArrays.firstObject isKindOfClass: [NSArray class ]] || !outputArrayOfArrays.firstObject .firstObject || ![outputArrayOfArrays.firstObject.firstObject isKindOfClass: [NSNumber class ]]) {
248
+ NSLog (@" Failed to get the results array from output." );
249
+ return ;
250
+ }
251
+
252
+ NSArray <NSNumber *> *ret = outputArrayOfArrays.firstObject ;
253
+
254
+ result (ret);
255
+ return ;
256
+ }];
257
+ } else {
163
258
result (FlutterMethodNotImplemented);
164
259
}
165
260
}
0 commit comments