@Override public List<RecognisedObject> recognise(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { INDArray image = imageLoader.asMatrix(stream); preProcessor.transform(image); INDArray[] output = model.output(false, image); return predict(output[0]); } private List<RecognisedObject> predict(INDArray predictions)
@Override public List<RecognisedObject> recognise( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { INDArray image = preProcessImage(imageLoader.asMatrix(stream)); INDArray scores = graph.outputSingle(image); List<RecognisedObject> result = new ArrayList<>(); for (int i = 0; i < scores.length(); i++) { if (scores.getDouble(i) > minConfidence) { String label = labelMap.get(i); String id = i + ""; result.add(new RecognisedObject(label, labelLang, id, scores.getDouble(i))); LOG.debug("Found Object {}", label); } } return result; } }
@Override public INDArray asMatrix(File f) throws IOException { try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) { return asMatrix(bis); } }
/** * Convert a file to a row vector * * @param f the image to convert * @return the flattened image * @throws IOException */ @Override public INDArray asRowVector(File f) throws IOException { return asMatrix(f).ravel(); }
/** * Returns {@code asMatrix(image).ravel()}. * @see #asMatrix(Object) */ public INDArray asRowVector(Object image) throws IOException { return asMatrix(image).ravel(); }
@Override public INDArray asRowVector(InputStream is) throws IOException { return asMatrix(is).ravel(); }
public INDArray asRowVector(Mat image) throws IOException { return asMatrix(image).ravel(); }
public static String classify(InputStream is) throws IOException { INDArray imageMatrix = loader.asMatrix(is); INDArray[] output = model.output(imageMatrix); StringBuffer sb = new StringBuffer(); Map<String, Double> values = new HashMap<>(); for (int i = 0; i < labels.length; i++) { double result = (double) output[0].getFloat(i); if (result > 0.005) { values.put(labels[i], result * 100); } } // Map<String, Double> sortedMap = MapUtil.sortByValue(values); values.forEach((label, value) -> sb.append(label + ": " + String.format("%.2f", value) + "%\n")); return sb.toString(); }
public INDArray executeArray(ImageWritable image) throws IOException { Random random = null; if (seed != 0) { random = new Random(seed); } ImageWritable currentImage = image; for (ImageTransform transform : transformList) { currentImage = transform.transform(currentImage, random); } NativeImageLoader imageLoader = new NativeImageLoader(); return imageLoader.asMatrix(currentImage); }
/** * Convert ImageWritable to INDArray * * @param writable ImageWritable to convert * @return INDArray * @throws IOException */ public INDArray asMatrix(ImageWritable writable) throws IOException { Mat image = converter.convert(writable.getFrame()); return asMatrix(image); } }
INDArray indArray = imageLoader.asMatrix(file); normalizer.transform(indArray);
private INDArray loadImage(String contentFile) throws IOException { INDArray content = LOADER.asMatrix(new ClassPathResource(contentFile).getFile()); IMAGE_PRE_PROCESSOR.transform(content); return content; }
private INDArray prepareImage(Mat file, int width, int height) throws IOException { NativeImageLoader loader = new NativeImageLoader(height, width, 3); ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1); INDArray indArray = loader.asMatrix(file); imagePreProcessingScaler.transform(indArray); return indArray; }
@Override public INDArray asMatrix(InputStream is) throws IOException { byte[] bytes = IOUtils.toByteArray(is); Mat image = imdecode(new Mat(bytes), CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR); if (image == null || image.empty()) { PIX pix = pixReadMem(bytes, bytes.length); if (pix == null) { throw new IOException("Could not decode image from input stream"); } image = convert(pix); pixDestroy(pix); } INDArray a = asMatrix(image); image.deallocate(); return a; }
INDArray imgNdarray = imageLoader.asMatrix(imgFile);
private INDArray createCombinationImage() throws IOException { INDArray content = LOADER.asMatrix(new ClassPathResource(CONTENT_FILE).getFile()); IMAGE_PRE_PROCESSOR.transform(content); INDArray combination = createCombineImageWithRandomPixels(); combination.muli(NOISE_RATION).addi(content.muli(1 - NOISE_RATION)); return combination; }