piyushghai commented on a change in pull request #13237: [MXNET-1182] Predictor 
example
URL: https://github.com/apache/incubator-mxnet/pull/13237#discussion_r233187333
 
 

 ##########
 File path: 
scala-package/examples/src/main/java/org/apache/mxnetexamples/javaapi/infer/predictor/PredictorExample.java
 ##########
 @@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.mxnetexamples.javaapi.infer.predictor;
+
+import org.apache.mxnet.infer.javaapi.Predictor;
+import org.apache.mxnet.javaapi.*;
+import org.kohsuke.args4j.CmdLineParser;
+import org.kohsuke.args4j.Option;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.imageio.ImageIO;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class PredictorExample {
+    @Option(name = "--model-path-prefix", usage = "input model directory and 
prefix of the model")
+    private String modelPathPrefix = "/model/ssd_resnet50_512";
+    @Option(name = "--input-image", usage = "the input image")
+    private String inputImagePath = "/images/dog.jpg";
+
+    final static Logger logger = 
LoggerFactory.getLogger(PredictorExample.class);
+
+    private static BufferedImage loadIamgeFromFile(String inputImagePath) {
+        BufferedImage buf = null;
+        try {
+            buf = ImageIO.read(new File(inputImagePath));
+        } catch (Exception e) {
+            System.err.println(e);
+        }
+        return buf;
+    }
+
+    private static BufferedImage reshapeImage(BufferedImage buf, int newWidth, 
int newHeight) {
+        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, 
BufferedImage.TYPE_INT_RGB);
+        Graphics2D g = resizedImage.createGraphics();
+        g.drawImage(buf, 0, 0, newWidth, newHeight, null);
+        g.dispose();
+        return resizedImage;
+    }
+
+    private static float[] imagePreprocess(BufferedImage buf) {
+        // Get height and width of the image
+        int w = buf.getWidth();
+        int h = buf.getHeight();
+
+        // get an array of integer pixels in the default RGB color mode
+        int[] pixels = buf.getRGB(0, 0, w, h, null, 0, w);
+
+        // 3 times height and width for R,G,B channels
+        float[] result = new float[3 * h * w];
+
+        int row = 0;
+        // copy pixels to array vertically
+        while (row < h) {
+            int col = 0;
+            // copy pixels to array horizontally
+            while (col < w) {
+                int rgb = pixels[row * w + col];
+                // getting red color
+                result[0 * h * w + row * w + col] = (rgb >> 16) & 0xFF;
+                // getting green color
+                result[1 * h * w + row * w + col] = (rgb >> 8) & 0xFF;
+                // getting blue color
+                result[2 * h * w + row * w + col] = rgb & 0xFF;
+                col += 1;
+            }
+            row += 1;
+        }
+        buf.flush();
+        return result;
+    }
+
+    private static String printMaximumClass(float[] probabilities,
+                                            String modelPathPrefix) throws 
IOException {
+        String synsetFilePath = modelPathPrefix.substring(0,
 
 Review comment:
   We should not take the synsetFilePath from the modelPathPrefix.  
   This enforces a hard dependency on the synset file to be present in the 
model folder... 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to