Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.61 KB | None | 0 0
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:tflite/tflite.dart';
  6. import "dart:developer";
  7. import "package:image_picker/image_picker.dart";
  8. import 'package:path/path.dart';
  9.  
  10. void main() {
  11.   runApp(MyApp());
  12. }
  13.  
  14. const String ssd = "SSD MobileNet";
  15. const String yolo = "Tiny YOLOv2";
  16.  
  17. class MyApp extends StatelessWidget {
  18.   @override
  19.   Widget build(BuildContext context) {
  20.     return MaterialApp(
  21.       debugShowCheckedModeBanner: false,
  22.       home: TfliteHome(),
  23.     );
  24.   }
  25. }
  26.  
  27. class TfliteHome extends StatefulWidget {
  28.   @override
  29.   _TfliteHomeState createState() => _TfliteHomeState();
  30. }
  31.  
  32. class _TfliteHomeState extends State<TfliteHome> {
  33.   File _image;
  34.   bool _busy = false;
  35.   int _recTime;
  36.   int _numOfPredictions = 10;
  37.   var dbPath;
  38.  
  39.   List _recognitions;
  40.  
  41.   @override
  42.   void initState() {
  43.     super.initState();
  44.  
  45.     _busy = true;
  46.  
  47.     loadModel().then((val) {
  48.       setState(() {
  49.         _busy = false;
  50.       });
  51.     });
  52.   }
  53.  
  54.   loadModel() async {
  55.     Tflite.close();
  56.     try {
  57.      
  58.       String res;
  59.       res = await Tflite.loadModel(
  60.         model: "assets/tflite/mobilenet_v1_1.0_224.tflite",
  61.         labels: "assets/tflite/mobilenet_v1_1.0_224.txt",
  62.         numThreads: 1,
  63.       );
  64.       print(res);
  65.     } on PlatformException {
  66.       print("Failed to load the model");
  67.     }
  68.   }
  69.  
  70.   Future getImage() async {
  71.     // var image = await ImagePicker.pickImage(source: ImageSource.gallery);
  72.  
  73.     int startTime = new DateTime.now().millisecondsSinceEpoch;
  74.  
  75.     for (int i = 0; i < _numOfPredictions; i++)
  76.       await predictImageClassification();
  77.    
  78.     int endTime = new DateTime.now().millisecondsSinceEpoch;
  79.  
  80.     print("\n Object Detection took ${endTime - startTime}");
  81.     log("\n Object Detection took ${endTime - startTime}");
  82.     debugPrint("\n Object Detection took ${endTime - startTime}");
  83.    
  84.     setState(() {
  85.       _recTime = endTime - startTime;
  86.     });
  87.   }
  88.  
  89.   predictImageClassification() async {
  90.  
  91.     Directory directory = await getApplicationDocumentsDirectory();
  92.     var dbPath = join(directory.path, "skata.jpg");
  93.    
  94.     log("\n Path: ${directory.path}");
  95.  
  96.     var recognitions = await Tflite.runModelOnImage(
  97.       path: dbPath,
  98.       numResults: 6,
  99.       threshold: 0.05,
  100.       imageMean: 127.5,
  101.       imageStd: 127.5,
  102.     );
  103.  
  104.     setState(() {
  105.       _recognitions = recognitions;
  106.     });
  107.   }
  108.  
  109.   @override
  110.   Widget build(BuildContext context) {
  111.     Size size = MediaQuery.of(context).size;
  112.  
  113.     // used to stack boundery boxes on images
  114.     List<Widget> stackChildren = [];
  115.     stackChildren.add(Positioned(
  116.       top: 0.0,
  117.       left: 0.0,
  118.       width: size.width,
  119.       child: _image == null ? Text("No Image Selected") : Image.file(_image),
  120.     ));
  121.  
  122.     if (_busy) {
  123.       stackChildren.add(Center(
  124.         child: CircularProgressIndicator(),
  125.       ));
  126.     } else if (_recognitions != null){
  127.       stackChildren.add(Center(
  128.        // child: Text("HELLO ${_recognitions.label} : ${_recognitions.confidence}"),
  129.       ));
  130.     }
  131.  
  132.     return Scaffold(
  133.       appBar: AppBar(
  134.         title: Text("Symaskins appen"),
  135.       ),
  136.       floatingActionButton: FloatingActionButton(
  137.         child: Icon(Icons.image),
  138.         tooltip: "Pick Image from gallery",
  139.         onPressed: getImage,
  140.       ),
  141.       body: Container(
  142.         child: _recTime == null ? Text("hello maybe start first yes???") : Text("HELLO it took ${_recTime}ms (in int not float yes) to finish things! yes path: ")
  143.       ),
  144.     );
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement