@@ -58,7 +58,8 @@ public class RoboflowMobile: NSObject {
58
58
let colors = modelInfo [ " colors " ] as? [ String : String ] ,
59
59
let classes = modelInfo [ " classes " ] as? [ String ] ,
60
60
let name = modelInfo [ " name " ] as? String ,
61
- let modelType = modelInfo [ " modelType " ] as? String {
61
+ let modelType = modelInfo [ " modelType " ] as? String ,
62
+ let environment = modelInfo [ " environment " ] as? [ String : Any ] {
62
63
63
64
getConfigDataBackground ( modelName: model, modelVersion: modelVersion, apiKey: apiKey, deviceID: deviceID)
64
65
@@ -69,7 +70,7 @@ public class RoboflowMobile: NSObject {
69
70
in: . userDomainMask,
70
71
appropriateFor: nil ,
71
72
create: false )
72
- _ = modelObject. loadMLModel ( modelPath: documentsURL. appendingPathComponent ( modelURL) , colors: colors, classes: classes)
73
+ _ = modelObject. loadMLModel ( modelPath: documentsURL. appendingPathComponent ( modelURL) , colors: colors, classes: classes, environment : environment )
73
74
74
75
completion ( modelObject, nil , name, modelType)
75
76
} catch {
@@ -78,15 +79,14 @@ public class RoboflowMobile: NSObject {
78
79
} else if retries > 0 {
79
80
clearModelCache ( modelName: model, modelVersion: modelVersion)
80
81
retries -= 1
81
- getModelData ( modelName: model, modelVersion: modelVersion, apiKey: apiKey, deviceID: deviceID) { [ self ] fetchedModel, error, modelName, modelType, colors, classes in
82
+ getModelData ( modelName: model, modelVersion: modelVersion, apiKey: apiKey, deviceID: deviceID) { [ self ] fetchedModel, error, modelName, modelType, colors, classes, environment in
82
83
if let err = error {
83
84
completion ( nil , err, " " , " " )
84
85
} else if let fetchedModel = fetchedModel {
85
86
let modelObject = getModelClass ( modelType: modelType)
86
- _ = modelObject. loadMLModel ( modelPath: fetchedModel, colors: colors ?? [ : ] , classes: classes ?? [ ] )
87
+ _ = modelObject. loadMLModel ( modelPath: fetchedModel, colors: colors ?? [ : ] , classes: classes ?? [ ] , environment : environment ?? [ : ] )
87
88
completion ( modelObject, nil , modelName, modelType)
88
89
} else {
89
- print ( " No Model Found. Trying Again. " )
90
90
clearAndRetryLoadingModel ( model, modelVersion, completion)
91
91
}
92
92
}
@@ -138,7 +138,6 @@ public class RoboflowMobile: NSObject {
138
138
completion ( dict, nil )
139
139
140
140
} catch {
141
- print ( error. localizedDescription)
142
141
completion ( nil , error. localizedDescription)
143
142
}
144
143
} ) . resume ( )
@@ -152,10 +151,10 @@ public class RoboflowMobile: NSObject {
152
151
153
152
154
153
//Get the model metadata from the Roboflow API
155
- private func getModelData( modelName: String , modelVersion: Int , apiKey: String , deviceID: String , completion: @escaping ( URL ? , Error ? , String , String , [ String : String ] ? , [ String ] ? ) -> ( ) ) {
154
+ private func getModelData( modelName: String , modelVersion: Int , apiKey: String , deviceID: String , completion: @escaping ( URL ? , Error ? , String , String , [ String : String ] ? , [ String ] ? , [ String : Any ] ? ) -> ( ) ) {
156
155
getConfigData ( modelName: modelName, modelVersion: modelVersion, apiKey: apiKey, deviceID: deviceID) { data, error in
157
156
if let error = error {
158
- completion ( nil , error, " " , " " , nil , nil )
157
+ completion ( nil , error, " " , " " , nil , nil , nil )
159
158
return
160
159
}
161
160
@@ -165,7 +164,7 @@ public class RoboflowMobile: NSObject {
165
164
let modelType = coreMLDict [ " modelType " ] as? String ,
166
165
let modelURLString = coreMLDict [ " model " ] as? String ,
167
166
let modelURL = URL ( string: modelURLString) else {
168
- completion ( nil , error, " " , " " , nil , nil )
167
+ completion ( nil , error, " " , " " , nil , nil , nil )
169
168
return
170
169
}
171
170
@@ -188,28 +187,29 @@ public class RoboflowMobile: NSObject {
188
187
//Download the model from the link in the API response
189
188
self . downloadModelFile ( modelName: " \( modelName) - \( modelVersion) .mlmodel " , modelVersion: modelVersion, modelURL: modelURL) { fetchedModel, error in
190
189
if let error = error {
191
- completion ( nil , error, " " , " " , nil , nil )
190
+ completion ( nil , error, " " , " " , nil , nil , nil )
192
191
return
193
192
}
194
193
195
194
if let fetchedModel = fetchedModel {
196
- _ = self . cacheModelInfo ( modelName: modelName, modelVersion: modelVersion, colors: colors ?? [ : ] , classes: classes ?? [ ] , name: name, modelType: modelType, compiledModelURL: fetchedModel)
197
- completion ( fetchedModel, nil , name, modelType, colors, classes)
195
+ _ = self . cacheModelInfo ( modelName: modelName, modelVersion: modelVersion, colors: colors ?? [ : ] , classes: classes ?? [ ] , name: name, modelType: modelType, compiledModelURL: fetchedModel, environment : environmentDict ?? [ : ] )
196
+ completion ( fetchedModel, nil , name, modelType, colors, classes, environmentDict )
198
197
} else {
199
- completion ( nil , error, " " , " " , nil , nil )
198
+ completion ( nil , error, " " , " " , nil , nil , nil )
200
199
}
201
200
}
202
201
}
203
202
}
204
203
205
204
206
- private func cacheModelInfo( modelName: String , modelVersion: Int , colors: [ String : String ] , classes: [ String ] , name: String , modelType: String , compiledModelURL: URL ) -> [ String : Any ] ? {
205
+ private func cacheModelInfo( modelName: String , modelVersion: Int , colors: [ String : String ] , classes: [ String ] , name: String , modelType: String , compiledModelURL: URL , environment : [ String : Any ] ) -> [ String : Any ] ? {
207
206
let modelInfo : [ String : Any ] = [
208
207
" colors " : colors,
209
208
" classes " : classes,
210
209
" name " : name,
211
210
" modelType " : modelType,
212
- " compiledModelURL " : compiledModelURL. lastPathComponent
211
+ " compiledModelURL " : compiledModelURL. lastPathComponent,
212
+ " environment " : environment
213
213
]
214
214
215
215
do {
@@ -227,8 +227,6 @@ public class RoboflowMobile: NSObject {
227
227
if let modelInfoData = UserDefaults . standard. data ( forKey: " \( modelName) - \( modelVersion) " ) {
228
228
let decodedData = try NSKeyedUnarchiver . unarchivedObject ( ofClasses: [ NSDictionary . self, NSString . self, NSArray . self] , from: modelInfoData) as? [ String : Any ]
229
229
return decodedData
230
- } else {
231
- print ( " Error: Could not find data for key \( modelName) - \( modelVersion) " )
232
230
}
233
231
} catch {
234
232
print ( " Error unarchiving data: \( error. localizedDescription) " )
@@ -257,13 +255,9 @@ public class RoboflowMobile: NSObject {
257
255
finalModelURL = try self . unzipModelFile ( zipURL: finalModelURL)
258
256
}
259
257
260
- print ( " Compiling model at: \( finalModelURL) " )
261
-
262
258
//Compile the downloaded model
263
259
let compiledModelURL = try MLModel . compileModel ( at: finalModelURL)
264
260
265
- print ( " Model compiled to: \( compiledModelURL) " )
266
-
267
261
// Ensure Documents directory exists
268
262
let documentsURL = try FileManager . default. url ( for: . documentDirectory,
269
263
in: . userDomainMask,
@@ -272,13 +266,9 @@ public class RoboflowMobile: NSObject {
272
266
273
267
let savedURL = documentsURL. appendingPathComponent ( " \( modelName) - \( modelVersion) .mlmodelc " )
274
268
275
- print ( " Attempting to move from: \( compiledModelURL) " )
276
- print ( " To: \( savedURL) " )
277
-
278
269
// Check if the compiled model exists
279
270
guard FileManager . default. fileExists ( atPath: compiledModelURL. path) else {
280
271
let error = NSError ( domain: " ModelCompilationError " , code: 1 , userInfo: [ NSLocalizedDescriptionKey: " Compiled model does not exist at: \( compiledModelURL. path) " ] )
281
- print ( " Error: \( error. localizedDescription) " )
282
272
completion ( nil , error)
283
273
return
284
274
}
@@ -291,15 +281,11 @@ public class RoboflowMobile: NSObject {
291
281
// Move the compiled model
292
282
do {
293
283
try FileManager . default. moveItem ( at: compiledModelURL, to: savedURL)
294
- print ( " Successfully moved model to: \( savedURL) " )
295
284
} catch {
296
- print ( " Move failed with error: \( error. localizedDescription) " )
297
285
// If move fails, try copying instead
298
286
do {
299
287
try FileManager . default. copyItem ( at: compiledModelURL, to: savedURL)
300
- print ( " Successfully copied model to: \( savedURL) " )
301
288
} catch {
302
- print ( " Copy also failed: \( error. localizedDescription) " )
303
289
completion ( nil , error)
304
290
return
305
291
}
0 commit comments