|
| 1 | +/* |
| 2 | + * Copyright (C) The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.android.gms.samples.vision.face.multitracker; |
| 17 | + |
| 18 | +import android.graphics.Canvas; |
| 19 | +import android.graphics.Color; |
| 20 | +import android.graphics.Paint; |
| 21 | + |
| 22 | +import com.google.android.gms.vision.MultiProcessor; |
| 23 | +import com.google.android.gms.vision.Tracker; |
| 24 | +import com.google.android.gms.vision.face.Face; |
| 25 | +import com.google.android.gms.samples.vision.face.multitracker.ui.camera.GraphicOverlay; |
| 26 | + |
| 27 | +/** |
| 28 | + * Factory for creating a tracker and associated graphic to be associated with a new face. The |
| 29 | + * multi-processor uses this factory to create face trackers as needed -- one for each individual. |
| 30 | + */ |
| 31 | +class FaceTrackerFactory implements MultiProcessor.Factory<Face> { |
| 32 | + private GraphicOverlay mGraphicOverlay; |
| 33 | + |
| 34 | + FaceTrackerFactory(GraphicOverlay graphicOverlay) { |
| 35 | + mGraphicOverlay = graphicOverlay; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Tracker<Face> create(Face face) { |
| 40 | + FaceGraphic graphic = new FaceGraphic(mGraphicOverlay); |
| 41 | + return new GraphicTracker<>(mGraphicOverlay, graphic); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Graphic instance for rendering face position, size, and ID within an associated graphic overlay |
| 47 | + * view. |
| 48 | + */ |
| 49 | +class FaceGraphic extends TrackedGraphic<Face> { |
| 50 | + private static final float FACE_POSITION_RADIUS = 10.0f; |
| 51 | + private static final float ID_TEXT_SIZE = 40.0f; |
| 52 | + private static final float ID_Y_OFFSET = 50.0f; |
| 53 | + private static final float ID_X_OFFSET = -50.0f; |
| 54 | + private static final float BOX_STROKE_WIDTH = 5.0f; |
| 55 | + |
| 56 | + private static final int COLOR_CHOICES[] = { |
| 57 | + Color.MAGENTA, |
| 58 | + Color.RED, |
| 59 | + Color.YELLOW |
| 60 | + }; |
| 61 | + private static int mCurrentColorIndex = 0; |
| 62 | + |
| 63 | + private Paint mFacePositionPaint; |
| 64 | + private Paint mIdPaint; |
| 65 | + private Paint mBoxPaint; |
| 66 | + |
| 67 | + private volatile Face mFace; |
| 68 | + |
| 69 | + FaceGraphic(GraphicOverlay overlay) { |
| 70 | + super(overlay); |
| 71 | + |
| 72 | + mCurrentColorIndex = (mCurrentColorIndex + 1) % COLOR_CHOICES.length; |
| 73 | + final int selectedColor = COLOR_CHOICES[mCurrentColorIndex]; |
| 74 | + |
| 75 | + mFacePositionPaint = new Paint(); |
| 76 | + mFacePositionPaint.setColor(selectedColor); |
| 77 | + |
| 78 | + mIdPaint = new Paint(); |
| 79 | + mIdPaint.setColor(selectedColor); |
| 80 | + mIdPaint.setTextSize(ID_TEXT_SIZE); |
| 81 | + |
| 82 | + mBoxPaint = new Paint(); |
| 83 | + mBoxPaint.setColor(selectedColor); |
| 84 | + mBoxPaint.setStyle(Paint.Style.STROKE); |
| 85 | + mBoxPaint.setStrokeWidth(BOX_STROKE_WIDTH); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Updates the face instance from the detection of the most recent frame. Invalidates the |
| 90 | + * relevant portions of the overlay to trigger a redraw. |
| 91 | + */ |
| 92 | + void updateItem(Face face) { |
| 93 | + mFace = face; |
| 94 | + postInvalidate(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Draws the face annotations for position, size, and ID on the supplied canvas. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public void draw(Canvas canvas) { |
| 102 | + Face face = mFace; |
| 103 | + if (face == null) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // Draws a circle at the position of the detected face, with the face's track id below. |
| 108 | + float cx = translateX(face.getPosition().x + face.getWidth() / 2); |
| 109 | + float cy = translateY(face.getPosition().y + face.getHeight() / 2); |
| 110 | + canvas.drawCircle(cx, cy, FACE_POSITION_RADIUS, mFacePositionPaint); |
| 111 | + canvas.drawText("id: " + getId(), cx + ID_X_OFFSET, cy + ID_Y_OFFSET, mIdPaint); |
| 112 | + |
| 113 | + // Draws an oval around the face. |
| 114 | + float xOffset = scaleX(face.getWidth() / 2.0f); |
| 115 | + float yOffset = scaleY(face.getHeight() / 2.0f); |
| 116 | + float left = cx - xOffset; |
| 117 | + float top = cy - yOffset; |
| 118 | + float right = cx + xOffset; |
| 119 | + float bottom = cy + yOffset; |
| 120 | + canvas.drawOval(left, top, right, bottom, mBoxPaint); |
| 121 | + } |
| 122 | +} |
0 commit comments