File size: 7,121 Bytes
5a3f083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter_pytorch_lite/flutter_pytorch_lite.dart';

class PlantAnomalyDetector {
  Module? _module;
  static const double _threshold = 0.5687; // Your threshold from training
  
  // Normalization values from your training data
  static const List<double> _mean = [0.4682, 0.4865, 0.3050];
  static const List<double> _std = [0.2064, 0.1995, 0.1961];

  /// Initialize the model from assets
  Future<void> loadModel() async {
    try {
      // Load model from assets
      final filePath = '${Directory.systemTemp.path}/plant_anomaly_detector.ptl';
      final modelBytes = await _getBuffer('assets/models/plant_anomaly_detector.ptl');
      File(filePath).writeAsBytesSync(modelBytes);
      
      _module = await FlutterPytorchLite.load(filePath);
      print('Model loaded successfully');
    } catch (e) {
      print('Error loading model: $e');
      rethrow;
    }
  }

  /// Get byte buffer from assets
  static Future<Uint8List> _getBuffer(String assetFileName) async {
    ByteData rawAssetFile = await rootBundle.load(assetFileName);
    final rawBytes = rawAssetFile.buffer.asUint8List();
    return rawBytes;
  }

  /// Normalize tensor values using training statistics
  List<double> _normalize(List<double> input) {
    List<double> normalized = [];
    int channels = 3;
    int pixelsPerChannel = input.length ~/ channels;
    
    for (int c = 0; c < channels; c++) {
      for (int i = 0; i < pixelsPerChannel; i++) {
        int idx = c * pixelsPerChannel + i;
        double normalizedValue = (input[idx] - _mean[c]) / _std[c];
        normalized.add(normalizedValue);
      }
    }
    
    return normalized;
  }

  /// Calculate reconstruction error (MSE) between original and reconstructed
  double _calculateReconstructionError(List<double> original, List<double> reconstructed) {
    if (original.length != reconstructed.length) {
      throw ArgumentError('Original and reconstructed tensors must have same length');
    }
    
    double sumSquaredError = 0.0;
    for (int i = 0; i < original.length; i++) {
      double diff = original[i] - reconstructed[i];
      sumSquaredError += diff * diff;
    }
    
    return sumSquaredError / original.length;
  }

  /// Detect if an image is a plant or anomaly
  Future<PlantDetectionResult> detectPlant(ui.Image image) async {
    if (_module == null) {
      throw StateError('Model not loaded. Call loadModel() first.');
    }

    try {
      // Convert image to tensor
      final inputShape = Int64List.fromList([1, 3, 224, 224]);
      Tensor inputTensor = await TensorImageUtils.imageToFloat32Tensor(
        image,
        width: 224,
        height: 224,
      );

      // Get original normalized values for reconstruction error calculation
      List<double> originalValues = inputTensor.dataAsFloat32List;
      List<double> normalizedOriginal = _normalize(originalValues);

      // Forward pass through the model
      IValue input = IValue.from(inputTensor);
      IValue output = await _module!.forward([input]);
      
      // Get reconstruction
      Tensor reconstructionTensor = output.toTensor();
      List<double> reconstruction = reconstructionTensor.dataAsFloat32List;

      // Calculate reconstruction error
      double reconstructionError = _calculateReconstructionError(
        normalizedOriginal, 
        reconstruction
      );

      // Determine if it's an anomaly
      bool isAnomaly = reconstructionError > _threshold;
      double confidence = (reconstructionError - _threshold).abs() / _threshold;

      return PlantDetectionResult(
        isPlant: !isAnomaly,
        reconstructionError: reconstructionError,
        threshold: _threshold,
        confidence: confidence,
      );

    } catch (e) {
      print('Error during inference: $e');
      rethrow;
    }
  }

  /// Dispose the model
  Future<void> dispose() async {
    if (_module != null) {
      await _module!.destroy();
      _module = null;
    }
  }
}

/// Result class for plant detection
class PlantDetectionResult {
  final bool isPlant;
  final double reconstructionError;
  final double threshold;
  final double confidence;

  PlantDetectionResult({
    required this.isPlant,
    required this.reconstructionError,
    required this.threshold,
    required this.confidence,
  });

  @override
  String toString() {
    return 'PlantDetectionResult('
        'isPlant: $isPlant, '
        'reconstructionError: ${reconstructionError.toStringAsFixed(4)}, '
        'threshold: ${threshold.toStringAsFixed(4)}, '
        'confidence: ${(confidence * 100).toStringAsFixed(2)}%'
        ')';
  }
}

/// Example usage in a Flutter widget
class PlantDetectionWidget extends StatefulWidget {
  @override
  _PlantDetectionWidgetState createState() => _PlantDetectionWidgetState();
}

class _PlantDetectionWidgetState extends State<PlantDetectionWidget> {
  final PlantAnomalyDetector _detector = PlantAnomalyDetector();
  bool _isModelLoaded = false;

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

  Future<void> _loadModel() async {
    try {
      await _detector.loadModel();
      setState(() {
        _isModelLoaded = true;
      });
    } catch (e) {
      print('Failed to load model: $e');
    }
  }

  Future<void> _detectFromAsset(String assetPath) async {
    if (!_isModelLoaded) return;

    try {
      // Load image from assets
      const assetImage = AssetImage('assets/images/test_plant.jpg');
      final image = await TensorImageUtils.imageProviderToImage(assetImage);
      
      // Run detection
      final result = await _detector.detectPlant(image);
      
      // Show result
      print('Detection result: $result');
      
      // You can update UI here with the result
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          title: Text(result.isPlant ? 'Plant Detected' : 'Anomaly Detected'),
          content: Text(
            'Reconstruction Error: ${result.reconstructionError.toStringAsFixed(4)}\n'
            'Confidence: ${(result.confidence * 100).toStringAsFixed(2)}%'
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text('OK'),
            ),
          ],
        ),
      );
      
    } catch (e) {
      print('Error during detection: $e');
    }
  }

  @override
  void dispose() {
    _detector.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Plant Anomaly Detection')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (!_isModelLoaded)
              CircularProgressIndicator()
            else
              ElevatedButton(
                onPressed: () => _detectFromAsset('assets/images/test_plant.jpg'),
                child: Text('Detect Plant'),
              ),
          ],
        ),
      ),
    );
  }
}