beginning work on ui
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:image/image.dart' as image_lib;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:tflite_flutter/tflite_flutter.dart';
|
||||
import 'package:tflite_flutter_helper/tflite_flutter_helper.dart';
|
||||
|
||||
@@ -31,6 +33,7 @@ class Classifier {
|
||||
/// Labels file loaded as list
|
||||
late List<String> _labels;
|
||||
int classifierCreationStart = -1;
|
||||
bool _shouldReturnFrame = false;
|
||||
|
||||
Classifier(
|
||||
Interpreter interpreter, {
|
||||
@@ -67,6 +70,10 @@ class Classifier {
|
||||
}
|
||||
}
|
||||
|
||||
void setReturnFrame(bool returnFrame) {
|
||||
_shouldReturnFrame = returnFrame;
|
||||
}
|
||||
|
||||
/// Pre-process the image
|
||||
TensorImage? getProcessedImage(TensorImage? inputImage) {
|
||||
int cropSize = min(_inputImage.height, _inputImage.width);
|
||||
@@ -83,12 +90,14 @@ class Classifier {
|
||||
}
|
||||
|
||||
/// Runs object detection on the input image
|
||||
Map<String, dynamic>? predict(image_lib.Image image) {
|
||||
Future<Map<String, dynamic>?> predict(image_lib.Image image) async {
|
||||
var preProcStart = DateTime.now().millisecondsSinceEpoch;
|
||||
_inputImage.loadImage(image);
|
||||
_inputImage.loadImage(image_lib.copyRotate(image, 90));
|
||||
_inputImage = getProcessedImage(_inputImage)!;
|
||||
|
||||
|
||||
var inferenceStart = DateTime.now().millisecondsSinceEpoch;
|
||||
_interpreter.run(_inputImage.buffer, _outputBuffer.getBuffer());
|
||||
_interpreter.run(_inputImage.buffer, _outputBuffer.getBuffer());
|
||||
var postProcStart = DateTime.now().millisecondsSinceEpoch;
|
||||
Map<String, double> labeledProb =
|
||||
TensorLabel.fromList(labels, _outputProcessor.process(_outputBuffer))
|
||||
@@ -106,6 +115,7 @@ class Classifier {
|
||||
inferenceTime: postProcStart - inferenceStart,
|
||||
postProcessingTime: endTime - postProcStart,
|
||||
),
|
||||
'image': _shouldReturnFrame? _inputImage.image : null,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:tensordex_mobile/tflite/classifier.dart';
|
||||
import 'package:tflite_flutter/tflite_flutter.dart';
|
||||
|
||||
import '../utils/image_utils.dart';
|
||||
import '../utils/logger.dart';
|
||||
|
||||
class IsolateBase {
|
||||
final ReceivePort _receivePort = ReceivePort();
|
||||
@@ -25,6 +26,7 @@ class MLIsolate extends IsolateBase {
|
||||
_sendPort = await _receivePort.first;
|
||||
}
|
||||
|
||||
|
||||
static void entryPoint(SendPort sendPort) async {
|
||||
final port = ReceivePort();
|
||||
sendPort.send(port.sendPort);
|
||||
@@ -33,10 +35,15 @@ class MLIsolate extends IsolateBase {
|
||||
var cameraImage = mlIsolateData.cameraImage;
|
||||
var converted = ImageUtils.convertCameraImage(cameraImage);
|
||||
if (converted != null) {
|
||||
Classifier classifier = Classifier(
|
||||
var classifier = Classifier(
|
||||
Interpreter.fromAddress(mlIsolateData.interpreterAddress),
|
||||
labels: mlIsolateData.labels);
|
||||
var result = classifier.predict(converted);
|
||||
if (classifier.interpreter.address !=
|
||||
mlIsolateData.interpreterAddress) {
|
||||
logger.e('INTERPRETER ADDRESS MISMATCH!');
|
||||
}
|
||||
classifier.setReturnFrame(mlIsolateData.shouldSaveFrame);
|
||||
var result = await classifier.predict(converted);
|
||||
mlIsolateData.responsePort?.send(result);
|
||||
} else {
|
||||
mlIsolateData.responsePort?.send({'response': 'not working yet'});
|
||||
@@ -49,6 +56,7 @@ class MLIsolate extends IsolateBase {
|
||||
class MLIsolateData {
|
||||
CameraImage cameraImage;
|
||||
int interpreterAddress;
|
||||
bool shouldSaveFrame;
|
||||
List<String> labels;
|
||||
SendPort? responsePort;
|
||||
|
||||
@@ -56,5 +64,6 @@ class MLIsolateData {
|
||||
this.cameraImage,
|
||||
this.interpreterAddress,
|
||||
this.labels,
|
||||
this.shouldSaveFrame,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user