Enable loading external binary file as model input

Currently model input can be internal random input or external
image file or image URL.

In this change, we add support for loading external binary file, which
enables the future scenario with RISP4ML pipeline.

Change-Id: I22c4f290608ec9c59713034a5aebeef9a3fd5a4f
diff --git a/build_tools/gen_mlmodel_input.py b/build_tools/gen_mlmodel_input.py
index 117aacb..3345be2 100755
--- a/build_tools/gen_mlmodel_input.py
+++ b/build_tools/gen_mlmodel_input.py
@@ -38,13 +38,19 @@
         raise RuntimeError("Input file %s doesn't exist" % {input_name})
     if len(input_shape) < 3:
         raise ValueError("Input shape < 3 dimensions")
-    resized_img = Image.open(input_name).resize(
-        (input_shape[1], input_shape[2]))
-    input = np.array(resized_img).reshape(np.prod(input_shape))
-    if not is_quant:
-        low = np.min(float_input_range)
-        high = np.max(float_input_range)
-        input = (high - low) * input / 255.0 + low
+    input_ext = os.path.splitext(input_name)[1]
+    if (not input_ext) or (input_ext == '.bin'):
+        with open(input_name, mode='rb') as f:
+            input = np.fromfile(f, dtype=np.uint8 if is_quant else np.float32).reshape(
+                np.prod(input_shape))
+    else:
+        resized_img = Image.open(input_name).resize(
+            (input_shape[1], input_shape[2]))
+        input = np.array(resized_img).reshape(np.prod(input_shape))
+        if not is_quant:
+            low = np.min(float_input_range)
+            high = np.max(float_input_range)
+            input = (high - low) * input / 255.0 + low
     write_binary_file(output_file, input, is_quant)