normalized keypoint container. Detection adds optional keypoint list.

PiperOrigin-RevId: 514624925
This commit is contained in:
MediaPipe Team 2023-03-06 21:58:37 -08:00 committed by Copybara-Service
parent e4ac1a10c2
commit c1b460920c
3 changed files with 132 additions and 1 deletions

View File

@ -38,8 +38,12 @@ android_library(
android_library(
name = "detection",
srcs = ["Detection.java"],
javacopts = [
"-Xep:AndroidJdkLibsChecker:OFF",
],
deps = [
":category",
":normalizedkeypoint",
"//third_party:autovalue",
"@maven//:com_google_guava_guava",
],
@ -92,6 +96,18 @@ android_library(
],
)
android_library(
name = "normalizedkeypoint",
srcs = ["NormalizedKeypoint.java"],
javacopts = [
"-Xep:AndroidJdkLibsChecker:OFF",
],
deps = [
"//third_party:autovalue",
"@maven//:com_google_guava_guava",
],
)
# Expose the java source files for building mediapipe tasks core AAR.
filegroup(
name = "java_src",

View File

@ -18,6 +18,7 @@ import android.graphics.RectF;
import com.google.auto.value.AutoValue;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* Represents one detected object in the results of {@link
@ -39,7 +40,22 @@ public abstract class Detection {
// such as Guava, because it may introduce conflicts with clients who also happen to use those
// libraries. Therefore, instead of using ImmutableList here, we convert the List into
// unmodifiableList
return new AutoValue_Detection(Collections.unmodifiableList(categories), boundingBox);
return new AutoValue_Detection(
Collections.unmodifiableList(categories), boundingBox, Optional.empty());
}
/**
* Creates a {@link Detection} instance from a list of {@link Category} and a bounding box.
*
* @param categories a list of {@link Category} objects that contain category name, display name,
* score, and the label index.
* @param boundingBox a {@link RectF} object to represent the bounding box.
* @param keypoints an optional list of {@link NormalizedKeypoints} associated with the detection.
*/
public static Detection create(
List<Category> categories, RectF boundingBox, Optional<List<NormalizedKeypoint>> keypoints) {
return new AutoValue_Detection(
Collections.unmodifiableList(categories), boundingBox, keypoints);
}
/** A list of {@link Category} objects. */
@ -47,4 +63,12 @@ public abstract class Detection {
/** A {@link RectF} object to represent the bounding box of the detected object. */
public abstract RectF boundingBox();
/**
* An optional list of {@link NormalizedKeypoint} associated with the detection. Keypoints
* represent interesting points related to the detection. For example, the keypoints represent the
* eyes, ear and mouth from face detection model. Or in the template matching detection, e.g.
* KNIFT, they can represent the feature points for template matching.
*/
public abstract Optional<List<NormalizedKeypoint>> keypoints();
}

View File

@ -0,0 +1,91 @@
// Copyright 2023 The MediaPipe Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.mediapipe.tasks.components.containers;
import com.google.auto.value.AutoValue;
import java.util.Objects;
import java.util.Optional;
/**
* Normalized keypoint represents a point in 2D space with x, y coordinates. x and y are normalized
* to [0.0, 1.0] by the image width and height respectively.
*/
@AutoValue
public abstract class NormalizedKeypoint {
private static final float TOLERANCE = 1e-6f;
/**
* Creates a {@link NormalizedKeypoint} instance from normalized x and y coordinates.
*
* @param x the x coordinates of the normalized keypoint.
* @param y the y coordinates of the normalized keypoint.
*/
public static NormalizedKeypoint create(float x, float y) {
return new AutoValue_NormalizedKeypoint(x, y, Optional.empty(), Optional.empty());
}
/**
* Creates a {@link NormalizedKeypoint} instance from normalized x and y coordinates, and the
* optional label and keypoint score.
*
* @param x the x coordinates of the normalized keypoint.
* @param y the y coordinates of the normalized keypoint.
* @param label optional label of the keypoint.
* @param score optional score of the keypoint.
*/
public static NormalizedKeypoint create(
float x, float y, Optional<String> label, Optional<Float> score) {
return new AutoValue_NormalizedKeypoint(x, y, label, score);
}
// The x coordinates of the normalized keypoint.
public abstract float x();
// The y coordinates of the normalized keypoint.
public abstract float y();
// optional label of the keypoint.
public abstract Optional<String> label();
// optional score of the keypoint.
public abstract Optional<Float> score();
@Override
public final boolean equals(Object o) {
if (!(o instanceof NormalizedKeypoint)) {
return false;
}
NormalizedKeypoint other = (NormalizedKeypoint) o;
return Math.abs(other.x() - this.x()) < TOLERANCE && Math.abs(other.x() - this.y()) < TOLERANCE;
}
@Override
public final int hashCode() {
return Objects.hash(x(), y(), label(), score());
}
@Override
public final String toString() {
return "<Normalized Keypoint (x="
+ x()
+ " y="
+ y()
+ " label= "
+ label().orElse("null")
+ " score="
+ (score().isPresent() ? score().get() : "null")
+ ")>";
}
}