Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runModelOnImage returns null #280

Open
akaanuzman opened this issue May 28, 2023 · 1 comment
Open

runModelOnImage returns null #280

akaanuzman opened this issue May 28, 2023 · 1 comment

Comments

@akaanuzman
Copy link

akaanuzman commented May 28, 2023

I am getting null as a result of runModelOnImage method. Do you have a solution? Can you help me?

// ignore_for_file: depend_on_referenced_packages
import 'dart:io';
import 'dart:math' hide log;

import 'package:tflite/tflite.dart';
import 'package:flutter/material.dart';
import 'dart:developer';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

class FoodReccomendationViewModel extends ChangeNotifier {
late String? response;
List results = [];

Future loadAIModelFromAsset() async {
Tflite.close();
response = await Tflite.loadModel(
model: "assets/ai/age_gender_estimation.tflite",
labels: "assets/ai/age_gender_labels.txt",
numThreads: 2,
);
log("Model loaded: $response");
}

Future disposeModel() async {
await Tflite.close();
}

Future imageClassification(String imageUrl) async {
File file = await _fileFromImageUrl(imageUrl);
log(file.path);
try {

  var recognitions = await Tflite.runModelOnImage(
    path: file.path,
  );
  results = recognitions ?? [];
  notifyListeners();
  log("Results is: $results");
  log("Recognitions is: $recognitions");

} catch (e) {
  log(e.toString());
}

}

Future _fileFromImageUrl(String imageUrl) async {
final response = await http.get(Uri.parse(imageUrl));
final documentDirectory = await getApplicationDocumentsDirectory();
String randomNumber = (Random().nextInt(899999) + 100000).toString();
final file = File(join(documentDirectory.path, '$randomNumber.jpg'));
file.writeAsBytesSync(response.bodyBytes);
return file;
}
}

DEBUG CONSOLE:
D/EGL_emulation( 1990): app_time_stats: avg=88.81ms min=2.92ms max=3563.28ms count=46
I/tflite ( 1990): Replacing 16 node(s) with delegate (TfLiteXNNPackDelegate) node, yielding 1 partitions for the whole graph.
[log] Model loaded: success
D/EGL_emulation( 1990): app_time_stats: avg=20.34ms min=2.89ms max=760.86ms count=56
[log] /data/user/0/com.example.digital_order_system/app_flutter/391694.jpg
V/time ( 1990): Inference took 29
[log] Results is: []
[log] Recognitions is: []

@dequilla3
Copy link

recheck your file path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants