ai_image_validator 1.0.4
ai_image_validator: ^1.0.4 copied to clipboard
A Flutter package for validating images using Google's Gemini AI. Easily verify if images match specific classes with customizable confidence thresholds.
AI Image Validator #
A Flutter package for validating images using Google's Gemini AI. Easily verify if images match specific classes with customizable confidence thresholds.
π₯ Demo #
See the package in action: image selection, AI validation, and result display.
β¨ Features #
- π€ AI-Powered Validation: Uses Google's Gemini AI for accurate image classification
- πΈ Image Selection: Built-in support for camera and gallery
- π― Customizable Classes: Define your own list of allowed image classes
- βοΈ Confidence Threshold: Set minimum confidence levels for validation
- π¨ Fully Customizable UI: Create your own interface, the package handles only the logic
- β‘ Simple API: Easy to integrate with just a few lines of code
π¦ Installation #
Add this to your package's pubspec.yaml file:
dependencies:
ai_image_validator: ^1.0.0
Then run:
flutter pub get
π Getting Started #
1. Get a Gemini API Key #
- Visit Google AI Studio
- Create a new API key (it's free!)
- Copy your API key
2. Initialize the Package #
import 'package:ai_image_validator/ai_image_validator.dart';
void main() {
// Initialize once at app startup
AiImageValidator.initialize(
apiKey: 'YOUR_GEMINI_API_KEY',
model: 'gemini-2.0-flash', // Optional, this is the default
);
runApp(MyApp());
}
π Usage #
Basic Example #
import 'package:ai_image_validator/ai_image_validator.dart';
import 'dart:io';
// 1. Pick an image
final File? imageFile = await AiImageValidator.pickImage(
ImageSource.gallery, // or ImageSource.camera
);
if (imageFile != null) {
// 2. Validate the image
final ImageValidationResult result = await AiImageValidator.validateImage(
imageFile: imageFile,
allowedClasses: ['cat', 'dog', 'bird'],
minConfidence: 0.7,
);
// 3. Check the result
if (result.isAllowed) {
print('β
Valid image: ${result.detectedClass}');
print('Confidence: ${result.confidence}');
} else {
print('β Invalid image');
}
}
Complete Example with Custom UI #
import 'package:flutter/material.dart';
import 'package:ai_image_validator/ai_image_validator.dart';
import 'dart:io';
class ImageValidatorPage extends StatefulWidget {
@override
_ImageValidatorPageState createState() => _ImageValidatorPageState();
}
class _ImageValidatorPageState extends State<ImageValidatorPage> {
File? _image;
ImageValidationResult? _result;
bool _isLoading = false;
// Configure your validation parameters
final List<String> allowedClasses = ['cat', 'dog', 'bird'];
final double minConfidence = 0.7;
Future<void> _validateImage(ImageSource source) async {
setState(() => _isLoading = true);
try {
// Pick image
final imageFile = await AiImageValidator.pickImage(source);
if (imageFile == null) {
setState(() => _isLoading = false);
return;
}
// Validate image
final result = await AiImageValidator.validateImage(
imageFile: imageFile,
allowedClasses: allowedClasses,
minConfidence: minConfidence,
);
setState(() {
_image = imageFile;
_result = result;
_isLoading = false;
});
// Show result
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
result.isAllowed
? 'β
Valid: ${result.detectedClass}'
: 'β Invalid image',
),
backgroundColor: result.isAllowed ? Colors.green : Colors.red,
),
);
} catch (e) {
setState(() => _isLoading = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: $e')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('AI Image Validator')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_image != null) Image.file(_image!, height: 200),
if (_result != null) ...[
Text('Class: ${_result!.detectedClass}'),
Text('Confidence: ${(_result!.confidence * 100).toInt()}%'),
Text(_result!.isAllowed ? 'β
Valid' : 'β Invalid'),
],
if (_isLoading) CircularProgressIndicator(),
SizedBox(height: 20),
ElevatedButton.icon(
onPressed: () => _validateImage(ImageSource.gallery),
icon: Icon(Icons.photo_library),
label: Text('Pick from Gallery'),
),
ElevatedButton.icon(
onPressed: () => _validateImage(ImageSource.camera),
icon: Icon(Icons.camera_alt),
label: Text('Take Photo'),
),
],
),
),
);
}
}
π API Reference #
AiImageValidator.initialize() #
Initializes the Gemini AI client. Must be called once before using the package.
Parameters:
apiKey(String, required): Your Gemini API keymodel(String, optional): Gemini model to use (default: 'gemini-2.0-flash')
AiImageValidator.initialize(
apiKey: 'YOUR_API_KEY',
model: 'gemini-2.0-flash',
);
AiImageValidator.pickImage() #
Opens the device's image picker to select an image.
Parameters:
source(ImageSource, required): EitherImageSource.cameraorImageSource.gallery
Returns: Future<File?> - The selected image file, or null if cancelled
final File? image = await AiImageValidator.pickImage(ImageSource.gallery);
AiImageValidator.validateImage() #
Validates an image using Gemini AI.
Parameters:
imageFile(File, required): The image file to validateallowedClasses(ListminConfidence(double, optional): Minimum confidence threshold (default: 0.7)
Returns: Future<ImageValidationResult>
final result = await AiImageValidator.validateImage(
imageFile: imageFile,
allowedClasses: ['cat', 'dog', 'bird'],
minConfidence: 0.7,
);
ImageValidationResult #
The result object returned by validateImage().
Properties:
detectedClass(String): The class detected by the AIconfidence(double): Confidence level (0.0 - 1.0)isAllowed(bool): Whether the image is valid (class is in allowedClasses and confidence >= minConfidence)
print(result.detectedClass); // e.g., "cat"
print(result.confidence); // e.g., 0.95
print(result.isAllowed); // e.g., true
π‘ Use Cases #
- Profile Picture Validation: Ensure users upload appropriate profile photos
- Product Image Verification: Validate that uploaded images match product categories
- Content Moderation: Filter images based on specific categories
- Document Verification: Check if uploaded images are valid documents
- Animal/Pet Recognition: Verify pet images for pet-related apps
- Food Classification: Validate food images for recipe or restaurant apps
π¨ Customization #
This package provides only the core validation logic. You have complete freedom to:
- Design your own UI/UX
- Implement custom image pickers
- Add your own loading states
- Create custom error handling
- Integrate with state management (GetX, Provider, Bloc, etc.)
See the example folder for a complete implementation with GetX, BottomSheet, and SnackBar.
βοΈ Requirements #
- Flutter SDK: >=3.10.0
- Dart SDK: >=3.10.4
- Google Gemini API key (free at Google AI Studio)
π Supported Platforms #
- β Android
- β iOS
- β Web
- β macOS
- β Windows
- β Linux
π± Permissions #
Android #
Add to android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
iOS #
Add to ios/Runner/Info.plist:
<key>NSCameraUsageDescription</key>
<string>We need access to your camera to take photos</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library to select images</string>
π€ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
π License #
This project is licensed under the MIT License - see the LICENSE file for details.
π Credits #
- Powered by Google Gemini AI
- Uses image_picker for image selection
- Uses google_generative_ai for AI integration
π Support #
- π Report bugs
- π‘ Request features
- π§ Email: your-email@example.com
π Show Your Support #
If this package helped you, please βοΈ the repo and share it with others!