Search in sources :

Example 1 with Vertex

use of com.google.api.services.vision.v1.model.Vertex in project java-docs-samples by GoogleCloudPlatform.

the class FaceDetectApp method annotateWithFace.

/**
 * Annotates an image {@code img} with a polygon defined by {@code face}.
 */
private static void annotateWithFace(BufferedImage img, FaceAnnotation face) {
    Graphics2D gfx = img.createGraphics();
    Polygon poly = new Polygon();
    for (Vertex vertex : face.getFdBoundingPoly().getVertices()) {
        poly.addPoint(vertex.getX(), vertex.getY());
    }
    gfx.setStroke(new BasicStroke(5));
    gfx.setColor(new Color(0x00ff00));
    gfx.draw(poly);
}
Also used : BasicStroke(java.awt.BasicStroke) Vertex(com.google.api.services.vision.v1.model.Vertex) Color(java.awt.Color) Polygon(java.awt.Polygon) Graphics2D(java.awt.Graphics2D)

Example 2 with Vertex

use of com.google.api.services.vision.v1.model.Vertex in project java-docs-samples by GoogleCloudPlatform.

the class FaceDetectAppTest method annotateWithFaces_manyFaces_outlinesFaces.

@Test
public void annotateWithFaces_manyFaces_outlinesFaces() throws Exception {
    // Arrange
    ImmutableList<FaceAnnotation> faces = ImmutableList.of(new FaceAnnotation().setFdBoundingPoly(new BoundingPoly().setVertices(ImmutableList.of(new Vertex().setX(10).setY(5), new Vertex().setX(20).setY(5), new Vertex().setX(20).setY(25), new Vertex().setX(10).setY(25)))), new FaceAnnotation().setFdBoundingPoly(new BoundingPoly().setVertices(ImmutableList.of(new Vertex().setX(60).setY(50), new Vertex().setX(70).setY(60), new Vertex().setX(50).setY(60)))));
    BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    // Act
    FaceDetectApp.annotateWithFaces(img, faces);
    // Assert
    assertThat(img.getRGB(10, 5) & 0x00ff00).named("img face #1 vertex (10, 5) green channel").isEqualTo(0x00ff00);
    assertThat(img.getRGB(20, 5) & 0x00ff00).named("img face #1 vertex (20, 5) green channel").isEqualTo(0x00ff00);
    assertThat(img.getRGB(20, 25) & 0x00ff00).named("img face #1 vertex (20, 25) green channel").isEqualTo(0x00ff00);
    assertThat(img.getRGB(10, 25) & 0x00ff00).named("img face #1 vertex (10, 25) green channel").isEqualTo(0x00ff00);
    assertThat(img.getRGB(60, 50) & 0x00ff00).named("img face #2 vertex (60, 50) green channel").isEqualTo(0x00ff00);
    assertThat(img.getRGB(70, 60) & 0x00ff00).named("img face #2 vertex (70, 60) green channel").isEqualTo(0x00ff00);
    assertThat(img.getRGB(50, 60) & 0x00ff00).named("img face #2 vertex (50, 60) green channel").isEqualTo(0x00ff00);
}
Also used : Vertex(com.google.api.services.vision.v1.model.Vertex) FaceAnnotation(com.google.api.services.vision.v1.model.FaceAnnotation) BoundingPoly(com.google.api.services.vision.v1.model.BoundingPoly) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Aggregations

Vertex (com.google.api.services.vision.v1.model.Vertex)2 BoundingPoly (com.google.api.services.vision.v1.model.BoundingPoly)1 FaceAnnotation (com.google.api.services.vision.v1.model.FaceAnnotation)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 Polygon (java.awt.Polygon)1 BufferedImage (java.awt.image.BufferedImage)1 Test (org.junit.Test)1