-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
deepnetts
authored and
deepnetts
committed
Dec 2, 2021
1 parent
07e2bfb
commit c4f296c
Showing
18 changed files
with
1,342 additions
and
1,003 deletions.
There are no files selected for viewing
123 changes: 71 additions & 52 deletions
123
src/main/java/javax/visrec/ri/BufferedImageFactory.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 |
---|---|---|
@@ -1,52 +1,71 @@ | ||
package javax.visrec.ri; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.visrec.ImageFactory; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* {@link ImageFactory} to provide {@link BufferedImage} as return object. | ||
* | ||
*/ | ||
public class BufferedImageFactory implements ImageFactory<BufferedImage> { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BufferedImage getImage(Path path) throws IOException { | ||
BufferedImage img = ImageIO.read(path.toFile()); | ||
if (img == null) { | ||
throw new IOException("Failed to transform Path into BufferedImage due to unknown image encoding"); | ||
} | ||
return img; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BufferedImage getImage(URL file) throws IOException { | ||
BufferedImage img = ImageIO.read(file); | ||
if (img == null) { | ||
throw new IOException("Failed to transform URL into BufferedImage due to unknown image encoding"); | ||
} | ||
return img; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BufferedImage getImage(InputStream file) throws IOException { | ||
BufferedImage img = ImageIO.read(file); | ||
if (img == null) { | ||
throw new IOException("Failed to transform InputStream into BufferedImage due to unknown image encoding"); | ||
} | ||
return img; | ||
} | ||
} | ||
/** | ||
* Visual Recognition API for Java, JSR381 | ||
* Copyright (C) 2020 Zoran Sevarac, Frank Greco | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
package javax.visrec.ri; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.visrec.ImageFactory; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* {@link ImageFactory} to provide {@link BufferedImage} as return object. | ||
* | ||
*/ | ||
public class BufferedImageFactory implements ImageFactory<BufferedImage> { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BufferedImage getImage(Path path) throws IOException { | ||
BufferedImage img = ImageIO.read(path.toFile()); | ||
if (img == null) { | ||
throw new IOException("Failed to transform Path into BufferedImage due to unknown image encoding"); | ||
} | ||
return img; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BufferedImage getImage(URL file) throws IOException { | ||
BufferedImage img = ImageIO.read(file); | ||
if (img == null) { | ||
throw new IOException("Failed to transform URL into BufferedImage due to unknown image encoding"); | ||
} | ||
return img; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public BufferedImage getImage(InputStream file) throws IOException { | ||
BufferedImage img = ImageIO.read(file); | ||
if (img == null) { | ||
throw new IOException("Failed to transform InputStream into BufferedImage due to unknown image encoding"); | ||
} | ||
return img; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/javax/visrec/ri/ml/classification/AbstractImageClassifier.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
19 changes: 19 additions & 0 deletions
19
src/main/java/javax/visrec/ri/ml/classification/FeedForwardNetBinaryClassifier.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
123 changes: 71 additions & 52 deletions
123
src/main/java/javax/visrec/ri/ml/classification/ImageClassifierNetwork.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 |
---|---|---|
@@ -1,52 +1,71 @@ | ||
package javax.visrec.ri.ml.classification; | ||
|
||
import deepnetts.data.ExampleImage; | ||
import deepnetts.net.ConvolutionalNetwork; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Implementation of abstract image classifier for BufferedImage-s using | ||
* Convolutional network form Deep Netts. | ||
*/ | ||
public class ImageClassifierNetwork extends AbstractImageClassifier<BufferedImage, ConvolutionalNetwork> { | ||
|
||
// it seems that these are not used at the end, onlz in builder. Do we need them exposed here__ | ||
private int inputWidth, inputHeight; | ||
|
||
public ImageClassifierNetwork(ConvolutionalNetwork network) { | ||
super(BufferedImage.class, network); | ||
} | ||
|
||
@Override | ||
public Map<String, Float> classify(BufferedImage inputImage) { | ||
// create input for neural network from image | ||
ExampleImage exImage = new ExampleImage(inputImage); | ||
|
||
// get underlying ML model, in this case convolutional network | ||
ConvolutionalNetwork neuralNet = getModel(); | ||
// set neural network input and get outputs | ||
neuralNet.setInput(exImage.getInput()); | ||
float[] outputs = neuralNet.getOutput(); | ||
|
||
// get all class labels with corresponding output larger then classification threshold | ||
Map<String, Float> results = new HashMap<>(); | ||
for (int i = 0; i < outputs.length; i++) { | ||
if (outputs[i] >= getThreshold()) | ||
results.put(neuralNet.getOutputLabel(i), outputs[i]); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
public int getInputWidth() { | ||
return inputWidth; | ||
} | ||
|
||
public int getInputHeight() { | ||
return inputHeight; | ||
} | ||
|
||
} | ||
/** | ||
* Visual Recognition API for Java, JSR381 | ||
* Copyright (C) 2020 Zoran Sevarac, Frank Greco | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along | ||
* with this program; if not, write to the Free Software Foundation, Inc., | ||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
*/ | ||
|
||
package javax.visrec.ri.ml.classification; | ||
|
||
import deepnetts.data.ExampleImage; | ||
import deepnetts.net.ConvolutionalNetwork; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Implementation of abstract image classifier for BufferedImage-s using | ||
* Convolutional network form Deep Netts. | ||
*/ | ||
public class ImageClassifierNetwork extends AbstractImageClassifier<BufferedImage, ConvolutionalNetwork> { | ||
|
||
// it seems that these are not used at the end, onlz in builder. Do we need them exposed here__ | ||
private int inputWidth, inputHeight; | ||
|
||
public ImageClassifierNetwork(ConvolutionalNetwork network) { | ||
super(BufferedImage.class, network); | ||
} | ||
|
||
@Override | ||
public Map<String, Float> classify(BufferedImage inputImage) { | ||
// create input for neural network from image | ||
ExampleImage exImage = new ExampleImage(inputImage); | ||
|
||
// get underlying ML model, in this case convolutional network | ||
ConvolutionalNetwork neuralNet = getModel(); | ||
// set neural network input and get outputs | ||
neuralNet.setInput(exImage.getInput()); | ||
float[] outputs = neuralNet.getOutput(); | ||
|
||
// get all class labels with corresponding output larger then classification threshold | ||
Map<String, Float> results = new HashMap<>(); | ||
for (int i = 0; i < outputs.length; i++) { | ||
if (outputs[i] >= getThreshold()) | ||
results.put(neuralNet.getOutputLabel(i), outputs[i]); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
public int getInputWidth() { | ||
return inputWidth; | ||
} | ||
|
||
public int getInputHeight() { | ||
return inputHeight; | ||
} | ||
|
||
} |
Oops, something went wrong.