Created
May 10, 2021 10:30
-
-
Save pengj/e43a759d6391c2d4b09a0faab3448b3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GestureAnalyzer(private val onGestureDetected: (Int) -> Unit) : ImageAnalysis.Analyzer { | |
private val analyzer = MLGestureAnalyzerFactory | |
.getInstance().gestureAnalyzer | |
private var inprogress = false | |
@SuppressLint("UnsafeExperimentalUsageError") | |
override fun analyze(imageProxy: ImageProxy) { | |
if (inprogress) { | |
return | |
} | |
imageProxy.image?.let { | |
inprogress = true | |
analyzer.asyncAnalyseFrame(MLFrame.fromMediaImage(it, 1)) | |
.addOnSuccessListener { list -> | |
if (list.isNotEmpty()) { | |
val category = list.first().category | |
onGestureDetected.invoke(category) | |
Log.d(TAG, "onGestureDetected: $category") | |
} | |
inprogress = false | |
imageProxy.close() | |
}.addOnFailureListener { | |
Log.e(TAG, it.toString()) | |
inprogress = false | |
imageProxy.close() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment