Skip to content

Commit

Permalink
Working application
Browse files Browse the repository at this point in the history
Fixed colour space conversions, per colour:

Network input is [-1,1]
Output is [-1, 1]
Display is [0,256]
  • Loading branch information
andrewginns committed Jul 1, 2018
1 parent cceb4ad commit 811fe6d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 87 deletions.
4 changes: 2 additions & 2 deletions android/res/values/base-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
-->

<resources>
<string name="app_name">TensorFlow Demo</string>
<string name="activity_name_stylize">TF Stylize</string>
<string name="app_name">CycleGAN TensorFlow Demo</string>
<string name="activity_name_stylize">CycleGAN-TF</string>
</resources>
140 changes: 55 additions & 85 deletions android/src/org/tensorflow/demo/StylizeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.util.Log;
//import java.io.IOException;
//import java.io.InputStream;
import java.util.ArrayList;
Expand All @@ -59,6 +60,7 @@
import org.tensorflow.demo.env.ImageUtils;
import org.tensorflow.demo.env.Logger;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;

/**
* Sample activity that stylizes the camera preview according to "A Learned Representation For
* Artistic Style" (https://arxiv.org/abs/1610.07629)
Expand Down Expand Up @@ -574,48 +576,53 @@ public void run() {
// of the form [r, g, b, r, g, b, ...].
private void stylizeImage(final Bitmap bitmap) {
++frameNum;

bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

for (int i = 0; i < intValues.length; i++) {
if (DEBUG_MODEL) {
// Create a white square that steps through a black background 1 pixel per frame.
final int centerX = (frameNum + bitmap.getWidth() / 2) % bitmap.getWidth();
final int centerY = bitmap.getHeight() / 2;
final int squareSize = 10;
for (int i = 0; i < intValues.length; ++i) {
final int x = i % bitmap.getWidth();
final int y = i / bitmap.getHeight();
final float val =
Math.abs(x - centerX) < squareSize && Math.abs(y - centerY) < squareSize ? 1.0f : 0.0f;
floatValues[i * 3] = val;
floatValues[i * 3 + 1] = val;
floatValues[i * 3 + 2] = val;
}
} else {
final int test = 55;
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
floatValues[i * 3] = val >> 16 & 0xFF;
floatValues[i * 3 + 1] = val >> 8 & 0xFF;
floatValues[i * 3 + 2] = val & 0xFF;

// floatValues[i * 3] = ((val >> 16) & 0xFF);
floatValues[i * 3] = ((val >> 16) & 0xFF) / (127.5f) - 1f ;

// floatValues[i * 3 + 1] = ((val >> 8) & 0xFF);
floatValues[i * 3 + 1] = ((val >> 8) & 0xFF) / (127.5f) - 1f;

// floatValues[i * 3 + 2] = (val & 0xFF);
floatValues[i * 3 + 2] = (val & 0xFF) / (127.5f) - 1f;

// if(i == test){
// Log.d("ADebugTag", "\nFloatValue1: " + Float.toString(floatValues[i * 3]));
// }
}

// bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
// // red
// Log.d("ADebugTag", "\nValue1: " + Integer.toString((intValues[test] >> 16) & 0xFF));
// Log.d("ADebugTag", "Altered Value1: " + Float.toString(floatValues[test * 3]));
//
// // Turning camera output into RGB https://stackoverflow.com/questions/5669501/how-do-you-get-the-rgb-values-from-a-bitmap-on-an-android-device
// for (int i = 0; i < intValues.length; ++i) {
// final int val = intValues[i];
// floatValues[i * 3] = Color.red(val);
// floatValues[i * 3 + 1] = Color.green(val);
// floatValues[i * 3 + 2] = Color.blue(val);
// }

// if (DEBUG_MODEL) {
// // Create a white square that steps through a black background 1 pixel per frame.
// final int centerX = (frameNum + bitmap.getWidth() / 2) % bitmap.getWidth();
// final int centerY = bitmap.getHeight() / 2;
// final int squareSize = 10;
// for (int i = 0; i < intValues.length; ++i) {
// final int x = i % bitmap.getWidth();
// final int y = i / bitmap.getHeight();
// final float val =
// Math.abs(x - centerX) < squareSize && Math.abs(y - centerY) < squareSize ? 1.0f : 0.0f;
// floatValues[i * 3] = val;
// floatValues[i * 3 + 1] = val;
// floatValues[i * 3 + 2] = val;
// }
// } else {
// for (int i = 0; i < intValues.length; ++i) {
// final int val = intValues[i];
// floatValues[i * 3] = ((val >> 16) & 0xFF) / 255.0f;
// floatValues[i * 3 + 1] = ((val >> 8) & 0xFF) / 255.0f;
// floatValues[i * 3 + 2] = (val & 0xFF) / 255.0f;
// }
// }
// //green
// Log.d("ADebugTag", "\nValue2: " + Integer.toString((intValues[test] >> 8) & 0xFF));
// Log.d("ADebugTag", "Altered Value2: " + Float.toString(floatValues[test * 3 + 1]));
//
// //blue
// Log.d("ADebugTag", "\nValue3: " + Integer.toString(intValues[test] & 0xFF));
// Log.d("ADebugTag", "Altered Value3: " + Float.toString(floatValues[test * 3 + 2]));
}

//Pass the camera bitmap to Tensorflow then retrieve the graph output
// Copy the input data into TensorFlow.
Expand All @@ -629,61 +636,24 @@ private void stylizeImage(final Bitmap bitmap) {
// Copy the data from TensorFlow back into our array.
inferenceInterface.fetch(OUTPUT_NODE, floatValues);

// Turn RGB values back into a Bitmap
for (int i = 0; i < intValues.length; ++i) {
// // red
// float red = (floatValues[i * 3] + 1f) * 127.5f;
//
// // green
// float green = (floatValues[i * 3 + 1] + 1f) * 127.5f;
//
// // blue
// float blue = (floatValues[i * 3 + 2] + 1f) * 127.5f;
intValues[i] =
0xFF000000
//| (intValues[i] & 0xff) << 24
| (((int) (floatValues[i * 3] * 255)) << 16) //red
| (((int) (floatValues[i * 3 + 1] * 255)) << 8) //green
| ((int) (floatValues[i * 3 + 2] * 255)); //blue
| (((int) ((floatValues[i * 3] + 1f) * 127.5f)) << 16)
| (((int) ((floatValues[i * 3 + 1] + 1f) * 127.5f)) << 8)
| ((int) ((floatValues[i * 3 + 2] + 1f) * 127.5f));
}
Log.d("ADebugTag", "\noutValue3: " + Integer.toString(((int) ((floatValues[55 * 3] + 1f) * 127.5f)) << 16));

// // Turn RGB values back into a Bitmap
// for (int i = 0; i < intValues.length; ++i) {
// int red = Color.red((int)(floatValues[i * 3] * 255)); //adjusting these values
// int green = Color.green((int) (floatValues[i * 3 + 1] * 255));
// int blue = Color.blue((int) (floatValues[i * 3 + 2] * 255));
//
// intValues[i] =
// 0xFF000000
// //| (intValues[i] & 0xff) << 24
// | (red & 0xff) << 16
// | (green & 0xff) << 8
// | blue & 0xff;
// }

// Assign new value to output
bitmap.setPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

// int width = bitmap.getWidth();
// int height = bitmap.getHeight();
// for (int i = 0; i < width; ++i) {
// for (int j = 0; j < height; j++) {
// int alpha = (int) floatValues[i];
// int red = (int) floatValues[i * 3 + 2] * 255;
// int green = (int) floatValues[i * 3 + 1] * 255;
// int blue = (int) floatValues[i * 3] * 255;
//
// int newColor = alpha | red | green | blue;
// bitmap.setPixel(i, j, newColor);
// }
// }

//
// int width = bitmap.getWidth();
// int height = bitmap.getHeight();
// for (int i = 0; i < width; i++) {
// for (int j = 0; j < height; j++) {
// int col = bitmap.getPixel(i, j);
// int alpha = col & 0xFF000000;
// int red = (col & 0x00FF0000);
// int green = (col & 0x0000FF00);
// int blue = (col & 0x000000FF);
// int newColor = alpha | red | green | blue;
// bitmap.setPixel(i, j, newColor);
// }
// }
}

//Provides a debug overlay when you press the volume up or down buttons on the device, including
Expand Down

0 comments on commit 811fe6d

Please sign in to comment.