Use LoadModelFromMemoy in ASSIMP to load obj model with no texture loaded - android

I'm currently working on an Android project where I need to load .obj file with ASSIMP on Android Platform. My way of implementing that is to use the AssetManager to load the .obj file into memory first, and then using the importer.ReadFileFromMemory() function to create the aiScene object. I've managed to import all vertex data, but I found that the texture is missing. I actually read from the ASSIMP GitHub page where it mentioned that ReadFileFromMemory() won't read contents cross file, therefore I think it is not reading the .mtl file where texture is applied. I would like to use the importer.ReadFile() function, but I have no idea on how to work with it on Android platform. Any one has suggestions?
Attached is my implementation of the loadModelFromMemory, similar as the loadModel from LearnOpenGL.
void Model::loadModelFromMemory(const void* pbuffer, size_t pLength)
{
Log::Message("Enter loadModelFromMemory", LOG_INIT);
// read file via ASSIMP
Assimp::Importer importer;
const aiScene* scene = importer.ReadFileFromMemory(pbuffer, pLength, aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
// check for errors
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
Log::Message(strcat("ERROR::ASSIMP::", importer.GetErrorString()), LOG_ERROR);
return;
}
// process ASSIMP's root node recursively
processNode(scene->mRootNode, scene);
}
I have tried on using ReadFile but it is not working on the Android context. I also tried to use the MemoryIOWrapper provided by the ASSIMP, but have no clue on where to start.

There are different ways how to implement this.
Load the mtl- and the obj-file via memory buffers
Use the android-filesystem implementations to load assets from the asset-folder on your mobile phone. You can find our headers for that at Android File system implementations

Related

Is it possible to programatically preload a shared library?

Im building a project that needs to modify the behavior of some bionic methods (e.g getaddrinfo, __android_print). I've been able to create the hooked library both using a standalone compiler or including it directly in the Apk using Cmake. I've been able to preload the shared library using setprop wrap.com.foo.bar and LD_PRELOAD, and it is working and I get the result I want. However, I want to preload the hooked library programatically so I dont need to go through the specific steps of LD_PRELOAD (a.k.a disable SELinux, root device, setprop), every time after rebooting a device.
I tried using
// MainActivity
companion object {
System.load("/data/data/com.foo.bar/lib/libhookedmethod.so")
}
But I don't see the method being replaced.
As a reference, the hooked method is rather simple. Here's an extreme simplification:
int __android_print(varargs a) {
int realmethod(...);
realmethod = dlsym("__android_print");
doStuff();
int res = realmethod(a) ;
return res;
}
Again, compiling and using LD_PRELOAD works, but I want to achieve it without using LD_PRELOAD...
Anything helps! Thanks in advance
The LD_PRELOAD works by asking the dynamic loader to load referenced library before any other. The loader resolves references to a given symbol by searching loaded libraries in their load order.
Once a symbol is resolved to a particular library, it will not be rebound to some other library for the duration of this process.
Above explanation should make it obvious why loading libhookedmethod.so after the program is already running has no effect.
About the only way you can achieve what you want is to setenv() (if not already set) and re-exec() the current process. Something like this:
int main(int argc, char *argv[])
{
char *e = getenv("LD_PRELOAD");
if (e == NULL || /* any other checks that show LD_PRELOAD to not be as we want it */) {
setenv("LD_PRELOAD", ..., 1);
execvp(argv[0], argv);
}
// LD_PRELOAD is to our liking. Run the actual program.
...
}

Load a set of images from a directory in React Native

I have a group of images in a directory and I want to add the paths to an array in react native.
The image names are 1.png, 2.png, 3.png... 20.png
The following is the method I currently use
const images = [require('../assets/1/1.png'),
require('../assets/1/2.png'),
require('../assets/1/3.png'),
...
require('../assets/1/20.png'),];
This is possible when there is a limited number of images. In some cases I might have to add 300 images. So is there an easy way to do this?
I tried adding in a loop but it does not work.
for (let i = 1; i < 300; i++) {
images.push(require('../assets/1/'+i+'.png'))
}
EDIT :
The following error occurs in the above approach,
error: bundling failed: Error: Components\IdleLoop.js:Invalid call at line 9: require('../assets/1/' + i + '.png')
at F:\_work\sampleApp\node_modules\metro\src\JSTransformer\worker.js:317:19
at Generator.next (<anonymous>)
at asyncGeneratorStep (F:\_work\sampleApp\node_modules\metro\src\JSTransformer\worker.js:75:24)
at _next (F:\_work\sampleApp\node_modules\metro\src\JSTransformer\worker.js:95:9)
As far as I know because React Native applications are statically bundled on build time, required assets are linked in advanced therefore there isn't a way to load them dynamically on runtime. I've ran into the same problem and gave up on that and ended up linking everything manually/statically.
For more information you might want to check out React Native - Image Require Module using Dynamic Names, which concludes the same.

