Search in sources :

Example 1 with LocationInfo

use of com.google.cloud.vision.v1.LocationInfo in project aalto-xml by FasterXML.

the class TestStreamReader method test.

@SuppressWarnings({ "unchecked", "resource" })
protected int test(File file) throws Exception {
    XMLInputFactory f = getFactory();
    System.out.print("Coalesce: " + f.getProperty(XMLInputFactory.IS_COALESCING));
    System.out.println(", NS-aware: " + f.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE));
    System.out.print("Entity-expanding: " + f.getProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES));
    System.out.println(", validating: " + f.getProperty(XMLInputFactory.IS_VALIDATING));
    // System.out.println(", name interning: "+f.getProperty(XMLInputFactory2.P_INTERN_NAMES));
    int total = 0;
    XMLStreamReader2 sr;
    // Let's deal with gzipped files too?
    /*
        if (file.getName().endsWith(".gz")) {
            System.out.println("[gzipped input file!]");
            sr = f.createXMLStreamReader
                (new InputStreamReader(new GZIPInputStream
                                       (new FileInputStream(file)), "UTF-8"));
        } else {
            sr = f.createXMLStreamReader(new FileInputStream(file));
            //sr = f.createXMLStreamReader(new FileReader(file));
        }
        */
    /*
        {
            byte[] data = readData(file);
            sr = f.createXMLStreamReader(new Stax2ByteArraySource(data, 0, data.length));
            System.out.println("File '"+file+"', read "+data.length+" bytes in.");
        }
        */
    sr = (XMLStreamReader2) f.createXMLStreamReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    // sr = f.createXMLStreamReader(new FileInputStream(file));
    System.out.println("SR; name interning: " + sr.getProperty(XMLInputFactory2.P_INTERN_NAMES));
    System.out.println("SR; URI interning: " + sr.getProperty(XMLInputFactory2.P_INTERN_NS_URIS));
    int type = sr.getEventType();
    System.out.println("START: version = '" + sr.getVersion() + "', xml-encoding = '" + sr.getCharacterEncodingScheme() + "', input encoding = '" + sr.getEncoding() + "'");
    while (type != END_DOCUMENT) {
        type = sr.next();
        // so it won't be optimized out...
        total += type;
        // boolean hasName = sr.hasName();
        System.out.print("[" + type + "]");
        if (sr.hasText()) {
            String text = sr.getText();
            // to prevent dead code elimination
            total += text.length();
            if (type == CHARACTERS || type == CDATA || type == COMMENT) {
                System.out.println(" Text(" + text.length() + " = '" + text + "'.");
                if (text.length() >= 1) {
                    System.out.println(" [first char code: 0x" + Integer.toHexString(text.charAt(0)) + "]");
                }
                LocationInfo li = sr.getLocationInfo();
                System.out.println(" [Loc, start: " + li.getStartLocation() + ", end: " + li.getEndLocation() + "]");
            } else if (type == SPACE) {
                System.out.print(" Ws = '" + text + "'.");
                char c = (text.length() == 0) ? ' ' : text.charAt(text.length() - 1);
                if (c != '\r' && c != '\n') {
                    System.out.println();
                }
            } else if (type == DTD) {
                System.out.println(" DTD;");
                List<Object> entities = (List<Object>) sr.getProperty("javax.xml.stream.entities");
                List<Object> notations = (List<Object>) sr.getProperty("javax.xml.stream.notations");
                int entCount = (entities == null) ? -1 : entities.size();
                int notCount = (notations == null) ? -1 : notations.size();
                System.out.print("  (" + entCount + " entities, " + notCount + " notations), sysid ");
                System.out.print(", declaration = <<");
                System.out.print(text);
                System.out.println(">>");
            } else if (type == ENTITY_REFERENCE) {
                // entity ref
                System.out.println(" Entity ref: &" + sr.getLocalName() + " -> '" + sr.getText() + "'.");
            // hasName = false; // to suppress further output
            } else {
                // PI?
                ;
            }
        }
        if (type == PROCESSING_INSTRUCTION) {
            System.out.println(" PI target = '" + sr.getPITarget() + "'.");
            System.out.println(" PI data = '" + sr.getPIData() + "'.");
        } else if (type == START_ELEMENT) {
            String prefix = sr.getPrefix();
            System.out.print('<');
            if (prefix != null && prefix.length() > 0) {
                System.out.print(prefix);
                System.out.print(':');
            }
            System.out.print(sr.getLocalName());
            // System.out.println("[first char 0x"+Integer.toHexString(sr.getLocalName().charAt(0))+"]");
            System.out.print(" [QNameNS: " + sr.getName().getNamespaceURI() + "]");
            System.out.print(" {ns '");
            System.out.print(sr.getNamespaceURI());
            System.out.print("'}> ");
            int count = sr.getAttributeCount();
            int nsCount = sr.getNamespaceCount();
            System.out.println(" [" + nsCount + " ns, " + count + " attrs]");
            // debugging:
            for (int i = 0; i < nsCount; ++i) {
                System.out.println(" ns#" + i + ": '" + sr.getNamespacePrefix(i) + "' -> '" + sr.getNamespaceURI(i) + "'");
            }
            for (int i = 0; i < count; ++i) {
                String val = sr.getAttributeValue(i);
                System.out.print(" attr#" + i + ": " + sr.getAttributePrefix(i) + ":" + sr.getAttributeLocalName(i) + " (" + sr.getAttributeNamespace(i) + ") -> '" + val + "' [" + (val.length()) + "]");
                System.out.println(sr.isAttributeSpecified(i) ? "[specified]" : "[Default]");
            }
        } else if (type == END_ELEMENT) {
            System.out.print("</");
            String prefix = sr.getPrefix();
            if (prefix != null && prefix.length() > 0) {
                System.out.print(prefix);
                System.out.print(':');
            }
            System.out.print(sr.getLocalName());
            System.out.print(" {ns '");
            System.out.print(sr.getNamespaceURI());
            System.out.print("'}> ");
            int nsCount = sr.getNamespaceCount();
            System.out.println(" [" + nsCount + " ns unbound]");
        } else if (type == START_DOCUMENT) {
            // only for multi-doc mode
            System.out.print("XML-DECL: version = '" + sr.getVersion() + "', xml-decl-encoding = '" + sr.getCharacterEncodingScheme() + "', app-encoding = '" + sr.getEncoding() + "', stand-alone set: " + sr.standaloneSet());
        }
    }
    return total;
}
Also used : XMLStreamReader2(org.codehaus.stax2.XMLStreamReader2) List(java.util.List) LocationInfo(org.codehaus.stax2.LocationInfo)

