Skip to content

Commit

Permalink
Merge pull request #373 from 3dwesupport/model-management
Browse files Browse the repository at this point in the history
Bug Fix: Tflite files are not uploading in Model Management on Android Devices
  • Loading branch information
sparsh3dwe authored Oct 6, 2023
2 parents 6f043ca + ab8ba7c commit bfe7d80
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 137 deletions.
11 changes: 0 additions & 11 deletions android/robot/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@

</activity>

<activity
android:name=".modelManagement.BackHandlingFilePickerActivity"
android:label="@string/app_name"
android:theme="@style/FilePickerTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<service
android:name=".logging.SensorService"
android:enabled="true"
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.openbot.modelManagement;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.OpenableColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -63,8 +66,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
Intent intent = result.getData();
// Handle the Intent
List<Uri> files = Utils.getSelectedFilesFromResult(intent);

String fileName = new File(files.get(0).getPath()).getName();
String fileName = getFileNameFromUri(files.get(0));
if (FileUtils.checkFileExistence(requireActivity(), fileName)) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(R.string.file_available_title);
Expand Down Expand Up @@ -110,6 +112,37 @@ public void handleOnBackPressed() {
requireActivity().getOnBackPressedDispatcher().addCallback(onBackPressedCallback);
}

/**
* Extracts the file name from a given Uri.
*
* @param uri The Uri from which to extract the file name.
* @return The extracted file name, or null if not found.
*/
private String getFileNameFromUri(Uri uri) {
String fileName = null;
if (uri.getScheme().equals("content")) {
// If the Uri uses the content:// scheme, use a ContentResolver to get the file name
ContentResolver contentResolver = requireActivity().getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor != null) {
try {
if (cursor.moveToFirst()) {
int displayNameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (displayNameIndex != -1) {
fileName = cursor.getString(displayNameIndex);
}
}
} finally {
cursor.close();
}
}
} else if (uri.getScheme().equals("file")) {
// If the Uri uses the file:// scheme, directly extract the file name
fileName = new File(uri.getPath()).getName();
}
return fileName;
}

private void processModelFromStorage(List<Uri> files, String fileName) {

Model item =
Expand Down Expand Up @@ -146,25 +179,11 @@ private void processModelFromStorage(List<Uri> files, String fileName) {
}

private void openPicker() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/octet-stream"); // Specify the MIME type for TFLite files
intent.addCategory(Intent.CATEGORY_OPENABLE);

Intent i = new Intent(requireActivity(), BackHandlingFilePickerActivity.class);
// This works if you defined the intent filter
// Intent i = new Intent(Intent.ACTION_GET_CONTENT);

// Set these depending on your use case. These are the defaults.
i.putExtra(BackHandlingFilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
i.putExtra(BackHandlingFilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
i.putExtra(BackHandlingFilePickerActivity.EXTRA_MODE, BackHandlingFilePickerActivity.MODE_FILE);

// Configure initial directory by specifying a String.
// You could specify a String like "/storage/emulated/0/", but that can
// dangerous. Always use Android's API calls to get paths to the SD-card or
// internal memory.
i.putExtra(
BackHandlingFilePickerActivity.EXTRA_START_PATH,
Environment.getExternalStorageDirectory().getPath());

mStartForResult.launch(i);
mStartForResult.launch(intent);
}

@Nullable
Expand Down Expand Up @@ -318,6 +337,7 @@ public void onModelDelete(Model mItem) {
adapter.notifyItemChanged(index);
}
FileUtils.updateModelConfig(requireActivity(), masterList);
requireActivity().runOnUiThread(() -> adapter.setItems(loadModelList(binding.modelSpinner.getSelectedItem().toString())));
});
builder.setNegativeButton("Cancel", (dialog, id) -> {});
AlertDialog dialog = builder.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ protected void addPixelValue(int pixelValue) {
@Override
protected void runInference() {
Object[] inputArray = {imgData};
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
try {
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
}
catch(Exception e){
System.out.println("Error Occurred: " + e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ protected void addPixelValue(int pixelValue) {
@Override
protected void runInference() {
Object[] inputArray = {imgData};
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
try {
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
}
catch(Exception e){
System.out.println("Error Occurred: " + e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.graphics.RectF;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -102,7 +103,12 @@ protected void addPixelValue(int pixelValue) {
@Override
protected void runInference() {
Object[] inputArray = {imgData};
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
try {
tflite.runForMultipleInputsOutputs(inputArray, outputMap);
}
catch(Exception e){
System.out.println("Error Occurred: " + e);
}
}

@Override
Expand Down
1 change: 0 additions & 1 deletion ios/OpenBot/OpenBot/models/modes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ public struct ModeItem {
var label: String;
var icon: UIImage;
var identifier: String;
var color: UIColor;
}

0 comments on commit bfe7d80

Please sign in to comment.