Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloads to NativeImageLoader taking org.opencv.core.Mat and String as filename #6459

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public String[] getAllowedFormats() {
return ALLOWED_FORMATS;
}

public INDArray asRowVector(String filename) throws IOException {
return asRowVector(new File(filename));
}

/**
* Convert a file to a row vector
*
Expand Down Expand Up @@ -176,6 +180,10 @@ public INDArray asRowVector(Mat image) throws IOException {
return asMatrix(image).ravel();
}

public INDArray asRowVector(org.opencv.core.Mat image) throws IOException {
return asMatrix(image).ravel();
}

static Mat convert(PIX pix) {
PIX tempPix = null;
if (pix.colormap() != null) {
Expand Down Expand Up @@ -214,6 +222,9 @@ static Mat convert(PIX pix) {
return mat2;
}

public INDArray asMatrix(String filename) throws IOException {
return asMatrix(new File(filename));
}

@Override
public INDArray asMatrix(File f) throws IOException {
Expand Down Expand Up @@ -292,6 +303,10 @@ private Mat streamToMat(InputStream is) throws IOException {

}

public Image asImageMatrix(String filename) throws IOException {
return asImageMatrix(filename);
}

@Override
public Image asImageMatrix(File f) throws IOException {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) {
Expand Down Expand Up @@ -491,6 +506,10 @@ public void asMatrixView(InputStream is, INDArray view) throws IOException {
image.deallocate();
}

public void asMatrixView(String filename, INDArray view) throws IOException {
asMatrixView(new File(filename), view);
}

public void asMatrixView(File f, INDArray view) throws IOException {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) {
asMatrixView(bis, view);
Expand All @@ -501,16 +520,31 @@ public void asMatrixView(Mat image, INDArray view) throws IOException {
transformImage(image, view);
}

public void asMatrixView(org.opencv.core.Mat image, INDArray view) throws IOException {
transformImage(image, view);
}

public INDArray asMatrix(Frame image) throws IOException {
return asMatrix(converter.convert(image));
}

public INDArray asMatrix(org.opencv.core.Mat image) throws IOException {
INDArray ret = transformImage(image, null);

return ret.reshape(ArrayUtil.combine(new long[] {1}, ret.shape()));
}

public INDArray asMatrix(Mat image) throws IOException {
INDArray ret = transformImage(image, null);

return ret.reshape(ArrayUtil.combine(new long[] {1}, ret.shape()));
}

protected INDArray transformImage(org.opencv.core.Mat image, INDArray ret) throws IOException {
Frame f = converter.convert(image);
return transformImage(converter.convert(f), ret);
}

protected INDArray transformImage(Mat image, INDArray ret) throws IOException {
if (imageTransform != null && converter != null) {
ImageWritable writable = new ImageWritable(converter.convert(image));
Expand Down Expand Up @@ -629,6 +663,10 @@ protected Mat scalingIfNeed(Mat image, long dstHeight, long dstWidth) {
}


public ImageWritable asWritable(String filename) throws IOException {
return asWritable(new File(filename));
}

/**
* Convert a file to a INDArray
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.datavec.image.loader;

import org.apache.commons.io.IOUtils;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.indexer.UByteIndexer;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
Expand Down Expand Up @@ -110,7 +111,7 @@ public void testConvertPix() throws Exception {
NativeImageLoader loader5 = new NativeImageLoader(h4, w4, ch4, NativeImageLoader.MultiPageMode.FIRST);
INDArray array6 = null;
try {
array6 = loader5.asMatrix(new ClassPathResource(path2MitosisFile).getFile());
array6 = loader5.asMatrix(new ClassPathResource(path2MitosisFile).getFile().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
fail();
Expand Down Expand Up @@ -141,7 +142,7 @@ public void testConvertPix() throws Exception {
NativeImageLoader loader7 = new NativeImageLoader(h4, w4, ch6, NativeImageLoader.MultiPageMode.MINIBATCH);
INDArray array8 = null;
try {
array8 = loader7.asMatrix(new ClassPathResource(path2MitosisFile).getFile());
array8 = loader7.asMatrix(new ClassPathResource(path2MitosisFile).getFile().getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -154,7 +155,7 @@ public void testConvertPix() throws Exception {

@Test
public void testAsRowVector() throws Exception {
BufferedImage img1 = makeRandomBufferedImage(0, 0, 1);
org.opencv.core.Mat img1 = makeRandomOrgOpenCvCoreMatImage(0, 0, 1);
Mat img2 = makeRandomImage(0, 0, 3);

int w1 = 35, h1 = 79, ch1 = 3;
Expand Down Expand Up @@ -344,6 +345,15 @@ BufferedImage makeRandomBufferedImage(int height, int width, int channels) {
return c2.convert(c.convert(img));
}

org.opencv.core.Mat makeRandomOrgOpenCvCoreMatImage(int height, int width, int channels) {
Mat img = makeRandomImage(height, width, channels);

Loader.load(org.bytedeco.javacpp.opencv_java.class);
OpenCVFrameConverter.ToOrgOpenCvCoreMat c = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();

return c.convert(c.convert(img));
}

Mat makeRandomImage(int height, int width, int channels) {
if (height <= 0) {
height = rng.nextInt() % 100 + 100;
Expand All @@ -366,7 +376,7 @@ Mat makeRandomImage(int height, int width, int channels) {

@Test
public void testAsWritable() throws Exception {
File f0 = new ClassPathResource("datavec-data-image/testimages/class0/0.jpg").getFile();
String f0 = new ClassPathResource("datavec-data-image/testimages/class0/0.jpg").getFile().getAbsolutePath();

NativeImageLoader imageLoader = new NativeImageLoader();
ImageWritable img = imageLoader.asWritable(f0);
Expand Down Expand Up @@ -398,7 +408,7 @@ public void testBufferRealloc() throws Exception {
m.setAccessible(true);

File f1 = new ClassPathResource("datavec-data-image/voc/2007/JPEGImages/000005.jpg").getFile();
File f2 = new ClassPathResource("datavec-data-image/voc/2007/JPEGImages/000007.jpg").getFile();
String f2 = new ClassPathResource("datavec-data-image/voc/2007/JPEGImages/000007.jpg").getFile().getAbsolutePath();

//Start with a large buffer
byte[] buffer = new byte[20*1024*1024];
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@

<javacpp.version>1.4.3-SNAPSHOT</javacpp.version>
<javacpp-presets.version>1.4.2</javacpp-presets.version>
<javacv.version>1.4.2</javacv.version>
<javacv.version>1.4.3-SNAPSHOT</javacv.version>
<openblas.version>0.3.0</openblas.version>
<mkl.version>2018.3</mkl.version>
<mkl-dnn.version>0.16</mkl-dnn.version>
Expand Down