TFLite model from teachable machine wont recognize other class but the first one

In flutter, using flutter_tflite: ^1.0.1 with a model exported from teachable machine.

When I test it in the teachable machine interface the model by default recognizes everything as class 1, but it recognizes and changes to class 2 when I show it an image of it.

The issue here is basically that this class 1 tends to come out with nearly 100% "confidence" in flutter as in teachable machine it at least changes to 0% or so for class 1 when shown an object of class 2.

this would be my testing code:

import 'dart:math';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tflite/flutter_tflite.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:weeding_bot/models/object_model.dart';

class ScanController extends GetxController {
  @override
  void onInit() {
    super.onInit();
    initCamera();
    initTFLite();
  }

  @override
  void dispose() {
    super.dispose();
    cameraController.dispose();
  }

  List<ObjectModel> allObjects = [];
  late CameraController cameraController;
  late List<CameraDescription> cameras;

  var isCameraInitialized = false;
  var cameraCount = 0;
  var detectorBusy = false;
  initCamera() async {
    if (await Permission.camera.request().isGranted) {
      cameras = await availableCameras();
      cameraController = CameraController(cameras[0], ResolutionPreset.max);
      await cameraController.initialize().then((value) {
        cameraController.startImageStream((image) {
          cameraCount++;
          if (cameraCount % 10 == 0) {
            cameraCount = 0;
            objectDetector(image);
          }
        });
        update();
      });
      isCameraInitialized = true;
      update();
    } else {
      debugPrint("Permission to access camera is not granted by user");
    }
    update();
  }

  objectDetector(CameraImage frame) async {
    try {
      if (detectorBusy) {
        debugPrint("detector is busy, skiping");
        return;
      }
      detectorBusy = true;
      var detector = await Tflite.runModelOnFrame(
          bytesList: frame.planes.map((e) {
            return e.bytes;
          }).toList(),
          imageHeight: frame.height,
          imageWidth: frame.width,
          imageMean: 127.5,
          imageStd: 127.5,
          rotation: 0,
          numResults: 5,
          threshold: 0.1);

      if (detector != null) {
        debugPrint("result is $detector");
      } else {}
    } catch (e) {
      debugPrint("$e");
    } finally {
      detectorBusy = false;
    }
  }

  initTFLite() async {
    await Tflite.loadModel(
      model: "assets/trainedModel/weedDetector.tflite",
      labels: "assets/trainedModel/labels.txt",
    );
  }
}

Upvotes: 0

Views: 214

Answers (1)

Chathura Chamikara
Chathura Chamikara

Reputation: 114

I suggest you try tflite_flutter instead of flutter_tflite.

Upvotes: 0

Related Questions