Skip to content

Instantly share code, notes, and snippets.

@navarasu
Created March 4, 2019 22:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navarasu/056b71752b889b693052040665763576 to your computer and use it in GitHub Desktop.
Save navarasu/056b71752b889b693052040665763576 to your computer and use it in GitHub Desktop.
object_detector for TF Lite object detection
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