How to read tensorflow memory mapped graph file in android?

Using Tensorflow 1.0.1 it's fine to read optimized graph and quantized graph in android using TensorFlowImageClassifier.create method, such as:
classifier = TensorFlowImageClassifier.create(
c.getAssets(),
MODEL_FILE,
LABEL_FILE,
IMAGE_SIZE,
IMAGE_MEAN,
IMAGE_STD,
INPUT_NAME,
OUTPUT_NAME);
But according to the Peter Warden's Blog(https://petewarden.com/2016/09/27/tensorflow-for-mobile-poets/), it's recommended to use memory mapped graph in mobile to avoid memory related crashes.
I built memmapped graph using
bazel-bin/tensorflow/contrib/util/convert_graphdef_memmapped_format \
--in_graph=/tf_files/rounded_graph.pb \
--out_graph=/tf_files/mmapped_graph.pb
and it created fine, but when I tried to load the file with TensorFlowImageClassifier.create(...) it says the file is not valid graph file.
In iOS, it's ok to load the file with
LoadMemoryMappedModel(
model_file_name, model_file_type, &tf_session, &tf_memmapped_env);
for it has a method for read memory mapped graph.
So, I guess there's a similar function in android, but I couldn't find it.
Could someone guide me how to load memory mapped graph in android ?
Since the file from the memmapped tool is no longer a standard GraphDef protobuf, you need to make some changes to the loading code. You can see an example of this in the iOS Camera demo app, the LoadMemoryMappedModel() function:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/ios_examples/camera/tensorflow_utils.mm#L159
The same code (with the Objective C calls for getting the filenames substituted) can be used on other platforms too. Because we’re using memory mapping, we need to start by creating a special TensorFlow environment object that’s set up with the file we’ll be using:
std::unique_ptr<tensorflow::MemmappedEnv> memmapped_env;
memmapped_env->reset(
new tensorflow::MemmappedEnv(tensorflow::Env::Default()));
tensorflow::Status mmap_status =
(memmapped_env->get())->InitializeFromFile(file_path);
You then need to pass in this environment to subsequent calls, like this one for loading the graph.
tensorflow::GraphDef tensorflow_graph;
tensorflow::Status load_graph_status = ReadBinaryProto(
memmapped_env->get(),
tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef,
&tensorflow_graph);
You also need to create the session with a pointer to the environment you’ve created:
tensorflow::SessionOptions options;
options.config.mutable_graph_options()
->mutable_optimizer_options()
->set_opt_level(::tensorflow::OptimizerOptions::L0);
options.env = memmapped_env->get();
tensorflow::Session* session_pointer = nullptr;
tensorflow::Status session_status =
tensorflow::NewSession(options, &session_pointer);
One thing to notice here is that we’re also disabling automatic optimizations, since in some cases these will fold constant sub-trees, and so create copies of tensor values that we don’t want and use up more RAM. This setup also means it's hard to use a model stored as an APK asset in Android, since those are compressed and don't have normal filenames. Instead you'll need to copy your file out of an APK onto a normal filesytem location.
Once you’ve gone through these steps, you can use the session and graph as normal, and you should see a reduction in loading time and memory usage.

Error in saving and using model of TensorForestEstimator for Android

