Skip to content

Commit

Permalink
Modify the Filter constructor to take the resource ID for #29.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Amos committed Jan 2, 2015
1 parent 9435130 commit 197b0af
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 57 deletions.
38 changes: 25 additions & 13 deletions diamond-android-library/src/edu/cmu/cs/diamond/android/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,20 @@ public class Filter {
private OutputStream os;
private File tempDir;

public Filter(FilterEnum type, Context context, String name, String[] args, byte[] blob) throws IOException {
File f = context.getFileStreamPath(context.getResources().getResourceEntryName(type.id));
public Filter(int resourceId, Context context, String name, String[] args, byte[] blob) throws IOException {
Resources r = context.getResources();
String resourceName = r.getResourceEntryName(resourceId);
File f = context.getFileStreamPath(resourceName);

if (!f.exists()) {
InputStream ins = r.openRawResource(resourceId);
byte[] buf = IOUtils.toByteArray(ins);
FileOutputStream fos = context.openFileOutput(resourceName, Context.MODE_PRIVATE);
IOUtils.write(buf, fos);
context.getFileStreamPath(resourceName).setExecutable(true);
fos.close();
}

ProcessBuilder pb = new ProcessBuilder(f.getAbsolutePath());
Map<String,String> env = pb.environment();
tempDir = File.createTempFile("filter", null, context.getCacheDir());
Expand Down Expand Up @@ -72,17 +84,17 @@ public void destroy() {
}
}

public static void loadFilters(Context context) throws IOException {
Resources r = context.getResources();
for (FilterEnum f : FilterEnum.values()) {
InputStream ins = r.openRawResource(f.id);
String name = r.getResourceEntryName(f.id);
byte[] buf = IOUtils.toByteArray(ins);
FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
IOUtils.write(buf, fos);
context.getFileStreamPath(name).setExecutable(true);
}
}
// public static void loadFilters(Context context) throws IOException {
// Resources r = context.getResources();
// for (FilterEnum f : FilterEnum.values()) {
// InputStream ins = r.openRawResource(f.id);
// String name = r.getResourceEntryName(f.id);
// byte[] buf = IOUtils.toByteArray(ins);
// FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
// IOUtils.write(buf, fos);
// context.getFileStreamPath(name).setExecutable(true);
// }
// }

public double process(Map<String, byte[]> m) throws IOException, FilterException {
while (true) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import org.apache.commons.io.IOUtils;

import edu.cmu.cs.diamond.android.Filter;
import edu.cmu.cs.diamond.android.FilterEnum;
import edu.cmu.cs.diamond.android.FilterException;
import edu.cmu.cs.diamond.android.TagEnum;
import edu.cmu.cs.diamond.android.examples.facialrecognition.R;
import edu.cmu.cs.diamond.android.token.*;
import android.app.Activity;
import android.content.Context;
import android.graphics.ImageFormat;
Expand Down Expand Up @@ -54,9 +51,9 @@ public void run() {
InputStream ocvXmlIS = context.getResources().openRawResource(R.raw.haarcascade_frontalface);
Filter rgbFilter, faceFilter;
try {
rgbFilter = new Filter(FilterEnum.RGBIMG, context, "RGB", null, null);
rgbFilter = new Filter(R.raw.rgbimg, context, "RGB", null, null);
byte[] ocvXml = IOUtils.toByteArray(ocvXmlIS);
faceFilter = new Filter(FilterEnum.OCV_FACE, context, "OCVFace",
faceFilter = new Filter(R.raw.ocv_face, context, "OCVFace",
faceFilterArgs, ocvXml);
} catch (IOException e1) {
Log.e(TAG, "Unable to create filter subprocess.");
Expand Down Expand Up @@ -123,13 +120,6 @@ protected void onCreate(Bundle savedInstanceState) {
// mCamera.changeConfiguration(null, null, ImageFormat.NV21);
mClassificationText = (TextView) findViewById(R.id.classificationText);

try {
Filter.loadFilters(context);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

mCamera.setPreviewCallback(previewCallback);
processingThread.start();
}
Expand Down

0 comments on commit 197b0af

Please sign in to comment.