forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MXNET-1180] Java Image API (apache#13807)
* add java example * add test and change PredictorExample * add image change * Add minor fixes * add License * add predictor Example tests * fix the issue with JUnit test * Satisfy Lint God ʕ •ᴥ•ʔ * update the pom file config * update documentation * add simplified methods
- Loading branch information
1 parent
c40b1dc
commit 1b59f6d
Showing
12 changed files
with
274 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
scala-package/core/src/main/scala/org/apache/mxnet/javaapi/Image.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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.javaapi | ||
// scalastyle:off | ||
import java.awt.image.BufferedImage | ||
// scalastyle:on | ||
import java.io.InputStream | ||
|
||
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 toRGB Whether to convert decoded image | ||
* to mxnet's default RGB format (instead of opencv's default BGR). | ||
* @return NDArray in HWC format with DType [[DType.UInt8]] | ||
*/ | ||
def imDecode(buf: Array[Byte], flag: Int, toRGB: Boolean): NDArray = { | ||
org.apache.mxnet.Image.imDecode(buf, flag, toRGB, None) | ||
} | ||
|
||
def imDecode(buf: Array[Byte]): NDArray = { | ||
imDecode(buf, 1, true) | ||
} | ||
|
||
/** | ||
* Same imageDecode with InputStream | ||
* | ||
* @param inputStream the inputStream of the image | ||
* @param flag Convert decoded image to grayscale (0) or color (1). | ||
* @param toRGB Whether to convert decoded image | ||
* @return NDArray in HWC format with DType [[DType.UInt8]] | ||
*/ | ||
def imDecode(inputStream: InputStream, flag: Int, toRGB: Boolean): NDArray = { | ||
org.apache.mxnet.Image.imDecode(inputStream, flag, toRGB, None) | ||
} | ||
|
||
def imDecode(inputStream: InputStream): NDArray = { | ||
imDecode(inputStream, 1, true) | ||
} | ||
|
||
/** | ||
* 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 toRGB 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 with DType [[DType.UInt8]] | ||
*/ | ||
def imRead(filename: String, flag: Int, toRGB: Boolean): NDArray = { | ||
org.apache.mxnet.Image.imRead(filename, Some(flag), Some(toRGB), None) | ||
} | ||
|
||
def imRead(filename: String): NDArray = { | ||
imRead(filename, 1, true) | ||
} | ||
|
||
/** | ||
* Resize image with OpenCV. | ||
* @param src source image in NDArray | ||
* @param w Width of resized image. | ||
* @param h Height of resized image. | ||
* @param interp Interpolation method (default=cv2.INTER_LINEAR). | ||
* @return org.apache.mxnet.NDArray | ||
*/ | ||
def imResize(src: NDArray, w: Int, h: Int, interp: Integer): NDArray = { | ||
val interpVal = if (interp == null) None else Some(interp.intValue()) | ||
org.apache.mxnet.Image.imResize(src, w, h, interpVal, None) | ||
} | ||
|
||
def imResize(src: NDArray, w: Int, h: Int): NDArray = { | ||
imResize(src, w, h, null) | ||
} | ||
|
||
/** | ||
* Do a fixed crop on the image | ||
* @param src Src image in NDArray | ||
* @param x0 starting x point | ||
* @param y0 starting y point | ||
* @param w width of the image | ||
* @param h height of the image | ||
* @return cropped NDArray | ||
*/ | ||
def fixedCrop(src: NDArray, x0: Int, y0: Int, w: Int, h: Int): NDArray = { | ||
org.apache.mxnet.Image.fixedCrop(src, x0, y0, w, h) | ||
} | ||
|
||
/** | ||
* Convert a NDArray image to a real image | ||
* The time cost will increase if the image resolution is big | ||
* @param src Source image file in RGB | ||
* @return Buffered Image | ||
*/ | ||
def toImage(src: NDArray): BufferedImage = { | ||
org.apache.mxnet.Image.toImage(src) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
scala-package/core/src/test/java/org/apache/mxnet/javaapi/ImageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* 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.javaapi; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import java.io.File; | ||
import java.net.URL; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
public class ImageTest { | ||
|
||
private static String imLocation; | ||
|
||
private static void downloadUrl(String url, String filePath, int maxRetry) throws Exception{ | ||
File tmpFile = new File(filePath); | ||
Boolean success = false; | ||
if (!tmpFile.exists()) { | ||
while (maxRetry > 0 && !success) { | ||
try { | ||
FileUtils.copyURLToFile(new URL(url), tmpFile); | ||
success = true; | ||
} catch(Exception e){ | ||
maxRetry -= 1; | ||
} | ||
} | ||
} else { | ||
success = true; | ||
} | ||
if (!success) throw new Exception("$url Download failed!"); | ||
} | ||
|
||
@BeforeClass | ||
public static void downloadFile() throws Exception { | ||
String tempDirPath = System.getProperty("java.io.tmpdir"); | ||
imLocation = tempDirPath + "/inputImages/Pug-Cookie.jpg"; | ||
downloadUrl("https://s3.amazonaws.com/model-server/inputs/Pug-Cookie.jpg", | ||
imLocation, 3); | ||
} | ||
|
||
@Test | ||
public void testImageProcess() { | ||
NDArray nd = Image.imRead(imLocation, 1, true); | ||
assertArrayEquals(nd.shape().toArray(), new int[]{576, 1024, 3}); | ||
NDArray nd2 = Image.imResize(nd, 224, 224, null); | ||
assertArrayEquals(nd2.shape().toArray(), new int[]{224, 224, 3}); | ||
NDArray cropped = Image.fixedCrop(nd, 0, 0, 224, 224); | ||
Image.toImage(cropped); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.