SHARE
TWEET

Untitled

a guest Jul 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:typed_data';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:mlkit/mlkit.dart';
  5.  
  6. void main() => runApp(MyApp());
  7.  
  8. class MyApp extends StatelessWidget {
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return MaterialApp(
  12.       title: 'Tflite add',
  13.       theme: ThemeData(
  14.         primarySwatch: Colors.blue,
  15.       ),
  16.       home: MyHomePage(),
  17.     );
  18.   }
  19. }
  20.  
  21. class MyHomePage extends StatefulWidget {
  22.   @override
  23.   _MyHomePageState createState() => _MyHomePageState();
  24. }
  25.  
  26. class _MyHomePageState extends State<MyHomePage> {
  27.   String _left = "1.2";
  28.   String _right = "3.4";
  29.   String _output = "";
  30.   FirebaseModelInterpreter interpreter = FirebaseModelInterpreter.instance;
  31.   FirebaseModelManager manager = FirebaseModelManager.instance;
  32.  
  33.   Future<void> initTflite() {
  34.     return this.manager.registerRemoteModelSource(
  35.           FirebaseRemoteModelSource(
  36.             modelName: "add",
  37.             enableModelUpdates: true,
  38.           ),
  39.         );
  40.   }
  41.  
  42.   void _onLeftOpChanged(String op) {
  43.     setState(() {
  44.       _left = op;
  45.     });
  46.   }
  47.  
  48.   void _onRightOpChanged(String op) {
  49.     setState(() {
  50.       _right = op;
  51.     });
  52.   }
  53.  
  54.   @override
  55.   void initState() {
  56.     super.initState();
  57.     initTflite();
  58.   }
  59.  
  60.   void _onActionButtonPressed() async {
  61.     final leftNum = double.parse(_left);
  62.     final rightNum = double.parse(_right);
  63.  
  64.     const ROW = 2;
  65.     const COLUMN = 5;
  66.  
  67.     const options = FirebaseModelInputOutputOptions([
  68.       FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [ROW, COLUMN]),
  69.       FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [ROW, COLUMN]),
  70.     ], [
  71.       FirebaseModelIOOption(FirebaseModelDataType.FLOAT32, [ROW, COLUMN]),
  72.     ]);
  73.  
  74.     final left = List<double>(ROW * COLUMN);
  75.     final right = List<double>(ROW * COLUMN);
  76.     for (var i = 0; i < ROW; i++) {
  77.       for (var j = 0; j < COLUMN; j++) {
  78.         left[COLUMN * i + j] = leftNum;
  79.         right[COLUMN * i + j] = rightNum;
  80.       }
  81.     }
  82.     final concat = <double>[]..addAll(left)..addAll(right);
  83.     print(concat);
  84.     final input = float32ListToUint8List(Float32List.fromList(concat));
  85.     print(input);
  86.  
  87.     final output = await this.interpreter.run("add", options, input);
  88.     print(output);
  89.  
  90.     setState(() {
  91.       _output = (output[0][0][0] as double).toStringAsFixed(2);
  92.     });
  93.   }
  94.  
  95.   @override
  96.   Widget build(BuildContext context) {
  97.     return Scaffold(
  98.       body: Padding(
  99.         padding: const EdgeInsets.all(16.0),
  100.         child: Center(
  101.           child: Column(
  102.             mainAxisAlignment: MainAxisAlignment.center,
  103.             children: <Widget>[
  104.               TextField(
  105.                 decoration: InputDecoration(
  106.                   labelText: 'left-hand operand',
  107.                 ),
  108.                 onChanged: _onLeftOpChanged,
  109.                 keyboardType: TextInputType.numberWithOptions(
  110.                   signed: true,
  111.                   decimal: true,
  112.                 ),
  113.               ),
  114.               TextField(
  115.                 decoration: InputDecoration(
  116.                   labelText: 'right-hand operand',
  117.                 ),
  118.                 onChanged: _onRightOpChanged,
  119.                 keyboardType: TextInputType.numberWithOptions(
  120.                   signed: true,
  121.                   decimal: true,
  122.                 ),
  123.               ),
  124.               Padding(
  125.                 padding: const EdgeInsets.only(top: 32.0),
  126.                 child: Text(
  127.                   _output,
  128.                   style: Theme.of(context).textTheme.display1,
  129.                 ),
  130.               ),
  131.             ],
  132.           ),
  133.         ),
  134.       ),
  135.       floatingActionButton: FloatingActionButton(
  136.         onPressed: _onActionButtonPressed,
  137.         tooltip: 'Execute',
  138.         child: Icon(Icons.add),
  139.       ),
  140.     );
  141.   }
  142. }
  143.  
  144. Uint8List float32ListToUint8List(Float32List list) {
  145.   return list.buffer.asUint8List();
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top