Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
duy-maimanh committed Feb 7, 2024
1 parent 43230bc commit 55e04ba
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
26 changes: 16 additions & 10 deletions example/movinet/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
# movinet
# Action Recognition Movinet

A new Flutter project.
| | Android | iOS | Linux | Mac | Windows | Web |
|------|---------|-----|-------|-----|---------|-----|
| live ||| | | | |

## Getting Started
This project is a sample of how to perform action recognition using
TensorFlow Lite in Flutter. It includes support for both Android and IOS.

This project is a starting point for a Flutter application.
## Download model and labels

A few resources to get you started if this is your first Flutter project:
To build the project, you must first download the Movinet TensorFlow Lite
model. You can do this by access https://www.kaggle.com/models/google/movinet
and download movinet model. After downloading the model, you must copy the model
into the `assets` folder.

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
## About the sample

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
- You can use Flutter-supported IDEs such as Android Studio or Visual Studio.
This project has been tested on Android Studio Flamingo.
- Before building, ensure that you have downloaded the model and the labels by
following a set of instructions.
Binary file added example/movinet/assets/images/tfl_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions example/movinet/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>NSCameraUsageDescription</key>
<string>Camera Permission</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UISupportedInterfaceOrientations</key>
Expand Down
5 changes: 4 additions & 1 deletion example/movinet/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text(widget.title)),
title: Image.asset(
'assets/images/tfl_logo.png',
fit: BoxFit.contain,
),
backgroundColor: Colors.black.withOpacity(0.5),
),
body: resultWidget(context),
Expand Down
3 changes: 2 additions & 1 deletion example/movinet/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ flutter:

# To add assets to your application, add an assets section, like this:
assets:
- assets/movinet_int8.tflite
- assets/
- assets/label/kinetics600_label_map.txt
- assets/images/tfl_logo.png

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
Expand Down
80 changes: 31 additions & 49 deletions lib/src/interpreter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,14 @@ class Interpreter {
inputTensors.elementAt(i).setTo(inputs[i]);
}

var inferenceStartNanos = DateTime.now().microsecondsSinceEpoch;
var inferenceStartNanos = DateTime
.now()
.microsecondsSinceEpoch;
invoke();
_lastNativeInferenceDurationMicroSeconds =
DateTime.now().microsecondsSinceEpoch - inferenceStartNanos;
DateTime
.now()
.microsecondsSinceEpoch - inferenceStartNanos;
}