I use the randomforest estimator, implemented in tensorflow, to predict if a text is english or not. I saved my model (A dataset with 2k samples and 2 class labels 0/1 (Not English/English)) using the following code (train_input_fn function return features and class labels):
model_path='test/'
TensorForestEstimator(params, model_dir='model/')
estimator.fit(input_fn=train_input_fn, max_steps=1)
After running the above code, the graph.pbtxt and checkpoints are saved in the model folder. Now I want to use it on Android. I have 2 problems:
As the first step, I need to freeze the graph and checkpoints to a .pb file to use it on Android. I tried freeze_graph (I used the code here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py). When I call the freeze_graph in my mode, I get the following error and the code cannot create the final .pb graph:
File "/Users/XXXXXXX/freeze_graph.py", line 105, in freeze_graph
_ = tf.import_graph_def(input_graph_def, name="")
File "/anaconda/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/importer.py", line 258, in import_graph_def
op_def = op_dict[node.op]
KeyError: u'CountExtremelyRandomStats'
this is how I call freeze_graph:
def save_model_android():
checkpoint_state_name = "model.ckpt-1"
input_graph_name = "graph.pbtxt"
output_graph_name = "output_graph.pb"
checkpoint_path = os.path.join(model_path, checkpoint_state_name)
input_graph_path = os.path.join(model_path, input_graph_name)
input_saver_def_path = None
input_binary = False
output_node_names = "output"
restore_op_name = "save/restore_all"
filename_tensor_name = "save/Const:0"
output_graph_path = os.path.join(model_path, output_graph_name)
clear_devices = True
freeze_graph.freeze_graph(input_graph_path, input_saver_def_path,
input_binary, checkpoint_path,
output_node_names, restore_op_name,
filename_tensor_name, output_graph_path,
clear_devices, "")
I also tried the freezing on the iris dataset in "tf.contrib.learn.datasets.load_iris". I get the same error. So I believe it is not related to the dataset.
As a second step, I need to use the .pb file on the phone to predict a text. I found the camera demo example by google and it contains a lot of code. I wonder if there is a step by step tutorial how to use a Tensorflow model on Android by passing a feature vector and get the class label.
Thanks, in advance!
UPDATE
By using the recent version of tensorflow (0.12), the problem is solved. However, now, the problem is that what I should pass to output_node_names ??? How can I get what are the output nodes in the graph ?
Re (1) it looks like you are running freeze_graph on a build of tensorflow which does not have access to contrib ops. Maybe try explicitly importing tensorforest before calling freeze_graph?
Re (2) I don't know of a simpler example.
CountExtremelyRandomStats is one of TensorForest's custom ops, and exists in tensorflow/contrib. As was pointed out, TF switched to including contrib ops by default at some point. I don't think there's an easy way to include the contrib custom ops in the global registry in the previous releases, because TensorForest uses the method of building a .so file that is included as a data file which is loaded at runtime (a method that was the standard when TensorForest was created, but may not be any longer). So there are no easily-included python build rules that will properly link in the C++ custom ops. You can try including tensorflow/contrib/tensor_forest:ops_lib as a dep in your build rule, but I don't think it will work.
In any case, you can try installing the nightly build of tensorflow. The alternative includes modifying how tensorforest custom ops are built, which is pretty nasty.

How do I load an image to an apk in delphi XE8

I am working on an app in Delphi XE8.
When I run the program on my phone, it gives me an error:
Loading bitmap failed(image.png)
My code works as follows:
if ListBox1.ItemIndex = 0 then
begin
img.bitmap.LoadFromFile('Image.png');
iMin:= Round(iNumber * 1);
iMax:= Round(iNumber *13.24);
iAvg:= Round(iNumber * 2.59);
label7.Text:= inttostr(iMin);
label5.Text:= inttostr(iAvg);
label6.Text:= inttostr(iMax);
label2.Text:= 'Minimum';
label3.Text:= 'Average';
label4.Text:= 'Maximum';
end
else
...
Please note the image is saved in the same folder as my program.
Don't use relative paths. Always use absolute paths.
You need to use the Deployment Manager to deploy the image file to an appropriate folder on the phone, and then use the System.IOUtils.TPath class to locate that folder at runtime:
Standard RTL Path Functions across the Supported Target Platforms
On Android, deploy the image file to the ./assets/internal folder, and then use the TPath.GetDocumentsPath() method at runtime, as documented on this blog:
Deploying and accessing local files on iOS and Android
What the EDN documentation and blog both fail to mention is that you also need to add the System.StartupCopy unit to your app's uses clause.
uses
..., System.IOUtils, System.StartupCopy;
...
img.bitmap.LoadFromFile(TPath.Combine(TPath.GetDocumentsPath, 'Image.png'));

Categories

Resources