Skip to content

Commit

Permalink
Fix bug where casing of J2P landmarks was wrong
Browse files Browse the repository at this point in the history
This was sending in snake case instead of camel case. Added test for this.
  • Loading branch information
Adam Harwood committed Feb 27, 2023
1 parent ca416c5 commit fb87819
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class AnalysisClient(

for (nextLandmark in InferenceLandmark.values()) {
inference.keypointForLandmark(nextLandmark)?.let { keypoint ->
json[nextLandmark.value] = mapOf(
json[nextLandmark.camelCase()] = mapOf(
"x" to keypoint.x,
"y" to keypoint.y,
"score" to keypoint.score
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ class FrameInference constructor(
) {
val smoothKeypoints: Map<Int, Keypoint>

val cocoKeypoints = arrayOf(
"nose",
"left_eye",
"right_eye",
"left_ear",
"right_ear",
"left_shoulder",
"right_shoulder",
"left_elbow",
"right_elbow",
"left_wrist",
"right_wrist",
"left_hip",
"right_hip",
"left_knee",
"right_knee",
"left_ankle",
"right_ankle",
)
val cocoLabelToIdx = cocoKeypoints.associateBy ({ it }, { cocoKeypoints.indexOf(it) } )

val cocoPairs = arrayOf(
arrayOf("left_shoulder", "right_shoulder"),
arrayOf("left_shoulder", "left_hip"),
Expand All @@ -56,9 +35,7 @@ class FrameInference constructor(
}

fun keypointForLandmark(landmark: InferenceLandmark): Keypoint? {
return smoothKeypoints[
cocoLabelToIdx[landmark.value]
]
return smoothKeypoints[landmark.cocoIndex()]
}

fun userFacing(): UserFacing {
Expand Down Expand Up @@ -91,7 +68,7 @@ class FrameInference constructor(
val smoothedKeypoints = HashMap<Int, Keypoint>()
for (nextLandmark in InferenceLandmark.values()) {
val previousKeypoint = previousFrame!!.keypointForLandmark(nextLandmark)
val landmarkIndex = cocoLabelToIdx[nextLandmark.value]!!
val landmarkIndex = nextLandmark.cocoIndex()
val currentKeypoint = keypoints[landmarkIndex]

val minScore = 0.01
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,37 @@ enum class InferenceLandmark(val value: String) {
LEFT_KNEE("left_knee"),
RIGHT_KNEE("right_knee"),
LEFT_ANKLE("left_ankle"),
RIGHT_ANKLE("right_ankle"),
RIGHT_ANKLE("right_ankle");

private val cocoKeypoints = arrayOf(
"nose",
"left_eye",
"right_eye",
"left_ear",
"right_ear",
"left_shoulder",
"right_shoulder",
"left_elbow",
"right_elbow",
"left_wrist",
"right_wrist",
"left_hip",
"right_hip",
"left_knee",
"right_knee",
"left_ankle",
"right_ankle",
)
private val cocoLabelToIdx = cocoKeypoints.associateBy ({ it }, { cocoKeypoints.indexOf(it) } )

private val snakeCaseRegex = "_[a-zA-Z]".toRegex()
fun camelCase(): String {
return snakeCaseRegex.replace(value) {
it.value.replace("_","").uppercase()
}
}

fun cocoIndex(): Int {
return cocoLabelToIdx[value]!!
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ai.getguru.androidsdk

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
Expand Down Expand Up @@ -33,7 +34,9 @@ class AnalysisClientTest {
runBlocking {
analysisClient.add(
FrameInference(
emptyMap(),
mapOf(
InferenceLandmark.LEFT_HIP.cocoIndex() to Keypoint(0.5, 0.5, 1.0),
),
null,
0,
0.0,
Expand All @@ -43,6 +46,9 @@ class AnalysisClientTest {

val request = server.takeRequest()
assertTrue(request.path!!.endsWith("/videos/$videoId/j2p"))
val mapType = object : TypeToken<List<Map<String, Any>>>() {}.type
val body: List<Map<String, Any>> = gson.fromJson(request.body.readUtf8(), mapType)
assertTrue(body[0].containsKey(InferenceLandmark.LEFT_HIP.camelCase()))
}
}
}

0 comments on commit fb87819

Please sign in to comment.