Tensorflow android demo . How to retrain? - android

I am trying to use the android TensorFlow demo TF classify app code to change training data. I want to use two flowers instead just like in this demo: https://medium.com/#daj/creating-an-image-classifier-on-android-using-tensorflow-part-3-215d61cb5fcd
the problem is according to the tutorial im following it says that there should be a assets folder generated and i can put my training data in there and then re-build.
But when i built the tensorflow android demo there was no assets folder created in bazel-bin or in android src folder. i also did a search for assets folder and nothing.
I am using the docker container outlined in the article.

You have to create assets folder by yourself.
If you are using Android Studio, then select menu from
File -> New... -> Folder -> Assets Folder and then paste the files to that folder.
FYI, you also have to create libs folder (in project level) and jniLibs folder.
UPDATE:
After putting those graph file(.pb) and label file (.txt) in that Assets folder, you have to specify and load them from your code.
For example, assuming that your graph file name is "my_graph.pb" and label file is
"my_labels.txt", then specify them as :
private static final String MODEL_FILE = "file:///android_asset/my_graph.pb";
private static final String LABEL_FILE ="file:///android_asset/my_labels.txt";
and you can load them when initializing:
classifier = TensorFlowImageClassifier.create(
getAssets(),
MODEL_FILE,
LABEL_FILE,
IMAGE_SIZE,
IMAGE_MEAN,
IMAGE_STD,
INPUT_NAME,
OUTPUT_NAME);
} catch (final Exception e) {
throw new RuntimeException("Error initializing TensorFlow!", e);
}
Of course don't forget to define other constants (IMAGE_SIZE, IMAGE_MEAN, etc..) with appropriate values before initializing.
UPDATE 2
FYI, here's sample project's app structure in android studio:

Related

Android install-time asset delivery can't access the asset folder

I followed this guide Integrate asset delivery (Kotlin & Java) from step 1-8, to integrate install-time asset delivery. I added folders with drawables to my \asset_pack1\src\main\assets folder, but i can't access them with code or open the folder in android studio. When i build my bundle with gradle, the manifest appers in asset_pack1\build\intermediates\asset_pack_manifest, but the drawable folders don't.
it doesn't show the content of asset_pack1
I've create the build.gradle in my asset_pack1 folder:
plugins {
id 'com.android.asset-pack'
}
assetPack {
packName = "asset_pack1"
dynamicDelivery {
deliveryType = "install-time"
}
}
Setuo the asset pack in my app build.gradle:
android {
..
assetPacks = [":asset_pack1"]
..
}
Included the pack in my settings.gradle:
include ':app'
include ':asset_pack1'
Added my asset directories:
Content of \asset_pack1\src\main\assets
And built the bundle with gradle:
over the menu
Also tried it over the terminal with: ./gradlew bundle
Does anyone know what i did wrong?
I followed the guide multiple times but always with the same result. Could't find any thread describing the same problem.

In a react-native android project,How to copy a folder from assets to the DocumentDirectoryPath?

I am using a react-native project in which,I want to copy the contents of a certain folder in assets inside android to RNFS.DocumentDirectoryPath. How can this be performed?
I have used the attribute copyFileAssets().
copyfile() {
RNFS.copyFileAssets('/ICF-Package', RNFS.DocumentDirectoryPath + '/ICFPackage').then((result) => console.log('DONE')).catch((error) => console.log(error, 'ERROR'));
};
I'm getting the following error:
Error: Asset '/ICF-Package' could not be opened
Your code is correct. However, it looks like /ICF-Package is a folder or something, but not a file. copyFileAssets can only copy files, but no folders (and its content)

Use Sample data directory from a library

