loadModel method
Loads the YOLO model for inference.
This method must be called before predict to initialize the model.
Returns true
if the model was loaded successfully, false
otherwise.
Example:
bool success = await yolo.loadModel();
if (success) {
print('Model loaded successfully');
} else {
print('Failed to load model');
}
@throws ModelLoadingException if the model file cannot be found @throws PlatformException if there's an issue with the platform-specific code
Implementation
Future<bool> loadModel() async {
if (!_isInitialized) {
await _initializeInstance();
}
try {
final Map<String, dynamic> arguments = {
'modelPath': modelPath,
'task': task.name,
};
// Only include instanceId for multi-instance mode
if (_instanceId != 'default') {
arguments['instanceId'] = _instanceId;
}
final result = await _channel.invokeMethod('loadModel', arguments);
return result == true;
} on PlatformException catch (e) {
if (e.code == 'MODEL_NOT_FOUND') {
throw ModelLoadingException('Model file not found: $modelPath');
} else if (e.code == 'INVALID_MODEL') {
throw ModelLoadingException('Invalid model format: $modelPath');
} else if (e.code == 'UNSUPPORTED_TASK') {
throw ModelLoadingException(
'Unsupported task type: ${task.name} for model: $modelPath',
);
} else {
throw ModelLoadingException('Failed to load model: ${e.message}');
}
} catch (e) {
throw ModelLoadingException('Unknown error loading model: $e');
}
}