RenderScript problems in eclipse - java

I'm trying to compile a sample of renderScript, but no way.
I have some errors that I can not solve:
[09.22.2014 18:28:44 - aaa] error: Error reading '... \ aaa \ process.rs'
Also I get that kind ScriptC_process there when I created the file .rs.
project.properties:
project.properties:
renderscript.target=18
renderscript.support.mode=true
sdk.buildtools=19.1.0
target=android-20
Also show the configuration of the project. Thank you very much.
EDIT:
The code of process.rs is as follows:
#pragma version(1)
#pragma rs java_package_name(com.example.aaa)
//multipliers to convert a RGB colors to black and white
const static float3 gMonoMult = {0.299f, 0.587f, 0.114f};
void root(const uchar4 *v_in, uchar4 *v_out) {
//unpack a color to a float4
float4 f4 = rsUnpackColor8888(*v_in);
//take the dot product of the color and the multiplier
float3 mono = dot(f4.rgb, gMonoMult);
//repack the float to a color
*v_out = rsPackColorTo8888(mono);
}
I'm just using for example the following website:
[link] https://stuff.mit.edu/afs/sipb/project/android/docs/guide/topics/renderscript/compute.html

Related

Draw points correctly with Canvas

I have a python script that draws landmarks on the contours of eye region with image input got from ckpts files (model) using OpenCV.
I want to draw those points ( landmarks ) in the same picture.
I got predictions points from the picture and I tried to draw those points (x,y) using Canvas but the results are different.
Difference between the two images:
Landmarks are drawn using python script (OpenCV)
Landmarks are draw using java code (Canvas)
I have tried many ways and I use Canvas library to draw points on imageview ( I loaded the same image in assets folder ) but this doesn't solve my problem..
This is a python code that shows how to draw landmarks on image:
predictions = estimator.predict(input_fn=_predict_input_fn)
for _, result in enumerate(predictions):
img = cv2.imread(result['name'].decode('ASCII') + '.jpg')
print(result['logits'])
print(result['name'])
marks = np.reshape(result['logits'], (-1, 2)) * IMG_WIDTH
print("reshape values "+str(np.reshape(result['logits'], (-1,2))))
print("marks "+str(marks))
for mark in marks:
cv2.circle(img, (int(mark[0]), int(
mark[1])), 1, (0, 255, 0), -1, cv2.LINE_AA)
try:
img = cv2.resize(img, (512, 512))
cv2.imshow('result', img)
except Exception as e:
print(str(e))
# output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]
# print(output_node_names)
cv2.waitKey()
This file shows print logs from python code:
[0.33135968 0.19592011 0.34212315 0.17297666 0.36624995 0.16413747
0.3894139 0.17440952 0.39828074 0.1978043 0.3891497 0.22268474
0.36345637 0.22974193 0.3401759 0.2193309 0.30167252 0.20411113
0.3167112 0.19134495 0.33793524 0.18388326 0.3642417 0.18049955
0.3903508 0.18533507 0.40906873 0.1957745 0.42142123 0.21091096
0.40550107 0.21829814 0.38345626 0.22071144 0.35900232 0.22142673
0.3363348 0.21877256 0.3161971 0.2133534 0.62843406 0.21482795
0.6389724 0.1914106 0.6628249 0.1835615 0.6858679 0.19583184
0.6946868 0.22111627 0.6840309 0.24444285 0.66027373 0.25241333
0.6351568 0.24192403 0.60499936 0.22642238 0.6210091 0.21289764
0.6423563 0.2042976 0.6685919 0.20277795 0.69201195 0.20948553
0.70882106 0.22015369 0.71931773 0.23518339 0.7076659 0.24166131
0.69054717 0.24350837 0.6694564 0.24258481 0.64537776 0.23927754
0.62199306 0.23511863]
b'C:\\Users\\*******\\cnn-facial-landmark\\targetiris\\irisdata-300VW_Dataset_2015_12_14-017-000880'
reshape values [[0.33135968 0.19592011]
[0.34212315 0.17297666]
[0.36624995 0.16413747]
[0.3894139 0.17440952]
[0.39828074 0.1978043 ]
[0.3891497 0.22268474]
[0.36345637 0.22974193]
[0.3401759 0.2193309 ]
[0.30167252 0.20411113]
[0.3167112 0.19134495]
[0.33793524 0.18388326]
[0.3642417 0.18049955]
[0.3903508 0.18533507]
[0.40906873 0.1957745 ]
[0.42142123 0.21091096]
[0.40550107 0.21829814]
[0.38345626 0.22071144]
[0.35900232 0.22142673]
[0.3363348 0.21877256]
[0.3161971 0.2133534 ]
[0.62843406 0.21482795]
[0.6389724 0.1914106 ]
[0.6628249 0.1835615 ]
[0.6858679 0.19583184]
[0.6946868 0.22111627]
[0.6840309 0.24444285]
[0.66027373 0.25241333]
[0.6351568 0.24192403]
[0.60499936 0.22642238]
[0.6210091 0.21289764]
[0.6423563 0.2042976 ]
[0.6685919 0.20277795]
[0.69201195 0.20948553]
[0.70882106 0.22015369]
[0.71931773 0.23518339]
[0.7076659 0.24166131]
[0.69054717 0.24350837]
[0.6694564 0.24258481]
[0.64537776 0.23927754]
[0.62199306 0.23511863]]
marks [[37.112286 21.943052]
[38.317795 19.373386]
[41.019993 18.383396]
[43.614357 19.533867]
[44.607445 22.154081]
[43.584766 24.940691]
[40.707115 25.731096]
[38.0997 24.565062]
[33.787323 22.860447]
[35.471653 21.430634]
[37.848747 20.594925]
[40.79507 20.21595 ]
[43.719288 20.757528]
[45.815697 21.926743]
[47.199177 23.622028]
[45.41612 24.44939 ]
[42.9471 24.71968 ]
[40.20826 24.799793]
[37.6695 24.502527]
[35.414074 23.89558 ]
[70.38461 24.06073 ]
[71.56491 21.437988]
[74.23639 20.558887]
[76.81721 21.933167]
[77.80492 24.765022]
[76.61146 27.3776 ]
[73.95066 28.270294]
[71.137566 27.095491]
[67.759926 25.359306]
[69.553024 23.844536]
[71.9439 22.881332]
[74.88229 22.71113 ]
[77.50534 23.46238 ]
[79.387955 24.657213]
[80.56358 26.34054 ]
[79.25858 27.066067]
[77.341286 27.272938]
[74.97912 27.169498]
[72.28231 26.799084]
[69.66322 26.333286]]
Java code (Android)
private void drawpoint(ImageView imageView,float x,float y, int raduis){
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
canvas.drawCircle(x,y, raduis, paint);
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
drawpoint(image2, 38, 19,1);
drawpoint(image2,41,18,1);
drawpoint(image2,43,19,1);
drawpoint(image2,40,25,1);
drawpoint(image2,38,24,1);
How can I solve this problem?
Problem solved.
I used the OpenCV library for drawing in Android instead of Canvas library.
I have used exactly this function:
Imgproc.circle()

Load a tensorflow model in java

I am trying to load a tensorflow model in Java.
tf.saved_model.simple_save(
sess,
"/tmp/model/"+timestamp,
inputs={"input_x" : cnn.input_x},
outputs={"input_y" : cnn.input_y})
This is how I save a tensorflow model in python.
public static void main( String[] args ) throws IOException
{
// good idea to print the version number, 1.2.0 as of this writing
System.out.println(TensorFlow.version());
final int NUM_PREDICTIONS = 1;
Random r = new Random();
long[] shape = new long[] {1,56};
IntBuffer buf = IntBuffer.allocate(1*56);
for (int i = 0; i < 56; i++) {
buf.put(r.nextInt());
}
buf.flip();
// load the model Bundle
try (SavedModelBundle b = SavedModelBundle.load("/tmp/model/1549001254", "serve")) {
Session sess = b.session();
// run the model and get the result, 4.0f.
try(Tensor x = Tensor.create(shape, buf)){
float[] result = sess.runner()
.feed("input_x", x)
.fetch("input_y")
.run()
.get(0)
.copyTo(new float[1][2])[0];
// print out the result.
System.out.println(result[0]);
}
}
}
This is how I load it in Java.
The given SavedModel SignatureDef contains the following input(s):
inputs['input_x'] tensor_info:
dtype: DT_INT32
shape: (-1, 56)
name: input_x:0
The given SavedModel SignatureDef contains the following output(s):
outputs['input_y'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 2)
name: input_y:0
Method name is: tensorflow/serving/predict
The input and output are saved well.
1.12.0
2019-02-01 15:58:59.065677: I tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: /tmp/model/1549001254
2019-02-01 15:58:59.072601: I tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
2019-02-01 15:58:59.085912: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2
2019-02-01 15:58:59.132271: I tensorflow/cc/saved_model/loader.cc:162] Restoring SavedModel bundle.
2019-02-01 15:58:59.199331: I tensorflow/cc/saved_model/loader.cc:138] Running MainOp with key legacy_init_op on SavedModel bundle.
2019-02-01 15:58:59.199435: I tensorflow/cc/saved_model/loader.cc:259] SavedModel load for tags { serve }; Status: success. Took 133774 microseconds.
Exception in thread "main" java.lang.IllegalArgumentException: You must feed a value for placeholder tensor 'input_y' with dtype float and shape [?,2]
[[{{node input_y}} = Placeholder[_output_shapes=[[?,2]], dtype=DT_FLOAT, shape=[?,2], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
at org.tensorflow.Session.run(Native Method)
at org.tensorflow.Session.access$100(Session.java:48)
at org.tensorflow.Session$Runner.runHelper(Session.java:314)
at org.tensorflow.Session$Runner.run(Session.java:264)
at Use_model.main(Use_model.java:38)
But It cannot load the model... The error message is like this.
I don't know what is the problem and how to fix it.
There is some confusion about input_y in your code. The exception says:
You must feed a value for placeholder tensor 'input_y' with dtype float and shape [?,2]
This means that, in your python code, input_y is defined to be a placeholder. I guess this is the placeholder that contains labels of the input_x items. Then input_y should be used in your loss function to compare the last layer of your cnn (let's call it cnn.output) with the actual labels (cnn.input_y), e.g.:
loss = tf.square(cnn.input_y - cnn.output)
Then, you python code should save cnn.output in the outputs dictionary, and not cnn.input_y:
tf.saved_model.simple_save(
sess,
"/tmp/model/"+timestamp,
inputs={"input_x" : cnn.input_x},
outputs={"output" : cnn.output})
In your java code you should then fetch "output":
float[] result = sess.runner()
.feed("input_x", x)
.fetch("output")
.run()
.get(0)
.copyTo(new float[1][2])[0];

(LibGDX) Android app crashes when creating FrameBuffers

I received a crash report from Google play store for one of my Android apps created using LibGDX.
Huawei MediaPad T3 7 (hwbg2), Android 6.0
java.lang.IllegalStateException:
at com.badlogic.gdx.graphics.glutils.GLFrameBuffer.build (GLFrameBuffer.java:233)
at com.badlogic.gdx.graphics.glutils.GLFrameBuffer.<init> (GLFrameBuffer.java:87)
at com.badlogic.gdx.graphics.glutils.FrameBuffer.<init> (FrameBuffer.java:51)
at com.badlogic.gdx.graphics.glutils.GLFrameBuffer$FrameBufferBuilder.build (GLFrameBuffer.java:474)
at com.badlogic.gdx.graphics.glutils.FrameBuffer.createFrameBuffer (FrameBuffer.java:72)
at com.badlogic.gdx.graphics.glutils.FrameBuffer.createFrameBuffer (FrameBuffer.java:56)
at MY_PACKAGE.editor.Backup.<init> (Backup.java:21)
at MY_PACKAGE.editor.EditingImage.<init> (EditingImage.java:277)
at MY_PACKAGE.screens.EditingScreen.<init> (EditingScreen.java:227)
at MY_PACKAGE.screens.Screens.<init> (Screens.java:42)
at MY_PACKAGE.MAIN_CLASS$2.run (MAIN_CLASS.java:121)
at MY_PACKAGE.screens.SplashScreen.render (SplashScreen.java:93)
at com.badlogic.gdx.Game.render (Game.java:46)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame (AndroidGraphics.java:495)
at android.opengl.GLSurfaceView$GLThread.guardedRun (GLSurfaceView.java:1599)
at android.opengl.GLSurfaceView$GLThread.run (GLSurfaceView.java:1295)
Code at GLFrameBuffer.java:233
if (result == GL20.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)
throw new IllegalStateException("frame buffer couldn't be constructed: incomplete attachment");
EditingImage.java is as follows
class EditingImage{
public static final int pixmapWidth = 1024;
public EditingImage{
frameBuffer = FrameBuffer.createFrameBuffer(Pixmap.Format.RGB888,pixmapWidth,pixmapWidth,false);
....
for (int i = 0; i < 50; i++){
final Backup backup = new Backup(pixmapWidth);
availableBackups.add(backup);
}
}
}
Backup.java is as follow
Backup(int width){
frameBuffer = FrameBuffer.createFrameBuffer(Pixmap.Format.RGB888, width, width, false);
....
}
The app has crashed inside Backup.java when creating the FrameBuffer (after how many loops I don't know).
As you can see FrameBuffer created in EditingImage has not crashed and it has been executed before instantiating Backup objects.
It works normally on my phone (Huawei Y6II). Also have tested in some Samsung phones.
Please help!
After 1 year 11 months I found the issue.
According to the documentation:
https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/FrameBuffer.html
It says for the format which is passed as the parameter in the constructor should be RGB565 or RGBA4444 or RGB5_A1
format - the format of the color buffer; according to the OpenGL ES
2.0 spec, only RGB565, RGBA4444 and RGB5_A1 are color-renderable
Where as in my case i have used RGB888

OPENCV crash when saving to file trained machine learning data (like SVM or ANN)

I have built a simple project in android studio, and included OpenCV in order to train either an SVM (support vector machine) or an ANN (artificial neural network). Everything seems to go well, including data creation, training and inspection of trained data, except for saving. Whenever I save a opencv ml-object (like ann.save(...) or svm.save(...)), android studio crashes.
SVM -
When I extract supportvectors using the line
classifier.getSupportVectors()
the numbers seem sain. However, the app crashes when I move past a breakpoint placed at
classifier.save("C:\\foo\\trentsvm.txt");
In logCat I dig up the following feedback:
07-04 14:36:10.939 25258-25258/com.example.tbrandsa.opencvtest A/libc:
Fatal signal 11 (SIGSEGV), code 2, fault addr 0x7f755f53f0 in tid
25258 (ndsa.opencvtest) [ 07-04 14:36:10.942 439: 439 W/]
debuggerd: handling request: pid=25258 uid=10227 gid=10227 tid=25258
I get a similar error if i instead try to save an artificial neural network (ANN), see update far below.
I have tried saving the file as XML and txt, and as "C:\trentsvm.someformat", and as "trentsvm.someformat". I also get the same error in my Eclipse java project. High pain, no gain. Full code is below. Could you help?
PS: I use OpenCv version 3.2.0. and Android Studio 2.3.2
// I based this code on stuff i found online. Not sure if all is as important or good.
// Purpose: multilabel classification - digit recognition for android app.
// Create data and labels for a digit recognition algorithm
int numTargets = 10; // (0-9 => 10 types of labels)
int totalSamples = 100; // Could have been number of images of digits
int totalIndicators = 10; // Could have been number of properites per digit image.
Mat labels = new Mat(totalSamples,1, CvType.CV_16S);
Mat data = new Mat(totalSamples, totalIndicators,CvType.CV_16S);
// Fill with dummy values:
for (int s = 0; s<totalSamples; s++)
{
int someLabel = s%numTargets;
labels.put(s,0, (double)someLabel);
for (int m = 0; m<totalIndicators; m++)
{
int someDataValue = (s%numTargets)*totalIndicators + m;
data.put(s, m, (double)someDataValue);
}
}
data.convertTo(data, CvType.CV_32F);
labels.convertTo(labels, CvType.CV_32S);
SVM classifier = SVM.create();
TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1);
classifier.setKernel(SVM.LINEAR);
classifier.setType(SVM.C_SVC); //We choose here the type CvSVM::C_SVC that can be used for n-class classification (n >= 2).
classifier.setGamma(0.5);
classifier.setNu(0.5);
classifier.setC(1);
classifier.setTermCriteria(criteria);
classifier.train(data, Ml.ROW_SAMPLE, labels);
// Check how trained SVM predicts the training data
Mat estimates = new Mat(totalSamples, 1, CvType.CV_32F);
classifier.predict(data, estimates, StatModel.RAW_OUTPUT);
for (int i = 0; i<totalSamples; i++)
{
double l = labels.get(i, 0)[0];
double e = estimates.get(i, 0)[0];
System.out.print("\n fact: "+l+", estimat: "+e);
}
Mat suppV = classifier.getSupportVectors();
try {
if (classifier.isTrained()){
// It crashes at the next line!
classifier.save("C:\\foo\\trentsvm.txt");
}
}
catch (Exception e)
{
}
Update july 5th: As suggested by ZdaR, I tried the to use an in-phone adress, but it did not solve the problem.
String address = Environment.getExternalStorageDirectory().getPath()+"/trentsvm.xml";
// address now has value "storage/emulated/0/trentsvm.xml"
classifier.save(address);
In logcat:
07-05 14:50:12.420 11743-11743/com.example.tbrandsa.opencv2 A/libc:
Fatal signal 11 (SIGSEGV), code 2, fault addr 0x7d517f1990 in tid
11743 (brandsa.opencv2)
[ 07-05 14:50:12.424 3134: 3134 W/ ] debuggerd: handling
request: pid=11743 uid=10319 gid=10319 tid=11743
Update july 6th:
When I run the same script in eclipse and use a debugger (JUnit 4, VM arguments: -Djava.library.path=C:\Users\tbrandsa\Downloads\opencv\build\java\x64;src\test\jniLibs, ) debugging on the pc without device, the caught exception "e" says the following
cause= Exception,
detailMessage= "Unknown Exception" ,
stackTrace=> StackTraceElement[0] ,
suppressedExeptions= Collections$UnmodifiableRandomAccessList,
Update july 13th:
I just tried with an artificial neural network (ANN) too, and it crashes when trying to save.
Error:
Fatal signal 11 (SIGSEGV), code 1, fault addr 0x15a57e688000c in tid
8507 (brandsa.opencv2) debuggerd: handling request: pid=8507
uid=10319 gid=10319 tid=8507
Code:
// Mat data is of size 100*20*CV_32FC1,
// Mat labels is of size 100*1*CV_32FC1
// layerSizes is of size 3*1*CV_8UC1
int[] hiddenLayers = {10};
Mat layerSizes = new Mat(2 + hiddenLayers.length,1,CvType.CV_8U);
layerSizes.put(0, 0, data.width());
for (int l = 0; l< hiddenLayers.length; l++){
layerSizes.put(1 + l, 0,hiddenLayers[l]);}
layerSizes.put(1 + hiddenLayers.length, 0,labels.width());
ANN_MLP ann = ANN_MLP.create();
ann.setLayerSizes(layerSizes);
ann.setActivationFunction(ANN_MLP.SIGMOID_SYM);
ann.train(data, Ml.ROW_SAMPLE , labels);
ann.save("/storage/emulated/0/Pictures/no.rema.priceagent.test/trentann.xml");

Error trying to detect faces in an image with OpenCV JAVA

I have used the code from this tutorial: http://opencvlover.blogspot.co.uk/2012/11/face-detection-in-javacv-using-haar.html
It has been slightly modified to read a different image, and display this image before attempting face detection (line 14). Through this I can confirm that the image is being loaded correctly.
The error occurs later at line 23. Here is the complete error code:
OpenCV Error: Null pointer (Invalid classifier cascade) in cvHaarDetectObjectsForROC,file ..\..\..\..\opencv\modules\objdetect\src\haar.cpp, line 1514
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\objdetect\src\haar.cpp:1514: error: (-27) Invalid classifier cascade in function cvHaarDetectObjectsForROC
at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method)
at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:238)
at FaceDetection.detect(FaceDetection.java:23)
at FaceDetection.main(FaceDetection.java:15)
Here is my complete program:
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_objdetect.*;
public class FaceDetection{
public static final String XML_FILE =
"resources/haarcascade_frontalface_default.xml";
public static void main(String[] args){
IplImage img = cvLoadImage("pic.jpg");
cvShowImage("",img);cvWaitKey(0);
detect(img);
}
public static void detect(IplImage src){
CvHaarClassifierCascade cascade = new
CvHaarClassifierCascade(cvLoad(XML_FILE));
CvMemStorage storage = CvMemStorage.create();
CvSeq sign = cvHaarDetectObjects(
src,
cascade,
storage,
1.5,
3,
CV_HAAR_DO_CANNY_PRUNING);
cvClearMemStorage(storage);
int total_Faces = sign.total();
for(int i = 0; i < total_Faces; i++){
CvRect r = new CvRect(cvGetSeqElem(sign, i));
cvRectangle (
src,
cvPoint(r.x(), r.y()),
cvPoint(r.width() + r.x(), r.height() + r.y()),
CvScalar.RED,
2,
CV_AA,
0);
}
cvShowImage("Result", src);
cvWaitKey(0);
}
}
Anybody know what is causing this error, or how it can be fixed? Thanks!
Solved!
I googled the "haarcascade_frontalface_default.xml", downloaded it and stuck it in my folder in the workspace, took /resources/ off of the filename in the XML string and it works.
Congrats on solving it. However to progress and learn, you must understand what went wrong.
The error occurred because the program cannot find the cascade classifier. I thought you declared the location of the classifier wrongly, but turns out you didn't have the classifier in the first place. You solved that by downloading a sample classifier and using it.
You do not necessarily have to put the classifier in the folder containing the program. You can also put it somewhere else and state the path of where the classifier is located.
I would also recommend you to train your own haar-classifier if you are really into object detection. This will help you understand better how cascade classifier works.

Categories