{"id":13744353,"url":"https://github.com/shaqian/flutter_tflite","last_synced_at":"2025-05-15T12:04:59.369Z","repository":{"id":40459856,"uuid":"150034328","full_name":"shaqian/flutter_tflite","owner":"shaqian","description":"Flutter plugin for TensorFlow Lite","archived":false,"fork":false,"pushed_at":"2024-06-05T15:59:17.000Z","size":66370,"stargazers_count":636,"open_issues_count":194,"forks_count":422,"subscribers_count":22,"default_branch":"master","last_synced_at":"2025-04-14T19:01:16.224Z","etag":null,"topics":["dart","flutter","tensorflow","tensorflowlite"],"latest_commit_sha":null,"homepage":"https://pub.dartlang.org/packages/tflite","language":"Objective-C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shaqian.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-23T23:29:03.000Z","updated_at":"2025-04-04T14:52:24.000Z","dependencies_parsed_at":"2024-06-18T22:57:23.340Z","dependency_job_id":null,"html_url":"https://github.com/shaqian/flutter_tflite","commit_stats":{"total_commits":70,"total_committers":18,"mean_commits":3.888888888888889,"dds":0.7142857142857143,"last_synced_commit":"ce8075a391e02e0cc0bc6c2db950aa961a17d745"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaqian%2Fflutter_tflite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaqian%2Fflutter_tflite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaqian%2Fflutter_tflite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shaqian%2Fflutter_tflite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shaqian","download_url":"https://codeload.github.com/shaqian/flutter_tflite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337612,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","flutter","tensorflow","tensorflowlite"],"created_at":"2024-08-03T05:01:07.872Z","updated_at":"2025-05-15T12:04:54.344Z","avatar_url":"https://github.com/shaqian.png","language":"Objective-C++","funding_links":[],"categories":["Objective-C++"],"sub_categories":[],"readme":"# tflite\n\nA Flutter plugin for accessing TensorFlow Lite API. Supports image classification, object detection ([SSD](https://github.com/tensorflow/models/tree/master/research/object_detection) and [YOLO](https://pjreddie.com/darknet/yolov2/)), [Pix2Pix](https://phillipi.github.io/pix2pix/) and [Deeplab](https://github.com/tensorflow/models/tree/master/research/deeplab) and [PoseNet](https://www.tensorflow.org/lite/models/pose_estimation/overview) on both iOS and Android.\n\n### Table of Contents\n\n- [Installation](#Installation)\n- [Usage](#Usage)\n    - [Image Classification](#Image-Classification)\n    - [Object Detection](#Object-Detection)\n      - [SSD MobileNet](#SSD-MobileNet)\n      - [YOLO](#Tiny-YOLOv2)\n    - [Pix2Pix](#Pix2Pix)\n    - [Deeplab](#Deeplab)\n    - [PoseNet](#PoseNet)\n- [Example](#Example)\n    - [Prediction in Static Images](#Prediction-in-Static-Images)\n    - [Real-time Detection](#Real-time-Detection)\n\n### Breaking changes\n\n#### Since 1.1.0:\n\n1. iOS TensorFlow Lite library is upgraded from TensorFlowLite　1.x to TensorFlowLiteObjC　2.x. Changes to native code are denoted with `TFLITE2`.\n\n#### Since 1.0.0:\n\n1. Updated to TensorFlow Lite API v1.12.0.\n2. No longer accepts parameter `inputSize` and `numChannels`. They will be retrieved from input tensor.\n3. `numThreads` is moved to `Tflite.loadModel`.\n\n## Installation\n\nAdd `tflite` as a [dependency in your pubspec.yaml file](https://flutter.io/using-packages/).\n\n### Android\n\nIn `android/app/build.gradle`, add the following setting in `android` block.\n\n```\n    aaptOptions {\n        noCompress 'tflite'\n        noCompress 'lite'\n    }\n```\n\n### iOS\n\nSolutions to build errors on iOS:\n\n* 'vector' file not found\"\n\n  Open `ios/Runner.xcworkspace` in Xcode, click Runner \u003e Tagets \u003e Runner \u003e Build Settings, search `Compile Sources As`, change the value to `Objective-C++`\n\n* 'tensorflow/lite/kernels/register.h' file not found\n\n  The plugin assumes the tensorflow header files are located in path \"tensorflow/lite/kernels\".\n\n  However, for early versions of tensorflow the header path is \"tensorflow/contrib/lite/kernels\".\n\n  Use `CONTRIB_PATH` to toggle the path. Uncomment `//#define CONTRIB_PATH` from here:\n  https://github.com/shaqian/flutter_tflite/blob/master/ios/Classes/TflitePlugin.mm#L1\n\n## Usage\n\n1. Create a `assets` folder and place your label file and model file in it. In `pubspec.yaml` add:\n\n```\n  assets:\n   - assets/labels.txt\n   - assets/mobilenet_v1_1.0_224.tflite\n```\n\n2. Import the library:\n\n```dart\nimport 'package:tflite/tflite.dart';\n```\n\n3. Load the model and labels:\n\n```dart\nString res = await Tflite.loadModel(\n  model: \"assets/mobilenet_v1_1.0_224.tflite\",\n  labels: \"assets/labels.txt\",\n  numThreads: 1, // defaults to 1\n  isAsset: true, // defaults to true, set to false to load resources outside assets\n  useGpuDelegate: false // defaults to false, set to true to use GPU delegate\n);\n```\n\n4. See the section for the respective model below.\n\n5. Release resources:\n\n```\nawait Tflite.close();\n```\n\n### GPU Delegate\n\nWhen using GPU delegate, refer to [this step](https://www.tensorflow.org/lite/performance/gpu#step_5_release_mode) for release mode setting to get better performance. \n\n### Image Classification\n\n- Output format:\n```\n{\n  index: 0,\n  label: \"person\",\n  confidence: 0.629\n}\n```\n\n- Run on image:\n\n```dart\nvar recognitions = await Tflite.runModelOnImage(\n  path: filepath,   // required\n  imageMean: 0.0,   // defaults to 117.0\n  imageStd: 255.0,  // defaults to 1.0\n  numResults: 2,    // defaults to 5\n  threshold: 0.2,   // defaults to 0.1\n  asynch: true      // defaults to true\n);\n```\n\n- Run on binary:\n\n```dart\nvar recognitions = await Tflite.runModelOnBinary(\n  binary: imageToByteListFloat32(image, 224, 127.5, 127.5),// required\n  numResults: 6,    // defaults to 5\n  threshold: 0.05,  // defaults to 0.1\n  asynch: true      // defaults to true\n);\n\nUint8List imageToByteListFloat32(\n    img.Image image, int inputSize, double mean, double std) {\n  var convertedBytes = Float32List(1 * inputSize * inputSize * 3);\n  var buffer = Float32List.view(convertedBytes.buffer);\n  int pixelIndex = 0;\n  for (var i = 0; i \u003c inputSize; i++) {\n    for (var j = 0; j \u003c inputSize; j++) {\n      var pixel = image.getPixel(j, i);\n      buffer[pixelIndex++] = (img.getRed(pixel) - mean) / std;\n      buffer[pixelIndex++] = (img.getGreen(pixel) - mean) / std;\n      buffer[pixelIndex++] = (img.getBlue(pixel) - mean) / std;\n    }\n  }\n  return convertedBytes.buffer.asUint8List();\n}\n\nUint8List imageToByteListUint8(img.Image image, int inputSize) {\n  var convertedBytes = Uint8List(1 * inputSize * inputSize * 3);\n  var buffer = Uint8List.view(convertedBytes.buffer);\n  int pixelIndex = 0;\n  for (var i = 0; i \u003c inputSize; i++) {\n    for (var j = 0; j \u003c inputSize; j++) {\n      var pixel = image.getPixel(j, i);\n      buffer[pixelIndex++] = img.getRed(pixel);\n      buffer[pixelIndex++] = img.getGreen(pixel);\n      buffer[pixelIndex++] = img.getBlue(pixel);\n    }\n  }\n  return convertedBytes.buffer.asUint8List();\n}\n```\n\n- Run on image stream (video frame):\n\n\u003e Works with [camera plugin 4.0.0](https://pub.dartlang.org/packages/camera). Video format: (iOS) kCVPixelFormatType_32BGRA, (Android) YUV_420_888.\n\n```dart\nvar recognitions = await Tflite.runModelOnFrame(\n  bytesList: img.planes.map((plane) {return plane.bytes;}).toList(),// required\n  imageHeight: img.height,\n  imageWidth: img.width,\n  imageMean: 127.5,   // defaults to 127.5\n  imageStd: 127.5,    // defaults to 127.5\n  rotation: 90,       // defaults to 90, Android only\n  numResults: 2,      // defaults to 5\n  threshold: 0.1,     // defaults to 0.1\n  asynch: true        // defaults to true\n);\n```\n\n### Object Detection\n\n- Output format:\n\n`x, y, w, h` are between [0, 1]. You can scale `x, w` by the width and `y, h` by the height of the image.\n\n```\n{\n  detectedClass: \"hot dog\",\n  confidenceInClass: 0.123,\n  rect: {\n    x: 0.15,\n    y: 0.33,\n    w: 0.80,\n    h: 0.27\n  }\n}\n```\n\n#### SSD MobileNet:\n\n- Run on image:\n\n```dart\nvar recognitions = await Tflite.detectObjectOnImage(\n  path: filepath,       // required\n  model: \"SSDMobileNet\",\n  imageMean: 127.5,     \n  imageStd: 127.5,      \n  threshold: 0.4,       // defaults to 0.1\n  numResultsPerClass: 2,// defaults to 5\n  asynch: true          // defaults to true\n);\n```\n\n- Run on binary:\n\n```dart\nvar recognitions = await Tflite.detectObjectOnBinary(\n  binary: imageToByteListUint8(resizedImage, 300), // required\n  model: \"SSDMobileNet\",  \n  threshold: 0.4,                                  // defaults to 0.1\n  numResultsPerClass: 2,                           // defaults to 5\n  asynch: true                                     // defaults to true\n);\n```\n\n- Run on image stream (video frame):\n\n\u003e Works with [camera plugin 4.0.0](https://pub.dartlang.org/packages/camera). Video format: (iOS) kCVPixelFormatType_32BGRA, (Android) YUV_420_888.\n\n```dart\nvar recognitions = await Tflite.detectObjectOnFrame(\n  bytesList: img.planes.map((plane) {return plane.bytes;}).toList(),// required\n  model: \"SSDMobileNet\",  \n  imageHeight: img.height,\n  imageWidth: img.width,\n  imageMean: 127.5,   // defaults to 127.5\n  imageStd: 127.5,    // defaults to 127.5\n  rotation: 90,       // defaults to 90, Android only\n  numResults: 2,      // defaults to 5\n  threshold: 0.1,     // defaults to 0.1\n  asynch: true        // defaults to true\n);\n```\n\n#### Tiny YOLOv2:\n\n- Run on image:\n\n```dart\nvar recognitions = await Tflite.detectObjectOnImage(\n  path: filepath,       // required\n  model: \"YOLO\",      \n  imageMean: 0.0,       \n  imageStd: 255.0,      \n  threshold: 0.3,       // defaults to 0.1\n  numResultsPerClass: 2,// defaults to 5\n  anchors: anchors,     // defaults to [0.57273,0.677385,1.87446,2.06253,3.33843,5.47434,7.88282,3.52778,9.77052,9.16828]\n  blockSize: 32,        // defaults to 32\n  numBoxesPerBlock: 5,  // defaults to 5\n  asynch: true          // defaults to true\n);\n```\n\n- Run on binary:\n\n```dart\nvar recognitions = await Tflite.detectObjectOnBinary(\n  binary: imageToByteListFloat32(resizedImage, 416, 0.0, 255.0), // required\n  model: \"YOLO\",  \n  threshold: 0.3,       // defaults to 0.1\n  numResultsPerClass: 2,// defaults to 5\n  anchors: anchors,     // defaults to [0.57273,0.677385,1.87446,2.06253,3.33843,5.47434,7.88282,3.52778,9.77052,9.16828]\n  blockSize: 32,        // defaults to 32\n  numBoxesPerBlock: 5,  // defaults to 5\n  asynch: true          // defaults to true\n);\n```\n\n- Run on image stream (video frame):\n\n\u003e Works with [camera plugin 4.0.0](https://pub.dartlang.org/packages/camera). Video format: (iOS) kCVPixelFormatType_32BGRA, (Android) YUV_420_888.\n\n```dart\nvar recognitions = await Tflite.detectObjectOnFrame(\n  bytesList: img.planes.map((plane) {return plane.bytes;}).toList(),// required\n  model: \"YOLO\",  \n  imageHeight: img.height,\n  imageWidth: img.width,\n  imageMean: 0,         // defaults to 127.5\n  imageStd: 255.0,      // defaults to 127.5\n  numResults: 2,        // defaults to 5\n  threshold: 0.1,       // defaults to 0.1\n  numResultsPerClass: 2,// defaults to 5\n  anchors: anchors,     // defaults to [0.57273,0.677385,1.87446,2.06253,3.33843,5.47434,7.88282,3.52778,9.77052,9.16828]\n  blockSize: 32,        // defaults to 32\n  numBoxesPerBlock: 5,  // defaults to 5\n  asynch: true          // defaults to true\n);\n```\n\n### Pix2Pix\n\n\u003e Thanks to [RP](https://github.com/shaqian/flutter_tflite/pull/18) from [Green Appers](https://github.com/GreenAppers)\n\n- Output format:\n  \n  The output of Pix2Pix inference is Uint8List type. Depending on the `outputType` used, the output is:\n\n  - (if outputType is png) byte array of a png image \n\n  - (otherwise) byte array of the raw output\n\n- Run on image:\n\n```dart\nvar result = await runPix2PixOnImage(\n  path: filepath,       // required\n  imageMean: 0.0,       // defaults to 0.0\n  imageStd: 255.0,      // defaults to 255.0\n  asynch: true      // defaults to true\n);\n```\n\n- Run on binary:\n\n```dart\nvar result = await runPix2PixOnBinary(\n  binary: binary,       // required\n  asynch: true      // defaults to true\n);\n```\n\n- Run on image stream (video frame):\n\n```dart\nvar result = await runPix2PixOnFrame(\n  bytesList: img.planes.map((plane) {return plane.bytes;}).toList(),// required\n  imageHeight: img.height, // defaults to 1280\n  imageWidth: img.width,   // defaults to 720\n  imageMean: 127.5,   // defaults to 0.0\n  imageStd: 127.5,    // defaults to 255.0\n  rotation: 90,       // defaults to 90, Android only\n  asynch: true        // defaults to true\n);\n```\n\n### Deeplab\n\n\u003e Thanks to [RP](https://github.com/shaqian/flutter_tflite/pull/22) from [see--](https://github.com/see--) for Android implementation.\n\n- Output format:\n  \n  The output of Deeplab inference is Uint8List type. Depending on the `outputType` used, the output is:\n\n  - (if outputType is png) byte array of a png image \n\n  - (otherwise) byte array of r, g, b, a values of the pixels \n\n- Run on image:\n\n```dart\nvar result = await runSegmentationOnImage(\n  path: filepath,     // required\n  imageMean: 0.0,     // defaults to 0.0\n  imageStd: 255.0,    // defaults to 255.0\n  labelColors: [...], // defaults to https://github.com/shaqian/flutter_tflite/blob/master/lib/tflite.dart#L219\n  outputType: \"png\",  // defaults to \"png\"\n  asynch: true        // defaults to true\n);\n```\n\n- Run on binary:\n\n```dart\nvar result = await runSegmentationOnBinary(\n  binary: binary,     // required\n  labelColors: [...], // defaults to https://github.com/shaqian/flutter_tflite/blob/master/lib/tflite.dart#L219\n  outputType: \"png\",  // defaults to \"png\"\n  asynch: true        // defaults to true\n);\n```\n\n- Run on image stream (video frame):\n\n```dart\nvar result = await runSegmentationOnFrame(\n  bytesList: img.planes.map((plane) {return plane.bytes;}).toList(),// required\n  imageHeight: img.height, // defaults to 1280\n  imageWidth: img.width,   // defaults to 720\n  imageMean: 127.5,        // defaults to 0.0\n  imageStd: 127.5,         // defaults to 255.0\n  rotation: 90,            // defaults to 90, Android only\n  labelColors: [...],      // defaults to https://github.com/shaqian/flutter_tflite/blob/master/lib/tflite.dart#L219\n  outputType: \"png\",       // defaults to \"png\"\n  asynch: true             // defaults to true\n);\n```\n\n### PoseNet\n\n\u003e Model is from [StackOverflow thread](https://stackoverflow.com/a/55288616).\n\n- Output format:\n\n`x, y` are between [0, 1]. You can scale `x` by the width and `y` by the height of the image.\n\n```\n[ // array of poses/persons\n  { // pose #1\n    score: 0.6324902,\n    keypoints: {\n      0: {\n        x: 0.250,\n        y: 0.125,\n        part: nose,\n        score: 0.9971070\n      },\n      1: {\n        x: 0.230,\n        y: 0.105,\n        part: leftEye,\n        score: 0.9978438\n      }\n      ......\n    }\n  },\n  { // pose #2\n    score: 0.32534285,\n    keypoints: {\n      0: {\n        x: 0.402,\n        y: 0.538,\n        part: nose,\n        score: 0.8798978\n      },\n      1: {\n        x: 0.380,\n        y: 0.513,\n        part: leftEye,\n        score: 0.7090239\n      }\n      ......\n    }\n  },\n  ......\n]\n```\n\n- Run on image:\n\n```dart\nvar result = await runPoseNetOnImage(\n  path: filepath,     // required\n  imageMean: 125.0,   // defaults to 125.0\n  imageStd: 125.0,    // defaults to 125.0\n  numResults: 2,      // defaults to 5\n  threshold: 0.7,     // defaults to 0.5\n  nmsRadius: 10,      // defaults to 20\n  asynch: true        // defaults to true\n);\n```\n\n- Run on binary:\n\n```dart\nvar result = await runPoseNetOnBinary(\n  binary: binary,     // required\n  numResults: 2,      // defaults to 5\n  threshold: 0.7,     // defaults to 0.5\n  nmsRadius: 10,      // defaults to 20\n  asynch: true        // defaults to true\n);\n```\n\n- Run on image stream (video frame):\n\n```dart\nvar result = await runPoseNetOnFrame(\n  bytesList: img.planes.map((plane) {return plane.bytes;}).toList(),// required\n  imageHeight: img.height, // defaults to 1280\n  imageWidth: img.width,   // defaults to 720\n  imageMean: 125.0,        // defaults to 125.0\n  imageStd: 125.0,         // defaults to 125.0\n  rotation: 90,            // defaults to 90, Android only\n  numResults: 2,           // defaults to 5\n  threshold: 0.7,          // defaults to 0.5\n  nmsRadius: 10,           // defaults to 20\n  asynch: true             // defaults to true\n);\n```\n\n## Example\n\n### Prediction in Static Images\n\n  Refer to the [example](https://github.com/shaqian/flutter_tflite/tree/master/example).\n\n### Real-time detection\n\n  Refer to [flutter_realtime_Detection](https://github.com/shaqian/flutter_realtime_detection).\n\n## Run test cases\n\n`flutter test test/tflite_test.dart`","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaqian%2Fflutter_tflite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshaqian%2Fflutter_tflite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshaqian%2Fflutter_tflite/lists"}