yzhliu commented on a change in pull request #12995: [MXNET-1180] Scala Image API URL: https://github.com/apache/incubator-mxnet/pull/12995#discussion_r230211920
########## File path: scala-package/core/src/main/scala/org/apache/mxnet/Image.scala ########## @@ -0,0 +1,185 @@ +/* + * 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.mxnet +// scalastyle:off +import java.awt.image.BufferedImage +// scalastyle:on +import java.io.InputStream + +import scala.collection.mutable +import scala.collection.mutable.{ArrayBuffer, ListBuffer} + +/** + * Image API of Scala package + * enable OpenCV feature + */ +object Image { + + /** + * Decode image with OpenCV. + * Note: return image in RGB by default, instead of OpenCV's default BGR. + * @param buf Buffer containing binary encoded image + * @param flag Convert decoded image to grayscale (0) or color (1). + * @param to_rgb Whether to convert decoded image + * to mxnet's default RGB format (instead of opencv's default BGR). + * @return NDArray in HWC format + */ + def imDecode (buf : Array[Byte], flag : Int, + to_rgb : Boolean, + out : Option[NDArray]) : NDArray = { + val nd = NDArray.array(buf.map(_.toFloat), Shape(buf.length)) + val byteND = NDArray.api.cast(nd, "uint8") + val args : ListBuffer[Any] = ListBuffer() + val map : mutable.Map[String, Any] = mutable.Map() + args += byteND + map("flag") = flag + map("to_rgb") = to_rgb + if (out.isDefined) map("out") = out.get + NDArray.genericNDArrayFunctionInvoke("_cvimdecode", args, map.toMap) + } + + /** + * Same imageDecode with InputStream + * @param inputStream the inputStream of the image + * @return NDArray in HWC format + */ + def imDecode (inputStream : InputStream, flag : Int = 1, + to_rgb : Boolean = true, + out : Option[NDArray] = None) : NDArray = { + val buffer = new Array[Byte](2048) + val arrBuffer = ArrayBuffer[Byte]() + var length = 0 + while (length != -1) { + length = inputStream.read(buffer) + if (length != -1) arrBuffer ++= buffer.slice(0, length) + } + imDecode(arrBuffer.toArray, flag, to_rgb, out) + } + + /** + * Read and decode image with OpenCV. + * Note: return image in RGB by default, instead of OpenCV's default BGR. + * @param filename Name of the image file to be loaded. + * @param flag Convert decoded image to grayscale (0) or color (1). + * @param to_rgb Whether to convert decoded image to mxnet's default RGB format + * (instead of opencv's default BGR). + * @return org.apache.mxnet.NDArray in HWC format + */ + def imRead (filename : String, flag : Option[Int] = None, + to_rgb : Option[Boolean] = None, + out : Option[NDArray] = None) : NDArray = { Review comment: functionality is good to me. minor comment on the code style, try to make it more compact and consistent across the package: remove space between function name and `(`, remove space between argument and `:`, align the indent for arguments. So as others above and below. ---------------------------------------------------------------- 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
