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

Crashing #5

Open
aliosmnoz opened this issue Apr 7, 2022 · 7 comments
Open

Crashing #5

aliosmnoz opened this issue Apr 7, 2022 · 7 comments

Comments

@aliosmnoz
Copy link

D/TAG (12566): onMethodCall: initNsfw
I/tflite (12566): Created TensorFlow Lite delegate for GPU.
D/io.github.devzwy.nsfw.NSFWHelper(12566): 尝试从传入的模型路径读取模型
I/tflite (12566): Initialized TensorFlow Lite runtime.
D/io.github.devzwy.nsfw.NSFWHelper(12566): 模型加载成功!
D/io.github.devzwy.nsfw.NSFWHelper(12566): NSFWHelper初始化成功!GPU加速已成功开启
D/TAG (12566): onMethodCall: getPhotoNSFWScore
E/AndroidRuntime(12566): FATAL EXCEPTION: DefaultDispatcher-worker-1
E/AndroidRuntime(12566): Process: com.strapps.closer, PID: 12566
E/AndroidRuntime(12566): java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 150528 bytes and a Java Buffer with 602112 bytes.
E/AndroidRuntime(12566): at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:332)
E/AndroidRuntime(12566): at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:305)
E/AndroidRuntime(12566): at org.tensorflow.lite.Tensor.setTo(Tensor.java:123)
E/AndroidRuntime(12566): at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:150)
E/AndroidRuntime(12566): at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:311)
E/AndroidRuntime(12566): at org.tensorflow.lite.Interpreter.run(Interpreter.java:272)
E/AndroidRuntime(12566): at io.github.devzwy.nsfw.NSFWHelper.getNSFWScore(NSFWHelper.kt:227)
E/AndroidRuntime(12566): at io.github.devzwy.nsfw.NSFWHelper$getNSFWScore$1.invokeSuspend(NSFWHelper.kt:173)
E/AndroidRuntime(12566): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E/AndroidRuntime(12566): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
E/AndroidRuntime(12566): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
E/AndroidRuntime(12566): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
E/AndroidRuntime(12566): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
E/AndroidRuntime(12566): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
I/Process (12566): Sending signal. PID: 12566 SIG: 9
Lost connection to device.

@aliosmnoz
Copy link
Author

And i tried
aaptOptions {
noCompress 'tflite'
noCompress 'lite'
}
in android/app/build.gradle but nothing changes still crashing.

@ahsanalidev
Copy link
Owner

Sorry didn't noticed the issues of the repo are you still facing the issue?

@ahsanalidev
Copy link
Owner

Can you share your code?

@aliosmnoz
Copy link
Author

Here is my codes
`import 'dart:io' show Directory, File;
import 'package:closer/helpers/app_localizations.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:flutter_nsfw/flutter_nsfw.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';

class NSFWDetector {
NSFWDetector(this.modelPath, this.enableLog, this.isOpenGPU, this.numThreads);

final String modelPath;
final bool enableLog;
final bool isOpenGPU;
final int numThreads;

bool isInitialized = false;

Future detectInPhoto(String photoPath) async {
if (!isInitialized) {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
var file = File(appDocPath + "/nsfw.tflite");
if (!file.existsSync()) {
var data = await rootBundle.load("assets/nsfw.tflite");
final buffer = data.buffer;
await file.writeAsBytes(
buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
await FlutterNsfw.initNsfw(
file.path,
);
isInitialized = true;
}

return FlutterNsfw.getPhotoNSFWScore(photoPath);

}
}

class ImageSourceSheet extends StatelessWidget {
NSFWDetector _nsfwDetector =
NSFWDetector('assets/nsfw.tflite', true, true, 2);
Future detectNSFWImage(String photo) async {
final nsfwStatus = await _nsfwDetector.detectInPhoto(photo);
if (nsfwStatus > 0.80) {
return true;
} else {
return false;
}
}

// Constructor
ImageSourceSheet({required this.onImageSelected});

// Callback function to return image file
final Function(File?) onImageSelected;
// ImagePicker instance
final picker = ImagePicker();

bool _isNSFW = false;

Future selectedImage(BuildContext context, File? image) async {
// init i18n
final i18n = AppLocalizations.of(context);

// Check file
if (image != null) {
  final croppedImage = await ImageCropper.cropImage(
      sourcePath: image.path,
      aspectRatioPresets: [CropAspectRatioPreset.square],
      maxWidth: 400,
      maxHeight: 400,
      androidUiSettings: AndroidUiSettings(
        toolbarTitle: i18n.translate("edit_crop_image"),
        toolbarColor: Theme.of(context).primaryColor,
        toolbarWidgetColor: Colors.white,
      ));
  _isNSFW = await detectNSFWImage(croppedImage!.path);
  if (_isNSFW == false) {
    onImageSelected(croppedImage);
  }
}

}

@OverRide
Widget build(BuildContext context) {
final i18n = AppLocalizations.of(context);
return BottomSheet(
onClosing: () {},
builder: ((context) => Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
/// Select image from gallery
SizedBox (
height: 75,
child: TextButton.icon(
icon: Icon(Icons.photo_library_outlined, color: Colors.grey, size: 27),
label: Text(i18n.translate("gallery"),
style: TextStyle(fontSize: 16,color: Theme.of(context).primaryColor)),
onPressed: () async {
// Get image from device gallery
final pickedFile = await picker.getImage(
source: ImageSource.gallery,
);
if (pickedFile == null) return;
selectedImage(context, File(pickedFile.path));
},
),
),
SizedBox(
width: MediaQuery.of(context).size.width / 4,
),
/// Capture image from camera
SizedBox (
height: 75,
child: TextButton.icon(
icon: Icon(Icons.camera_alt_outlined, color: Colors.grey, size: 27),
label: Text(i18n.translate("camera"),
style: TextStyle(fontSize: 16,color: Theme.of(context).primaryColor)),
onPressed: () async {
// Capture image from camera
final pickedFile = await picker.getImage(
source: ImageSource.camera,
);
if (pickedFile == null) return;
selectedImage(context, File(pickedFile.path));
},
),
),
],
)));
}
}
`

@aliosmnoz
Copy link
Author

aliosmnoz commented Apr 14, 2022

I getting this error on your example app, lastest android studio and lastest flutter sdk btw.

I think my codes work right but idk your plugin or tensorflow not work on lastest flutter version :/

@ahsanalidev
Copy link
Owner

Example app might be having error because running it on simulator causes GPU constraints. Your problem might be that android is compressing the model in the release build when you are storing it locally if you move the model to firebase and use firebase_ml_model_downloader to download the model and give the path of the model to the plugin it might work. This will reduce the appsize as well as things might work for you. I am very busy currently so I can't look into the issues right now give me few days after that I can look into the issues. If you want something urgent and you can directly use tensorflow.

@ahsanalidev
Copy link
Owner

I build the app on real android device but I was not able to encounter any error in debug build

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