@Test public void jpegTFObjRecTest() throws IOException, TikaException, SAXException { TensorflowImageRecParser p = new TensorflowImageRecParser(); Assume.assumeTrue(p.isAvailable()); try (InputStream stream = loader.getResourceAsStream(CONFIG_FILE_OBJ_REC)) { assert stream != null; Tika tika = new Tika(new TikaConfig(stream)); Metadata metadata = new Metadata(); try (InputStream imageStream = loader.getResourceAsStream(CAT_IMAGE_JPEG)) { Reader reader = tika.parse(imageStream, metadata); List<String> lines = IOUtils.readLines(reader); String text = StringUtils.join(lines, " "); String[] expectedObjects = {"Egyptian cat", "tabby, tabby cat"}; String metaValues = StringUtils.join(metadata.getValues(ObjectRecognitionParser.MD_KEY_OBJ_REC), " "); for (String expectedObject : expectedObjects) { String message = "'" + expectedObject + "' must have been detected"; Assert.assertTrue(message, text.contains(expectedObject)); Assert.assertTrue(message, metaValues.contains(expectedObject)); } } } }
@Override public synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (!recogniser.isAvailable()) { LOG.warn("{} is not available for service", recogniser.getClass()); return; } metadata.set(MD_REC_IMPL_KEY, recogniser.getClass().getName()); long start = System.currentTimeMillis(); List<? extends RecognisedObject> objects = recogniser.recognise(stream, handler, metadata, context); LOG.debug("Found {} objects", objects != null ? objects.size() : 0); LOG.info("Time taken {}ms", System.currentTimeMillis() - start); if (objects != null && !objects.isEmpty()) { int count; List<RecognisedObject> acceptedObjects = new ArrayList<RecognisedObject>(); List<String> xhtmlIds = new ArrayList<String>(); String xhtmlStartVal = null; count = 0; Collections.sort(objects, DESC_CONFIDENCE_SORTER); for (RecognisedObject object : objects) { if (object instanceof CaptionObject) { if (xhtmlStartVal == null) xhtmlStartVal = "captions"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_IMG_CAP, labelAndConfidence); xhtmlIds.add(String.valueOf(count++)); } else { if (xhtmlStartVal == null) xhtmlStartVal = "objects"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_OBJ_REC, labelAndConfidence); xhtmlIds.add(object.getId()); } LOG.info("Add {}", object); acceptedObjects.add(object); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.startElement("ol", "id", xhtmlStartVal); count = 0; for (RecognisedObject object : acceptedObjects) { xhtml.startElement("li", "id", xhtmlIds.get(count++)); String text = String.format(Locale.ENGLISH, " %s [%s](confidence = %f)", object.getLabel(), object.getLabelLang(), object.getConfidence()); xhtml.characters(text); xhtml.endElement("li"); } xhtml.endElement("ol"); xhtml.endDocument(); } else { LOG.warn("NO objects"); metadata.add("no.objects", Boolean.TRUE.toString()); } }
ObjectRecognitionParser extends AbstractParser implements Initializable { @Override public synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (!recogniser.isAvailable()) { LOG.warn("{} is not available for service", recogniser.getClass()); return; } metadata.set(MD_REC_IMPL_KEY, recogniser.getClass().getName()); long start = System.currentTimeMillis(); List<? extends RecognisedObject> objects = recogniser.recognise(stream, handler, metadata, context); LOG.debug("Found {} objects", objects != null ? objects.size() : 0); LOG.info("Time taken {}ms", System.currentTimeMillis() - start); if (objects != null && !objects.isEmpty()) { int count; List<RecognisedObject> acceptedObjects = new ArrayList<RecognisedObject>(); List<String> xhtmlIds = new ArrayList<String>(); String xhtmlStartVal = null; count = 0; Collections.sort(objects, DESC_CONFIDENCE_SORTER); for (RecognisedObject object : objects) { if (object instanceof CaptionObject) { if (xhtmlStartVal == null) xhtmlStartVal = "captions"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_IMG_CAP, labelAndConfidence); xhtmlIds.add(String.valueOf(count++)); } else { if (xhtmlStartVal == null) xhtmlStartVal = "objects"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_OBJ_REC, labelAndConfidence); xhtmlIds.add(object.getId()); } LOG.info("Add {}", object); acceptedObjects.add(object); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.startElement("ol", "id", xhtmlStartVal); count = 0; for (RecognisedObject object : acceptedObjects) { xhtml.startElement("li", "id", xhtmlIds.get(count++)); String text = String.format(Locale.ENGLISH, " %s [%s](confidence = %f)", object.getLabel(), object.getLabelLang(), object.getConfidence()); xhtml.characters(text); xhtml.endElement("li"); } xhtml.endElement("ol"); xhtml.endDocument(); } else { LOG.warn("NO objects"); metadata.add("no.objects", Boolean.TRUE.toString()); } } }
ObjectRecognitionParser extends AbstractParser implements Initializable { @Override public synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (!recogniser.isAvailable()) { LOG.warn("{} is not available for service", recogniser.getClass()); return; } metadata.set(MD_REC_IMPL_KEY, recogniser.getClass().getName()); long start = System.currentTimeMillis(); List<? extends RecognisedObject> objects = recogniser.recognise(stream, handler, metadata, context); LOG.debug("Found {} objects", objects != null ? objects.size() : 0); LOG.info("Time taken {}ms", System.currentTimeMillis() - start); if (objects != null && !objects.isEmpty()) { int count; List<RecognisedObject> acceptedObjects = new ArrayList<RecognisedObject>(); List<String> xhtmlIds = new ArrayList<String>(); String xhtmlStartVal = null; count = 0; Collections.sort(objects, DESC_CONFIDENCE_SORTER); for (RecognisedObject object : objects) { if (object instanceof CaptionObject) { if (xhtmlStartVal == null) xhtmlStartVal = "captions"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_IMG_CAP, labelAndConfidence); xhtmlIds.add(String.valueOf(count++)); } else { if (xhtmlStartVal == null) xhtmlStartVal = "objects"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_OBJ_REC, labelAndConfidence); xhtmlIds.add(object.getId()); } LOG.info("Add {}", object); acceptedObjects.add(object); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.startElement("ol", "id", xhtmlStartVal); count = 0; for (RecognisedObject object : acceptedObjects) { xhtml.startElement("li", "id", xhtmlIds.get(count++)); String text = String.format(Locale.ENGLISH, " %s [%s](confidence = %f)", object.getLabel(), object.getLabelLang(), object.getConfidence()); xhtml.characters(text); xhtml.endElement("li"); } xhtml.endElement("ol"); xhtml.endDocument(); } else { LOG.warn("NO objects"); metadata.add("no.objects", Boolean.TRUE.toString()); } }  }
ObjectRecognitionParser extends AbstractParser implements Initializable { @Override public synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (!recogniser.isAvailable()) { LOG.warn("{} is not available for service", recogniser.getClass()); return; } metadata.set(MD_REC_IMPL_KEY, recogniser.getClass().getName()); long start = System.currentTimeMillis(); List<? extends RecognisedObject> objects = recogniser.recognise(stream, handler, metadata, context); LOG.debug("Found {} objects", objects != null ? objects.size() : 0); LOG.info("Time taken {}ms", System.currentTimeMillis() - start); if (objects != null && !objects.isEmpty()) { int count; List<RecognisedObject> acceptedObjects = new ArrayList<RecognisedObject>(); List<String> xhtmlIds = new ArrayList<String>(); String xhtmlStartVal = null; count = 0; Collections.sort(objects, DESC_CONFIDENCE_SORTER); for (RecognisedObject object : objects) { if (object instanceof CaptionObject) { if (xhtmlStartVal == null) xhtmlStartVal = "captions"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_IMG_CAP, labelAndConfidence); xhtmlIds.add(String.valueOf(count++)); } else { if (xhtmlStartVal == null) xhtmlStartVal = "objects"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_OBJ_REC, labelAndConfidence); xhtmlIds.add(object.getId()); } LOG.info("Add {}", object); acceptedObjects.add(object); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.startElement("ol", "id", xhtmlStartVal); count = 0; for (RecognisedObject object : acceptedObjects) { xhtml.startElement("li", "id", xhtmlIds.get(count++)); String text = String.format(Locale.ENGLISH, " %s [%s](confidence = %f)", object.getLabel(), object.getLabelLang(), object.getConfidence()); xhtml.characters(text); xhtml.endElement("li"); } xhtml.endElement("ol"); xhtml.endDocument(); } else { LOG.warn("NO objects"); metadata.add("no.objects", Boolean.TRUE.toString()); } }  @Field(name = "class") void setRecogniser(String recogniserClass); @Override void initialize(Map<String, Param> params); @Override void checkInitialization(InitializableProblemHandler handler); @Override Set<MediaType> getSupportedTypes(ParseContext context); @Override synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context); }
ObjectRecognitionParser extends AbstractParser implements Initializable { @Override public synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (!recogniser.isAvailable()) { LOG.warn("{} is not available for service", recogniser.getClass()); return; } metadata.set(MD_REC_IMPL_KEY, recogniser.getClass().getName()); long start = System.currentTimeMillis(); List<? extends RecognisedObject> objects = recogniser.recognise(stream, handler, metadata, context); LOG.debug("Found {} objects", objects != null ? objects.size() : 0); LOG.info("Time taken {}ms", System.currentTimeMillis() - start); if (objects != null && !objects.isEmpty()) { int count; List<RecognisedObject> acceptedObjects = new ArrayList<RecognisedObject>(); List<String> xhtmlIds = new ArrayList<String>(); String xhtmlStartVal = null; count = 0; Collections.sort(objects, DESC_CONFIDENCE_SORTER); for (RecognisedObject object : objects) { if (object instanceof CaptionObject) { if (xhtmlStartVal == null) xhtmlStartVal = "captions"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_IMG_CAP, labelAndConfidence); xhtmlIds.add(String.valueOf(count++)); } else { if (xhtmlStartVal == null) xhtmlStartVal = "objects"; String labelAndConfidence = String.format(Locale.ENGLISH, "%s (%.5f)", object.getLabel(), object.getConfidence()); metadata.add(MD_KEY_OBJ_REC, labelAndConfidence); xhtmlIds.add(object.getId()); } LOG.info("Add {}", object); acceptedObjects.add(object); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.startElement("ol", "id", xhtmlStartVal); count = 0; for (RecognisedObject object : acceptedObjects) { xhtml.startElement("li", "id", xhtmlIds.get(count++)); String text = String.format(Locale.ENGLISH, " %s [%s](confidence = %f)", object.getLabel(), object.getLabelLang(), object.getConfidence()); xhtml.characters(text); xhtml.endElement("li"); } xhtml.endElement("ol"); xhtml.endDocument(); } else { LOG.warn("NO objects"); metadata.add("no.objects", Boolean.TRUE.toString()); } }  @Field(name = "class") void setRecogniser(String recogniserClass); @Override void initialize(Map<String, Param> params); @Override void checkInitialization(InitializableProblemHandler handler); @Override Set<MediaType> getSupportedTypes(ParseContext context); @Override synchronized void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context); static final String MD_KEY_OBJ_REC; static final String MD_KEY_IMG_CAP; static final String MD_REC_IMPL_KEY; }