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 drawGlasses(canvas: Canvas, face: FirebaseVisionFace) { | |
val leftEye = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_EYE) | |
val rightEye = face.getLandmark(FirebaseVisionFaceLandmark.RIGHT_EYE) | |
if (leftEye != null && rightEye != null) { | |
val eyeDistance = leftEye.position.x - rightEye.position.x | |
val delta = (widthScaleFactor * eyeDistance / 2).toInt() | |
val glassesRect = Rect( | |
translateX(leftEye.position.x).toInt() - delta, | |
translateY(leftEye.position.y).toInt() - delta, |
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
/** | |
* Adjusts the x coordinate from the preview's coordinate system to the view coordinate system. | |
*/ | |
private fun translateX(x: Float): Float { | |
return width - scaleX(x) | |
} | |
/** | |
* Adjusts the y coordinate from the preview's coordinate system to the view coordinate system. | |
*/ |
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
widthScaleFactor = canvas.width.toFloat() / previewWidth.toFloat() | |
heightScaleFactor = canvas.height.toFloat() / previewHeight.toFloat() |
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
// Perform face detection | |
detector.detectInImage(firebaseVisionImage).addOnSuccessListener { faceList -> | |
if (faceList.size > 0) { | |
// We just need the first face | |
val face = faceList[0] | |
// Draw the bitmaps on the detected faces | |
} | |
} |
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
// Build a image meta data object | |
val metadata = FirebaseVisionImageMetadata.Builder() | |
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) | |
.setWidth(frame.size.width) | |
.setHeight(frame.size.height) | |
.setRotation(rotation) | |
.build() | |
// Create vision image object, and it will be consumed by FirebaseVisionFaceDetector | |
// for face detection | |
val firebaseVisionImage = FirebaseVisionImage.fromByteArray(frame.data, metadata) |
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
// Obtain the FirebaseVisionFaceDetector instance | |
private val detector = FirebaseVision.getInstance().getVisionFaceDetector(options) |
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
// Initialize the face detection option, and we need all the face landmarks | |
private val options = FirebaseVisionFaceDetectorOptions.Builder() | |
.setLandmarkType(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS) | |
.build() |
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
cameraView.addFrameProcessor { frame -> | |
// Processes frame here | |
} |
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
lifecycle.addObserver(MainActivityLifecycleObserver(camera_view)) | |
//MainActivityLifecycleObserver.class | |
class MainActivityLifecycleObserver(private val cameraView: CameraView) : LifecycleObserver { | |
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME) | |
fun startCamera() { | |
cameraView.start() | |
} |
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
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) { | |
if (requestCode == PERMISION_REQUEST_CODE) { | |
if (android.Manifest.permission.CAMERA == permissions[0] && | |
grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
startFaceProcessor() | |
} | |
} | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
} |