Integrate AI image classification into a Flutter app

To integrate AI image classification into a Flutter app, you can use a pre-trained machine learning model along with a Flutter package that supports TensorFlow Lite. Below is a simple example using the tflite_flutter package for TensorFlow Lite.

Prerequisites:

  1. Install the tflite_flutter package:
   flutter pub add tflite_flutter
  1. Add the following dependency to your pubspec.yaml file:
   dependencies:
     tflite_flutter: ^0.5.0
  1. Fetch the dependencies:
   flutter pub get

Flutter Code:

Here’s a simple Flutter code example demonstrating how to use a pre-trained TensorFlow Lite model for image classification.

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'dart:typed_data';
import 'package:tflite_flutter/tflite_flutter.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ImageClassificationPage(),
    );
  }
}

class ImageClassificationPage extends StatefulWidget {
  @override
  _ImageClassificationPageState createState() => _ImageClassificationPageState();
}

class _ImageClassificationPageState extends State<ImageClassificationPage> {
  late File _image;
  late Uint8List _imageData;
  late Interpreter _interpreter;

  @override
  void initState() {
    super.initState();
    _loadModel();
  }

  Future<void> _loadModel() async {
    _interpreter = await Interpreter.fromAsset('assets/mobilenet_v1_1.0_224.tflite');
  }

  Future<void> _classifyImage() async {
    // Load the image
    _imageData = await _image.readAsBytes();
    List<int> inputShape = _interpreter.getInputTensor(0).shape!;

    // Preprocess the image
    TensorImage inputImage = TensorImage(TfLiteType.uint8);
    inputImage.loadList(_imageData, inputShape);

    // Run inference
    _interpreter.run(inputImage.buffer, _interpreter.getOutputTensor(0).buffer);

    // Get the output labels
    List<String> labels = await File('assets/labels.txt').readAsString().then((content) => content.split('\n'));

    // Get the inference result
    List<List<double>> output = _interpreter.getOutputTensor(0).toList2D();

    // Find the index with the highest probability
    int maxIndex = output[0].indexOf(output[0].reduce((curr, next) => curr > next ? curr : next));

    // Display the result
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('Image Classification Result'),
        content: Text('Label: ${labels[maxIndex]}\nProbability: ${output[0][maxIndex]}'),
        actions: [
          ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('OK'),
          ),
        ],
      ),
    );
  }

  Future<void> _pickImage() async {
    final picker = ImagePicker();
    PickedFile? pickedFile = await picker.getImage(source: ImageSource.gallery);

    if (pickedFile != null) {
      setState(() {
        _image = File(pickedFile.path);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Classification App'),
      ),
      body: Center(
        child: _image == null
            ? Text('Pick an image to classify')
            : Image.file(_image, height: 200.0),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await _pickImage();
          if (_image != null) {
            await _classifyImage();
          }
        },
        tooltip: 'Pick Image',
        child: Icon(Icons.image),
      ),
    );
  }

  @override
  void dispose() {
    _interpreter.close();
    super.dispose();
  }
}

This example uses the MobileNetV1 model for image classification, but you can replace it with another TensorFlow Lite model if needed. The labels.txt file contains the labels corresponding to the output classes.

Please make sure to have the required assets and dependencies in your Flutter project. The code allows you to pick an image from the gallery and performs image classification using the TensorFlow Lite model.

Leave a Reply

Your email address will not be published. Required fields are marked *