Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit feefc2b

Browse files
committed
supports URL input
1 parent e4fc66b commit feefc2b

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/main/scala/tensorflow/TensorFlowExample.scala

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package tensorflow
22

3+
import java.awt.image.DataBufferByte
4+
import java.io.{BufferedInputStream, ByteArrayOutputStream}
5+
import java.net.URL
36
import java.nio.file.{Files, Paths}
7+
import javax.imageio.ImageIO
48

59
import tensorflow.model.InceptionV3
610

@@ -9,7 +13,23 @@ object TensorFlowExample {
913
def main(args: Array[String]) {
1014
// image file
1115
val jpgFile = args.headOption.getOrElse("cropped_panda.jpg")
12-
val jpgAsBytes = Files.readAllBytes(Paths.get(jpgFile))
16+
val jpgAsBytes = jpgFile match {
17+
case urlString if urlString.startsWith("http") =>
18+
val url = new URL(urlString)
19+
val in = new BufferedInputStream(url.openStream())
20+
val out = new ByteArrayOutputStream()
21+
val buf = new Array[Byte](1024)
22+
var n = in.read(buf)
23+
while (n != -1) {
24+
out.write(buf, 0, n)
25+
n = in.read(buf)
26+
}
27+
val bytes = out.toByteArray
28+
out.close()
29+
in.close()
30+
bytes
31+
case file => Files.readAllBytes(Paths.get(file))
32+
}
1333

1434
// define the model
1535
val model = new InceptionV3("model")

0 commit comments

Comments
 (0)