AIHelper AIHelper is a Flutter package designed to simplify the integration of TensorFlow Lite models into your applications. It provides utilities for loading .tflite models, managing labels, and performing AI inference seamlessly.
Features Load TensorFlow Lite models and labels. Perform AI inference on images with ease. Normalize and preprocess image data for TFLite compatibility. Simple and intuitive API for developers. Installation Add ai_helper to your pubspec.yaml file:
yaml Copy code dependencies: ai_helper: ^1.0.0 Run the following command to install the package:
bash Copy code flutter pub get Usage
- Add Model and Labels Add your .tflite model and labels.txt to your Flutter project under the assets folder. Update the pubspec.yaml to include the assets: yaml Copy code flutter: assets: - assets/model.tflite - assets/labels.txt
- Initialize AIHelper Import the package and initialize it with your model and label paths:
dart Copy code import 'package:ai_helper/ai_helper.dart';
final aiHelper = AIHelper( modelPath: 'assets/model.tflite', labelPath: 'assets/labels.txt', ); 3. Perform Inference Pass an image file to the classify method:
dart Copy code import 'dart:io';
final result = aiHelper.classify(File('path_to_image.jpg'));
print('Prediction: ${result'label'
}');
print('Confidence: ${(result'confidence'
* 100).toStringAsFixed(2)}%');
Example
Here’s a complete example of using AIHelper in a Flutter app:
dart Copy code import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; import 'package:ai_helper/ai_helper.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'AI Helper Demo', theme: ThemeData(primarySwatch: Colors.blue), home: AIHelperDemo(), ); } }
class AIHelperDemo extends StatefulWidget { @override _AIHelperDemoState createState() => _AIHelperDemoState(); }
class _AIHelperDemoState extends State
@override void initState() { super.initState(); _aiHelper = AIHelper( modelPath: 'assets/model.tflite', labelPath: 'assets/labels.txt', ); }
Future
if (pickedFile != null) {
setState(() {
_image = File(pickedFile.path);
});
final result = _aiHelper.classify(_image!);
setState(() {
_prediction = "${result['label']} (${(result['confidence'] * 100).toStringAsFixed(2)}%)";
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('AI Helper Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
_image == null ? Text('No image selected.') : Image.file(_image!),
SizedBox(height: 20),
_prediction == null ? Text('No prediction.') : Text('Prediction: $_prediction'),
ElevatedButton(
onPressed: _pickImage,
child: Text('Pick Image'),
),
,
),
),
);
}
}
API Reference
AIHelper
Constructor
dart
Copy code
AIHelper({
required String modelPath,
required String labelPath,
int inputSize = 224,
});
modelPath: Path to the .tflite model file.
labelPath: Path to the labels.txt file.
inputSize: (Optional) Input size of the model (default: 224).
Methods
classify(File imageFile)
Takes a File object representing the image.
Returns a Map<String, dynamic> with:
label: The predicted label.
confidence: Confidence score of the prediction.
Assets
Ensure your .tflite model and labels.txt are correctly added to your project’s assets and included in pubspec.yaml.
License MIT License. Feel free to use and modify the package.
Notes Replace "assets/model.tflite" and "assets/labels.txt" with your actual file paths. Add additional utility methods (e.g., audio or video inference) in future updates.