/// Gets all input tensors associated with the model.
Expand All @@ -226,8 +230,9 @@ class Interpreter {

var tensors = List.generate(
tfliteBinding.TfLiteInterpreterGetInputTensorCount(_interpreter),
(i) => Tensor(
tfliteBinding.TfLiteInterpreterGetInputTensor(_interpreter, i)),
(i) =>
Tensor(
tfliteBinding.TfLiteInterpreterGetInputTensor(_interpreter, i)),
growable: false);

return tensors;
Expand All @@ -241,8 +246,10 @@ class Interpreter {

var tensors = List.generate(
tfliteBinding.TfLiteInterpreterGetOutputTensorCount(_interpreter),
(i) => Tensor(
tfliteBinding.TfLiteInterpreterGetOutputTensor(_interpreter, i)),
(i) =>
Tensor(
tfliteBinding.TfLiteInterpreterGetOutputTensor(
_interpreter, i)),
growable: false);

return tensors;
Expand All @@ -253,7 +260,7 @@ class Interpreter {
final dimensionSize = shape.length;
final dimensions = calloc<Int>(dimensionSize);
final externalTypedData =
dimensions.cast<Int32>().asTypedList(dimensionSize);
dimensions.cast<Int32>().asTypedList(dimensionSize);
externalTypedData.setRange(0, dimensionSize, shape);
final status = tfliteBinding.TfLiteInterpreterResizeInputTensor(
_interpreter, tensorIndex, dimensions, dimensionSize);
Expand Down Expand Up @@ -341,7 +348,7 @@ class Interpreter {
// check if signature key exists
if (!_signatureRunners.containsKey(signatureKey)) {
Pointer<Char> signatureKeyPointer =
signatureKey.toNativeUtf8() as Pointer<Char>;
signatureKey.toNativeUtf8() as Pointer<Char>;
final signatureRunner = tfliteBinding.TfLiteInterpreterGetSignatureRunner(
_interpreter, signatureKeyPointer);
_signatureRunners[signatureKey] = signatureRunner;
Expand All @@ -355,23 +362,23 @@ class Interpreter {
int getSignatureInputCount(String signatureKey) {
final signatureRunner = _getSignatureRunner(signatureKey);
final subGraphIndex =
tfliteBinding.TfLiteSignatureRunnerGetInputCount(signatureRunner);
tfliteBinding.TfLiteSignatureRunnerGetInputCount(signatureRunner);
return subGraphIndex;
}

/// Get the number of outputs associated with a signature
int getSignatureOutputCount(String signatureKey) {
final signatureRunner = _getSignatureRunner(signatureKey);
final subGraphIndex =
tfliteBinding.TfLiteSignatureRunnerGetOutputCount(signatureRunner);
tfliteBinding.TfLiteSignatureRunnerGetOutputCount(signatureRunner);
return subGraphIndex;
}

/// Get input name by index and signature key.
String getSignatureInputName(String signatureKey, int index) {
final signatureRunner = _getSignatureRunner(signatureKey);
final inputName =
tfliteBinding.TfLiteSignatureRunnerGetInputName(signatureRunner, index);
tfliteBinding.TfLiteSignatureRunnerGetInputName(signatureRunner, index);
return inputName.cast<Utf8>().toDartString();
}

Expand All @@ -383,8 +390,8 @@ class Interpreter {
return outputName.cast<Utf8>().toDartString();
}

List<int> getSignatureInputTensorShape(
String signatureKey, String inputName) {
List<int> getSignatureInputTensorShape(String signatureKey,
String inputName) {
final signatureRunner = _getSignatureRunner(signatureKey);
final inputTensor = Tensor(
tfliteBinding.TfLiteSignatureRunnerGetInputTensor(
Expand All @@ -393,8 +400,8 @@ class Interpreter {
return shape;
}

List<int> getSignatureOutputTensorShape(
String signatureKey, String outputName) {
List<int> getSignatureOutputTensorShape(String signatureKey,
String outputName) {
final signatureRunner = _getSignatureRunner(signatureKey);
final outputTensor = Tensor(
tfliteBinding.TfLiteSignatureRunnerGetOutputTensor(
Expand All @@ -403,32 +410,6 @@ class Interpreter {
return shape;
}

/// get signature input tensor
Map<String, Tensor> getSignatureInputTensors(
String signatureKey, List<String> keys) {
final signatureRunner = _getSignatureRunner(signatureKey);
Map<String, Tensor> tensors = HashMap();
keys.forEach((key) {
tensors[key] = Tensor(tfliteBinding.TfLiteSignatureRunnerGetInputTensor(
signatureRunner, key.toNativeUtf8().cast<Char>()));
});

return tensors;
}

/// get signature output tensor
Map<String, Tensor> getSignatureOutputTensors(
String signatureKey, List<String> keys) {
final signatureRunner = _getSignatureRunner(signatureKey);
Map<String, Tensor> tensors = HashMap();
keys.forEach((key) {
tensors[key] = Tensor(tfliteBinding.TfLiteSignatureRunnerGetOutputTensor(
signatureRunner, key.toNativeUtf8().cast<Char>()));
});

return tensors;
}

/// Run for single input and output
void runSignature(Map<String, Object> inputs, Map<String, Object> outputs,
String signatureKey) {
Expand All @@ -440,21 +421,22 @@ class Interpreter {
}

final Pointer<TfLiteSignatureRunner> signatureRunner =
_getSignatureRunner(signatureKey);

var inputTensor =
getSignatureInputTensors(signatureKey, inputs.keys.toList());
_getSignatureRunner(signatureKey);

inputs.forEach((key, value) {
inputTensor[key]?.setTo(value);
Tensor inputTensor = Tensor(
tfliteBinding.TfLiteSignatureRunnerGetInputTensor(
signatureRunner, key.toNativeUtf8().cast<Char>()));
inputTensor.setTo(value);
});

tfliteBinding.TfLiteSignatureRunnerInvoke(signatureRunner);

var outputTensor =
getSignatureOutputTensors(signatureKey, outputs.keys.toList());
outputs.forEach((key, value) {
outputTensor[key]?.copyTo(value);
Tensor outputTensor = Tensor(
tfliteBinding.TfLiteSignatureRunnerGetOutputTensor(
signatureRunner, key.toNativeUtf8().cast<Char>()));
outputTensor.copyTo(value);
});
}

Expand Down

0 comments on commit 55e04ba

Please sign in to comment.