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
// Convert it to an emoji | |
val gesture = when(bestCategory?.categoryName()) { | |
"Thumb_Up" -> "\uD83D\uDC4D" | |
"Thumb_Down" -> "\uD83D\uDC4E" | |
"Pointing_Up" -> "☝\uFE0F" | |
"Open_Palm" -> "✋" | |
"Closed_Fist" -> "✊" | |
"Victory" -> "✌\uFE0F" | |
"ILoveYou" -> "\uD83E\uDD1F" | |
else -> null |
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
private fun handleGestureRecognizerResult(result: GestureRecognizerResult) { | |
// Figure out the most likely gesture | |
val bestCategory = result.gestures() | |
.firstOrNull() | |
?.maxByOrNull { it.score() } | |
} |
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
val imageAnalyzer = ImageAnalysis.Analyzer { image -> | |
... | |
gestureRecognizer.recognizeAsync( | |
BitmapImageBuilder(scaledAndRotatedBitmap).build(), | |
System.currentTimeMillis() | |
) | |
} |
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
val imageAnalyzer = ImageAnalysis.Analyzer { image -> | |
val imageBitmap = image.toBitmap() | |
val scale = 500f / max(image.width, image.height) | |
// Create a bitmap that's scaled as needed for the model, and rotated as needed to match display orientation | |
val scaleAndRotate = Matrix().apply { | |
postScale(scale, scale) | |
postRotate(image.imageInfo.rotationDegrees.toFloat()) | |
} | |
val scaledAndRotatedBmp = Bitmap.createBitmap(imageBitmap, 0, 0, image.width, image.height, scaleAndRotate, true) |
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
private val gestureRecognizer by lazy { | |
val baseOptionsBuilder = BaseOptions.builder().setModelAssetPath("gesture_recognizer.task") | |
val baseOptions = baseOptionsBuilder.build() | |
val optionsBuilder = | |
GestureRecognizer.GestureRecognizerOptions.builder() | |
.setBaseOptions(baseOptions) | |
.setResultListener { result, _ -> handleGestureRecognizerResult(result) } | |
.setRunningMode(RunningMode.LIVE_STREAM) |
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
dependencies { | |
... | |
implementation(libs.tasks.vision) | |
} |
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
@Composable | |
fun CameraPreview( | |
modifier: Modifier = Modifier, | |
imageAnalysisUseCase: ImageAnalysis? | |
) { | |
... | |
fun rebindCameraProvider() { | |
... | |
cameraProvider.bindToLifecycle( |
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 HandGestureViewModel(application: Application): AndroidViewModel(application) { | |
... | |
val imageAnalyzer = ImageAnalysis.Analyzer { image -> | |
Log.v("cameraxdemo", "Received frame for analysis: ${image.width} x ${image.height}") | |
image.close() | |
} | |
} |
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
@Composable | |
fun CameraPreview( | |
modifier: Modifier = Modifier | |
) { | |
val previewUseCase = remember { androidx.camera.core.Preview.Builder().build() } | |
var cameraProvider by remember { mutableStateOf<ProcessCameraProvider?>(null) } | |
val localContext = LocalContext.current |
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
val imageAnalysisUseCase = remember { | |
ImageAnalysis.Builder().build().apply { | |
setAnalyzer(context.mainExecutor, viewModel.imageAnalyzer) | |
} | |
} |
NewerOlder