-
Notifications
You must be signed in to change notification settings - Fork 1
/
FaceGraphic.kt
235 lines (216 loc) · 8.59 KB
/
FaceGraphic.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.huawei.quickfaceanalyzer.graphic.face
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import com.huawei.hms.mlsdk.face.MLFace
import com.huawei.hms.mlsdk.face.MLFaceKeyPoint
import com.huawei.quickfaceanalyzer.graphic.GraphicOverlay
import java.util.*
import kotlin.math.abs
/**
* Graphic instance for rendering face position, contour, and landmarks within the associated
* graphic overlay view.
*/
class FaceGraphic constructor(overlay: GraphicOverlay, private val face: MLFace) : GraphicOverlay.Graphic(overlay) {
private val facePositionPaint: Paint
private val numColors = COLORS.size
private val idPaints = Array(numColors) { Paint() }
private val boxPaints = Array(numColors) { Paint() }
private val labelPaints = Array(numColors) { Paint() }
init {
val selectedColor = Color.WHITE
facePositionPaint = Paint()
facePositionPaint.color = selectedColor
for (i in 0 until numColors) {
idPaints[i] = Paint()
idPaints[i].color = COLORS[i][0]
idPaints[i].textSize = ID_TEXT_SIZE
boxPaints[i] = Paint()
boxPaints[i].color = COLORS[i][1]
boxPaints[i].style = Paint.Style.STROKE
boxPaints[i].strokeWidth = BOX_STROKE_WIDTH
labelPaints[i] = Paint()
labelPaints[i].color = COLORS[i][1]
labelPaints[i].style = Paint.Style.FILL
}
}
/** Draws the face annotations for position on the supplied canvas. */
override fun draw(canvas: Canvas) {
val face = face ?: return
// Draws a circle at the position of the detected face, with the face's track id below.
val x = translateX(face.border.centerX().toFloat())
val y = translateY(face.border.centerY().toFloat())
canvas.drawCircle(x, y, FACE_POSITION_RADIUS, facePositionPaint)
// Calculate positions.
val left = x - scale(face.border.width() / 2.0f)
val top = y - scale(face.border.height() / 2.0f)
val right = x + scale(face.border.width() / 2.0f)
val bottom = y + scale(face.border.height() / 2.0f)
val lineHeight = ID_TEXT_SIZE + BOX_STROKE_WIDTH
var yLabelOffset = -lineHeight
// Decide color based on face ID
val colorID = abs(face.tracingIdentity % NUM_COLORS)
// Calculate width and height of label box
var textWidth = idPaints[colorID].measureText("ID: " + face.tracingIdentity)
yLabelOffset -= lineHeight
textWidth =
textWidth.coerceAtLeast(
idPaints[colorID].measureText(
String.format(
Locale.US,
"Happiness: %.2f",
face.possibilityOfSmiling()
)
)
)
if (face.opennessOfLeftEye() > 0) {
yLabelOffset -= lineHeight
textWidth =
textWidth.coerceAtLeast(
idPaints[colorID].measureText(
String.format(
Locale.US,
"Left eye: %.2f",
face.opennessOfLeftEye()
)
)
)
}
if (face.opennessOfRightEye() > 0) {
yLabelOffset -= lineHeight
textWidth =
Math.max(
textWidth,
idPaints[colorID].measureText(
String.format(
Locale.US,
"Right eye: %.2f",
face.opennessOfRightEye()
)
)
)
}
// Draw labels
canvas.drawRect(
left - BOX_STROKE_WIDTH,
top + yLabelOffset,
left + textWidth + 2 * BOX_STROKE_WIDTH,
top,
labelPaints[colorID]
)
yLabelOffset += ID_TEXT_SIZE
canvas.drawRect(left, top, right, bottom, boxPaints[colorID])
canvas.drawText(
"ID: " + face.tracingIdentity, left, top + yLabelOffset,
idPaints[colorID]
)
yLabelOffset += lineHeight
// Draws all face contours.
for (contour in face.faceShapeList) {
for (point in contour.coordinatePoints) {
canvas.drawCircle(
translateX(point.x),
translateY(point.y),
FACE_POSITION_RADIUS,
facePositionPaint
)
}
}
// Draws smiling and left/right eye open probabilities.
if (face.emotions.smilingProbability > 0) {
canvas.drawText(
"Smiling: " + String.format(Locale.US, "%.2f", face.emotions.smilingProbability),
left,
top + yLabelOffset,
idPaints[colorID]
)
}
yLabelOffset += lineHeight
val leftEye = face.getFaceKeyPoint(MLFaceKeyPoint.TYPE_LEFT_EYE)
if (leftEye != null && face.opennessOfLeftEye() > 0) {
canvas.drawText(
"Left eye open: " + String.format(Locale.US, "%.2f", face.opennessOfLeftEye()),
translateX(leftEye.point.x) + ID_X_OFFSET,
translateY(leftEye.point.y) + ID_Y_OFFSET,
idPaints[colorID]
)
} else if (leftEye != null && face.opennessOfLeftEye() < 0) {
canvas.drawText(
"Left eye",
left,
top + yLabelOffset,
idPaints[colorID]
)
yLabelOffset += lineHeight
} else if (leftEye == null && face.opennessOfLeftEye() > 0) {
canvas.drawText(
"Left eye open: " + String.format(Locale.US, "%.2f", face.opennessOfLeftEye()),
left,
top + yLabelOffset,
idPaints[colorID]
)
yLabelOffset += lineHeight
}
val rightEye = face.getFaceKeyPoint(MLFaceKeyPoint.TYPE_RIGHT_EYE)
if (rightEye != null && face.opennessOfRightEye() > 0) {
canvas.drawText(
"Right eye open: " + String.format(Locale.US, "%.2f", face.opennessOfRightEye()),
translateX(rightEye.point.x) + ID_X_OFFSET,
translateY(rightEye.point.y) + ID_Y_OFFSET,
idPaints[colorID]
)
} else if (rightEye != null && face.opennessOfRightEye() < 0) {
canvas.drawText(
"Right eye",
left,
top + yLabelOffset,
idPaints[colorID]
)
yLabelOffset += lineHeight
} else if (rightEye == null && face.opennessOfRightEye() > 0) {
canvas.drawText(
"Right eye open: " + String.format(Locale.US, "%.2f", face.opennessOfRightEye()),
left,
top + yLabelOffset,
idPaints[colorID]
)
}
// Draw facial landmarks
drawFaceLandmark(canvas, MLFaceKeyPoint.TYPE_LEFT_EYE)
drawFaceLandmark(canvas, MLFaceKeyPoint.TYPE_RIGHT_EYE)
drawFaceLandmark(canvas, MLFaceKeyPoint.TYPE_LEFT_CHEEK)
drawFaceLandmark(canvas, MLFaceKeyPoint.TYPE_RIGHT_CHEEK)
}
private fun drawFaceLandmark(canvas: Canvas, @MLFaceKeyPoint.Type landmarkType: Int) {
val faceLandmark = face.getFaceKeyPoint(landmarkType)
if (faceLandmark != null) {
canvas.drawCircle(
translateX(faceLandmark.point.x),
translateY(faceLandmark.point.y),
FACE_POSITION_RADIUS,
facePositionPaint
)
}
}
companion object {
private const val FACE_POSITION_RADIUS = 4.0f
private const val ID_TEXT_SIZE = 30.0f
private const val ID_Y_OFFSET = 40.0f
private const val ID_X_OFFSET = -40.0f
private const val BOX_STROKE_WIDTH = 5.0f
private const val NUM_COLORS = 10
private val COLORS =
arrayOf(
intArrayOf(Color.BLACK, Color.WHITE),
intArrayOf(Color.WHITE, Color.MAGENTA),
intArrayOf(Color.BLACK, Color.LTGRAY),
intArrayOf(Color.WHITE, Color.RED),
intArrayOf(Color.WHITE, Color.BLUE),
intArrayOf(Color.WHITE, Color.DKGRAY),
intArrayOf(Color.BLACK, Color.CYAN),
intArrayOf(Color.BLACK, Color.YELLOW),
intArrayOf(Color.WHITE, Color.BLACK),
intArrayOf(Color.BLACK, Color.GREEN)
)
}
}