I created Sample data directory for my Android app using the process described in this article. I would like to share this set of sample data between my projects, so I created a library that only has sample data inside. But as far as I can see sampledata folder is not being compiled into the library. Is there a way to share sample data between multiple Android projects?
As already said, you can't do that with a library because sampledata simply can't be part of an Android library.
One thing you could though, host your names file somewhere and then fetch it with a gradle task, you could just add to an app's build.gradle
clean.doFirst {
println "cleanSamples"
def samplesDir = new File(projectDir.absolutePath, "sampledata")
if (samplesDir.exists()) {
samplesDir.deleteDir()
}
}
task fetchSamples {
println "fetchSamples"
def samplesDir = new File(projectDir.absolutePath, "sampledata")
if (samplesDir.exists()) {
println "samples dir already exists"
return
}
samplesDir.mkdir()
def names = new File(samplesDir, "names")
new URL('http://path/to/names').withInputStream { i ->
names.withOutputStream {
it << i
}
}
}
You can see 2 functions there, the first one is run before a clean task and it will just delete your sampledata folder. The second one is a task run on every build, it won't download the file every time but only if the directory is not there.
I understand you might as well copy paste names file, but, with this method you need to copy paste the tasks only once and you would be able to change names in any project just by uploading a new file and doing a clean build.
The short answer is no, you can't do that with sampledata folder. Basically, the format of the Android Libraries is AAR. If you reference the official documentation, it says that:
The file itself is a zip file containing the following mandatory entries:
/AndroidManifest.xml
/classes.jar
/res/
/R.txt
/public.txt
Additionally, an AAR file may include one or more of the following optional entries:
/assets/
/libs/name.jar
/jni/abi_name/name.so (where abi_name is one of the Android supported ABIs)
/proguard.txt
/lint.jar
So, sampledata can't be a part of AAR library.
UPDATE
Instead of your own data samples, you can use predefined sample resources. For example #tools:sample/first_names will randomly select from some common first names, eg., Sophia, Jacob, Ivan.
Example of usage:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="#tools:sample/first_names" />

How to add add custom assets into .apk file?

I need to have some files in android assets folder, how can I add them using QtCreator/QMake?
Assuming you have the following structure in your source directory:
foo.pro
extra_data/file1
extra_data/file2
…
Adding the following to foo.pro should deploy the extra_data folder to assets://extra_data (exact path might differ, cannot verify right now) in the APK:
folder_01.source = extra_data
folder_01.target = extra_data
DEPLOYMENTFOLDERS += folder_01
If you are developing the application, then simply copy/paste the files in assets folder.
But if your application is already built and available as .apk file then you cannot modify any of its content.
Copying files to /5.2.0/android_armv7/src/android/java/assets did the trick

Android access to resources outside of activity

I would like to know - are there ways to access android resources and/or assets files (or files from any other folder) outside of an Activity (without passing context)?
Can't access without context:
getResources().getIdentifier("resource_name", "drawable", getPackageName());
getAssets().open("file_in_assets_folder_name");
Throws Exception if not in Activity:
try {
Class class = R.drawable.class;
Field field = class.getField("resource_name");
Integer i = new Integer(field.getInt(null));
} catch (Exception e) {}
Also tried the below, but it complains file doesn't exist (doing something wrong here?):
URL url = new URL("file:///android_asset/file_name.ext");
InputSource source = new InputSource(url.openStream());
//exception looks like so 04-10 00:40:43.382: W/System.err(5547): java.io.FileNotFoundException: /android_asset/InfoItems.xml (No such file or directory)
If the folders are included in the Project Build Path, you can use ClassLoader access files under them outside the android.content.Context, for instance, from a POJO (in case if you don't want to pass a reference of android.content.Context):
String file = "res/raw/test.txt";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
The res folder is included into Project Build Path by default by all Android SDK version so far.
The assets folder was included into Project Build Path by default before Android SDK r14.
To add folders into Project Build Path, right click your project -- Build Path -- Configure Build Path, add your folder (for example, assets if using later SDK version) as a Source folder in build path.
Check out the similar question I answered before at here.
Android framework when compile your app create static class call:
your.namespace.project.R
you can statically call this class like:
your.namespace.project.R.string.my_prop
this.context.findViewById(your.namespace.project.R.id.my_id_prop);
Maybe you can access dynamic resources this way:
Resources res = this.context.getResources();
int id = res.getIdentifier("bar"+Integer.toString(i+1), "id", this.context.getPackageName());

Categories

Resources