Example 2 with LocationInfo

use of com.google.cloud.vision.v1.LocationInfo in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarks.

/**
 * Detects landmarks in the specified local image.
 *
 * @param filePath The path to the file to perform landmark detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarks(String filePath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) LocationInfo(com.google.cloud.vision.v1.LocationInfo) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 3 with LocationInfo

use of com.google.cloud.vision.v1.LocationInfo in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksUrl.

/**
 * Detects landmarks in the specified URI.
 *
 * @param uri The path to the file to perform landmark detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarksUrl(String uri, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Example 4 with LocationInfo

use of com.google.cloud.vision.v1.LocationInfo in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksGcs.

/**
 * Detects landmarks in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark
 *                detection on.
 * @param out A {@link PrintStream} to write detected landmarks to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.LANDMARK_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)3 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)3 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)3 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)3 Feature (com.google.cloud.vision.v1.Feature)3 Image (com.google.cloud.vision.v1.Image)3 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)3 LocationInfo (com.google.cloud.vision.v1.LocationInfo)3 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)3 ArrayList (java.util.ArrayList)3 ImageSource (com.google.cloud.vision.v1.ImageSource)2 ByteString (com.google.protobuf.ByteString)1 FileInputStream (java.io.FileInputStream)1 List (java.util.List)1 LocationInfo (org.codehaus.stax2.LocationInfo)1 XMLStreamReader2 (org.codehaus.stax2.XMLStreamReader2)1