I am trying to change the texture of my object with this code:
Texture2D baileyburlwood = Instantiate(Resources.Load("bailey burlwood") as Texture2D);
myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;
It is working perfectly fine in the editor, the texture changes but when I tried to run it in my android device, my object just goes black. There is also no error or any warning. Pls help! Thanks!
I am using Unity 5.5.1f btw
From the screenshot in your updated question, the image you want to load is called bailey burlwood.jpg which is already in the Resources folder..
Herein lies the problem:
Instantiate(Resources.Load("bailey burlwood") as Texture2D);
You instantiate prefabs, scripts and components not normal classes like Texture2D.
Your code would have worked if bailey burlwood.jpg is bailey burlwood.prefab and you load it with GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject; but that's not the case here.
Since the "bailey burlwood" file is a JPG file, you should load like this:
Texture2D baileyburlwood = Resources.Load("bailey burlwood") as Texture2D;
myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;
Note that there is no Instantiate function involved. See this post for how to load other image files with different image settings when using the Resources folder.
Related
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.
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.
I've set a company logo above the main menu. This is what it looks like in QtDesigner:
I distorted the logo on purpose. The point is that the image is there.
This is the resource folder in QtCreator project view:
This is what it looks like on my android phone:
So, well, what's going on here? I see no file errors in console (and loading CSS files from resources works form me). One thing that is possibly relevant is that the image is a SVG file.
SVG was indeed an issue. While QtDesigner displays SVG without problems, the actual Qt library cannot handle that. I used this code to test it:
QPixmap pixmap;
if (!pixmap.load( ":/logo.svg" )) {
QDebug("Failed to load main svg logo.");
if (!pixmap.load( ":/logo.png" )) {
QDebug("Failed to load main png logo.");
}
else {
QDebug("PNG logo loaded.");
}
}
else {
QDebug("SVG logo loaded.");
}
I want to show image (png,jpg etc) in dynamically created (as per requirement and fully through coding) TImage component, at runtime in C++ builder xe8 (not delphi). But I dont want to use opendialogbox (suggested in many web sites). I want to run this app on my android device. I tried to use LoadFromFile(), it crashes the app on android, but when I run this on windows, its running smoothly. I am just a beginner to c++ builder. So guys pls help. Thanx in advance for any kind of help.Here is what I did.
void __fastcall TForm1::TForm1(TComponent* Owner)
{
TImage* img = new TImage(this);
img->Parent = this;
img->Bitmap->LoadFromFile("D:\\res\\profile.png");
}
Did you see what is the error?
If you run the program with the provided by you code I assume the error would be that the file is not found, because there is no such directory "D:\" in android.
One way to set the path is to write a static path which points to your image. For example : "/storage/sdcard0/DCIM/Camera/MyImage.jpg";
The second way is to include the <System.IOUtils.hpp> header and to use some built-in functions like:
System::Ioutils::TPath::GetPicturesPath();
System::Ioutils::TPath::GetAlarmsPath();
You can check them out, they might be useful.
I have used google, read all Questions here on StackExchange but still couldnt solve my Problem.
So Im following a tutorial from kilobolt(Zombie Bird).
The desktop-version of the game is running without problems, but if I want to run it on an android emulator I get this error message:
"Couldn't load file: data/logo.png"
The line where I get the error looks like this:
logoTexture = new Texture(Gdx.files.internal("data/logo.png"));
My Android Path looks like this:
-Android
-Assets
-Data
-logo.png
I assume this is the correct path.
I have tried cleaning the project, I used Gradle > Refresh All, I restarted Eclipse.
Nothing worked.
Im a little bit confused cause if I move the file outside of Data but in the Assets Folder and change the path to this:
logoTexture = new Texture(Gdx.files.internal("logo.png"));
It works.
Can someone explain me why I cant use the Data Folder inside of Assets?
What emulator are you using to run it? For me, the same code works properly on an actual Android device, so I'd assume it is an Emulator error. Possibly has to do with the / as the seperator character. This is just a wild guess, but does
logoTexture = new Texture(Gdx.files.internal("data" + File.separator + "logo.png"));
fix it, or break it?
maybe it's caused by the difference of OpenGl ES 1 and OpenGl ES 2...
Using OpenGl ES 1 the picture size has the power of two. Bear with me - I'm new to libgdx.. :)