Created
March 4, 2019 22:42
-
-
Save navarasu/056b71752b889b693052040665763576 to your computer and use it in GitHub Desktop.
object_detector for TF Lite object detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:camera/camera.dart'; | |
import 'package:flutter/services.dart'; | |
class ObjectDetecter extends ValueNotifier<List> { | |
ObjectDetecter._() : super(null); | |
static CameraController _controller; | |
static bool _isDetecting = false; | |
static const platform = const MethodChannel('francium.tech/tensorflow'); | |
static final ObjectDetecter instance = ObjectDetecter._(); | |
void init(CameraController controller) async { | |
_controller=controller; | |
_controller.initialize().then((_) { | |
_controller.startImageStream((CameraImage image) { | |
if (!_isDetecting) { | |
_isDetecting = true; | |
_runDetection(image); | |
} | |
}); | |
}); | |
} | |
void _runDetection(CameraImage image) async { | |
try { | |
var inputImage = new Map(); | |
inputImage["planes"]=new List(image.planes.length); | |
for (int i = 0; i < image.planes.length; i++) { | |
var value={}; | |
value["bytes"]=image.planes[i].bytes; | |
value["bytesPerRow"]=image.planes[i].bytesPerRow; | |
value["bytesPerPixel"]=image.planes[i].bytesPerPixel; | |
value["height"]=image.planes[i].height; | |
value["width"]=image.planes[i].width; | |
inputImage["planes"][i]=value; | |
} | |
inputImage["height"]=image.height; | |
inputImage["width"]=image.width; | |
inputImage["rotation"]=90; | |
var results = await platform.invokeMethod('detectObject',inputImage,); | |
value=results; | |
} finally { | |
_isDetecting = false; | |
} | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
suspend(); | |
} | |
void suspend() { | |
_controller?.dispose(); | |
_controller = null; | |
value = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment