Skip to content

Instantly share code, notes, and snippets.

@jaegonlee
Created February 25, 2019 04:37
Show Gist options
  • Save jaegonlee/8a496da088d9e605ca4ac20331a44597 to your computer and use it in GitHub Desktop.
Save jaegonlee/8a496da088d9e605ca4ac20331a44597 to your computer and use it in GitHub Desktop.

Revisions

  1. jaegonlee created this gist Feb 25, 2019.
    112 changes: 112 additions & 0 deletions tensorflowp5.pde
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    import processing.video.*;

    import java.util.ArrayList;
    import java.util.List;
    import org.tensorflow.Graph;
    import org.tensorflow.Session;
    import org.tensorflow.Tensor;
    import org.tensorflow.TensorFlow;

    PImage img;
    PGraphics pg;

    Graph graph;
    Session session;

    Capture cam;

    int index = 0;
    String[] labels;
    String name = "";

    void setup() {
    size(320, 240, P3D);
    pg = createGraphics(224, 224, P3D);
    String value = TensorFlow.version();
    println(value);
    labels = loadStrings("labels.txt");
    img = loadImage("3.jpg");

    cam = new Capture(this, 320, 240);
    cam.start();
    graph = new Graph();
    session = new Session(graph);
    graph.importGraphDef(loadBytes("graph.pb"));
    }

    int i = 0;

    void draw() {
    if (cam.available()) {
    background(255);
    cam.read();
    pg.beginDraw();
    pg.image(cam, 0, 0, 224, 224);
    pg.endDraw();
    image(pg, 0, 0, width, height);

    if (i == 3) {
    name = classify(pg);
    i = 0;
    }
    i++;
    }
    fill(0);
    text(name, 20, 30);
    }

    void mousePressed() {
    // name = classify(index);
    img = loadImage(index+".jpg");
    index++;
    index%=4;
    }

    String classify(PImage img) {

    String s = "";
    float[] probabilities = null;
    img.loadPixels();
    float[] floatValues = new float[224 * 224 * 3];
    for (int i = 0; i < 224 * 224; i++) {
    int val = img.pixels[i];
    // ARGB -> BGR
    floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - 127.0) / 1.0;
    floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - 127.0) / 1.0;
    floatValues[i * 3 + 2] = ((val & 0xFF) - 127.0) / 1.0;
    }

    try {
    Tensor<Float> input = Tensors.create(floatValues);
    Tensor<Float> output =
    session
    .runner()
    .feed("encoded_image_bytes", input)
    .fetch("probabilities")
    .run()
    .get(0)
    .expect(Float.class);
    if (probabilities == null) {
    probabilities = new float[(int) output.shape()[0]];
    }
    output.copyTo(probabilities);
    int label = argmax(probabilities);
    s = labels[label];
    print(labels[label] + " ");
    println(probabilities[label] * 100.0);
    }
    catch (Exception e) {
    print(e);
    }
    return s;
    }

    int argmax(float[] probabilities) {
    int best = 0;
    for (int i = 1; i < probabilities.length; ++i) {
    if (probabilities[i] > probabilities[best]) {
    best = i;
    }
    }
    return best;
    }