Skip to content

added callbacks for face detection result in activity #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ dependencies {
implementation "com.otaliastudios:cameraview:1.5.1"

// Android face detector
implementation "com.github.husaynhakeem:android-face-detector:2.0" // Remote
// implementation(project(":facedetector")) // Local
// implementation "com.github.husaynhakeem:android-face-detector:2.0" // Remote
implementation(project(":facedetector")) // Local
}
32 changes: 26 additions & 6 deletions app/src/main/java/husaynhakeem/io/facedetectorapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package husaynhakeem.io.facedetectorapp

import android.os.Bundle
import android.util.Log
import android.util.Size
import androidx.appcompat.app.AppCompatActivity
import com.otaliastudios.cameraview.Facing
import husaynhakeem.io.facedetector.FaceBounds
import husaynhakeem.io.facedetector.FaceDetector
import husaynhakeem.io.facedetector.Frame
import husaynhakeem.io.facedetector.LensFacing
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

private val onFaceDetectionResultListener = object : FaceDetector.OnFaceDetectionResultListener{
override fun onFailure(exception: Exception) {
super.onFailure(exception)
exception.printStackTrace()
Log.e(TAG, "error ${exception.message}")
}

override fun onSuccess(faceBounds: List<FaceBounds>) {
super.onSuccess(faceBounds)
Log.e(TAG,"total faces ${faceBounds.size}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an error, it shouldn't use Log.e, instead an info or debug log should be used.

Also, please make sure to format the file after you're done (a space is missing before the log message).

for (face in faceBounds) {
Log.d(TAG, "face ${face.id} ${face.box.width()} ${face.box.height()}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One little change here, could you format the log message to be

face ${face.id} [${face.box.width()}, ${face.box.height()}]

}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val lensFacing =
savedInstanceState?.getSerializable(KEY_LENS_FACING) as Facing? ?: Facing.BACK
val lensFacing = Facing.FRONT
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lensFacing is saved in onSaveInstanceState(). Please revert setting it here to use the saved instance state.

setupCamera(lensFacing)
}

Expand All @@ -42,24 +59,27 @@ class MainActivity : AppCompatActivity() {

private fun setupCamera(lensFacing: Facing) {
val faceDetector = FaceDetector(faceBoundsOverlay)
faceDetector.setonFaceDetectionFailureListener(onFaceDetectionResultListener)
viewfinder.facing = lensFacing
viewfinder.rotation = 180f
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you rotating the viewfinder?

viewfinder.addFrameProcessor {
faceDetector.process(
Frame(
data = it.data,
rotation = it.rotation,
size = Size(it.size.width, it.size.height),
format = it.format,
lensFacing = if (viewfinder.facing == Facing.BACK) LensFacing.BACK else LensFacing.FRONT
lensFacing = LensFacing.FRONT
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you set the value of the frame's lensFacing parameter to the variable lensFacing?

)
)
}

toggleCameraButton.setOnClickListener {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert removing these lines.

viewfinder.toggleFacing()
}


}

//111.375 338.5481
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this line and the one below it.


companion object {
private const val TAG = "MainActivity"
private const val KEY_LENS_FACING = "key-lens-facing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Paint
import android.graphics.PointF
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.content.ContextCompat

Expand Down Expand Up @@ -46,6 +47,7 @@ class FaceBoundsOverlay @JvmOverloads constructor(ctx: Context, attrs: Attribute
canvas.drawId(faceBounds.id.toString(), faceBounds.box.center())
canvas.drawBounds(faceBounds.box)
}
Log.e("faces", "draw ${facesBounds.size}")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a log message here makes sense, but instead of an error log message, it should be an info or debug message, so Log.d or Log.i. Also, could you add a static log tag to the class, something like:

companion object {
    private const val TAG = "FaceBoundsOverlay"
}

Then use it for the logging.

Log.d(TAG, "Drawing ${facesBounds.size}")

}

/** Draws an anchor (dot) at the center of a face. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ class FaceDetector(private val faceBoundsOverlay: FaceBoundsOverlay) {
// Correct the detected faces so that they're correctly rendered on the UI, then
// pass them to [faceBoundsOverlay] to be drawn.
val faceBounds = faces.map { face -> face.toFaceBounds(this) }
mainExecutor.execute { faceBoundsOverlay.updateFaces(faceBounds) }
mainExecutor.execute {
faceBoundsOverlay.updateFaces(faceBounds)
onFaceDetectionResultListener?.onSuccess(faceBounds)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this!

}
}
.addOnFailureListener { exception ->
synchronized(lock) {
Expand Down