title: Is there a way to get the color of a recognized object inside a picture?
body: <p>I am using <a href="https://www.tensorflow.org/" rel="noreferrer">Tensorflow</a> in order to recognize object in a provided picture , following this <a href="https://www.tensorflow.org/install/install_java" rel="noreferrer">tutorial</a> and using <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java" rel="noreferrer">this repo</a> I succeed to make my program return the object inside a picture .
  For example this is the picture I used as input: </p>
  
  <p><a href="https://i.stack.imgur.com/ua5iz.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/ua5iz.jpg" alt="red-tshirt.jpg"></a></p>
  
  <p>and here's the output of my program :  </p>
  
  <p><a href="https://i.stack.imgur.com/eQDtK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/eQDtK.png" alt="enter image description here"></a></p>
  
  <p>All I want is to get the color of the recognized item (red jersey for the last case),is that possible ?</p>
  
  <p>Here's the code (from the last link just with small changes)</p>
  
  <pre><code>/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  ==============================================================================*/
  
  package com.test.sec.compoment;
  
  import java.io.IOException;
  import java.io.PrintStream;
  import java.nio.charset.Charset;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.util.Arrays;
  import java.util.List;
  import org.tensorflow.DataType;
  import org.tensorflow.Graph;
  import org.tensorflow.Output;
  import org.tensorflow.Session;
  import org.tensorflow.Tensor;
  import org.tensorflow.TensorFlow;
  import org.tensorflow.types.UInt8;
  
  /** Sample use of the TensorFlow Java API to label images using a pre-trained model. */
  public class ImageRecognition {
    private static void printUsage(PrintStream s) {
      final String url =
          "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip";
      s.println(
          "Java program that uses a pre-trained Inception model (http://arxiv.org/abs/1512.00567)");
      s.println("to label JPEG images.");
      s.println("TensorFlow version: " + TensorFlow.version());
      s.println();
      s.println("Usage: label_image &lt;model dir&gt; &lt;image file&gt;");
      s.println();
      s.println("Where:");
      s.println("&lt;model dir&gt; is a directory containing the unzipped contents of the inception model");
      s.println("            (from " + url + ")");
      s.println("&lt;image file&gt; is the path to a JPEG image file");
    }
  
    public void index() {
          String modelDir = "C:/Users/Admin/Downloads/inception5h";
          String imageFile = "C:/Users/Admin/Desktop/red-tshirt.jpg";
  
      byte[] graphDef = readAllBytesOrExit(Paths.get(modelDir, "tensorflow_inception_graph.pb"));
      List&lt;String&gt; labels =
          readAllLinesOrExit(Paths.get(modelDir, "imagenet_comp_graph_label_strings.txt"));
      byte[] imageBytes = readAllBytesOrExit(Paths.get(imageFile));
  
      try (Tensor&lt;Float&gt; image = constructAndExecuteGraphToNormalizeImage(imageBytes)) {
        float[] labelProbabilities = executeInceptionGraph(graphDef, image);
        int bestLabelIdx = maxIndex(labelProbabilities);
        System.out.println(
            String.format("BEST MATCH: %s (%.2f%% likely)",
                labels.get(bestLabelIdx),
                labelProbabilities[bestLabelIdx] * 100f));
      }
    }
  
    private static Tensor&lt;Float&gt; constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) {
      try (Graph g = new Graph()) {
        GraphBuilder b = new GraphBuilder(g);
        // Some constants specific to the pre-trained model at:
        // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
        //
        // - The model was trained with images scaled to 224x224 pixels.
        // - The colors, represented as R, G, B in 1-byte each were converted to
        //   float using (value - Mean)/Scale.
        final int H = 224;
        final int W = 224;
        final float mean = 117f;
        final float scale = 1f;
  
        // Since the graph is being constructed once per execution here, we can use a constant for the
        // input image. If the graph were to be re-used for multiple input images, a placeholder would
        // have been more appropriate.
        final Output&lt;String&gt; input = b.constant("input", imageBytes);
        final Output&lt;Float&gt; output =
            b.div(
                b.sub(
                    b.resizeBilinear(
                        b.expandDims(
                            b.cast(b.decodeJpeg(input, 3), Float.class),
                            b.constant("make_batch", 0)),
                        b.constant("size", new int[] {H, W})),
                    b.constant("mean", mean)),
                b.constant("scale", scale));
        try (Session s = new Session(g)) {
          return s.runner().fetch(output.op().name()).run().get(0).expect(Float.class);
        }
      }
    }
  
    private static float[] executeInceptionGraph(byte[] graphDef, Tensor&lt;Float&gt; image) {
      try (Graph g = new Graph()) {
        g.importGraphDef(graphDef);
        try (Session s = new Session(g);
            Tensor&lt;Float&gt; result =
                s.runner().feed("input", image).fetch("output").run().get(0).expect(Float.class)) {
          final long[] rshape = result.shape();
          if (result.numDimensions() != 2 || rshape[0] != 1) {
            throw new RuntimeException(
                String.format(
                    "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
                    Arrays.toString(rshape)));
          }
          int nlabels = (int) rshape[1];
          return result.copyTo(new float[1][nlabels])[0];
        }
      }
    }
  
    private static int maxIndex(float[] probabilities) {
      int best = 0;
      for (int i = 1; i &lt; probabilities.length; ++i) {
        if (probabilities[i] &gt; probabilities[best]) {
          best = i;
        }
      }
      return best;
    }
  
    private static byte[] readAllBytesOrExit(Path path) {
      try {
        return Files.readAllBytes(path);
      } catch (IOException e) {
        System.err.println("Failed to read [" + path + "]: " + e.getMessage());
        System.exit(1);
      }
      return null;
    }
  
    private static List&lt;String&gt; readAllLinesOrExit(Path path) {
      try {
        return Files.readAllLines(path, Charset.forName("UTF-8"));
      } catch (IOException e) {
        System.err.println("Failed to read [" + path + "]: " + e.getMessage());
        System.exit(0);
      }
      return null;
    }
  
    // In the fullness of time, equivalents of the methods of this class should be auto-generated from
    // the OpDefs linked into libtensorflow_jni.so. That would match what is done in other languages
    // like Python, C++ and Go.
    static class GraphBuilder {
      GraphBuilder(Graph g) {
        this.g = g;
      }
  
      Output&lt;Float&gt; div(Output&lt;Float&gt; x, Output&lt;Float&gt; y) {
        return binaryOp("Div", x, y);
      }
  
      &lt;T&gt; Output&lt;T&gt; sub(Output&lt;T&gt; x, Output&lt;T&gt; y) {
        return binaryOp("Sub", x, y);
      }
  
      &lt;T&gt; Output&lt;Float&gt; resizeBilinear(Output&lt;T&gt; images, Output&lt;Integer&gt; size) {
        return binaryOp3("ResizeBilinear", images, size);
      }
  
      &lt;T&gt; Output&lt;T&gt; expandDims(Output&lt;T&gt; input, Output&lt;Integer&gt; dim) {
        return binaryOp3("ExpandDims", input, dim);
      }
  
      &lt;T, U&gt; Output&lt;U&gt; cast(Output&lt;T&gt; value, Class&lt;U&gt; type) {
        DataType dtype = DataType.fromClass(type);
        return g.opBuilder("Cast", "Cast")
            .addInput(value)
            .setAttr("DstT", dtype)
            .build()
            .&lt;U&gt;output(0);
      }
  
      Output&lt;UInt8&gt; decodeJpeg(Output&lt;String&gt; contents, long channels) {
        return g.opBuilder("DecodeJpeg", "DecodeJpeg")
            .addInput(contents)
            .setAttr("channels", channels)
            .build()
            .&lt;UInt8&gt;output(0);
      }
  
      &lt;T&gt; Output&lt;T&gt; constant(String name, Object value, Class&lt;T&gt; type) {
        try (Tensor&lt;T&gt; t = Tensor.&lt;T&gt;create(value, type)) {
          return g.opBuilder("Const", name)
              .setAttr("dtype", DataType.fromClass(type))
              .setAttr("value", t)
              .build()
              .&lt;T&gt;output(0);
        }
      }
      Output&lt;String&gt; constant(String name, byte[] value) {
        return this.constant(name, value, String.class);
      }
  
      Output&lt;Integer&gt; constant(String name, int value) {
        return this.constant(name, value, Integer.class);
      }
  
      Output&lt;Integer&gt; constant(String name, int[] value) {
        return this.constant(name, value, Integer.class);
      }
  
      Output&lt;Float&gt; constant(String name, float value) {
        return this.constant(name, value, Float.class);
      }
  
      private &lt;T&gt; Output&lt;T&gt; binaryOp(String type, Output&lt;T&gt; in1, Output&lt;T&gt; in2) {
        return g.opBuilder(type, type).addInput(in1).addInput(in2).build().&lt;T&gt;output(0);
      }
  
      private &lt;T, U, V&gt; Output&lt;T&gt; binaryOp3(String type, Output&lt;U&gt; in1, Output&lt;V&gt; in2) {
        return g.opBuilder(type, type).addInput(in1).addInput(in2).build().&lt;T&gt;output(0);
      }
      private Graph g;
    }
  }
  </code></pre>