-
Notifications
You must be signed in to change notification settings - Fork 81
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}") | ||
for (face in faceBounds) { | ||
Log.d(TAG, "face ${face.id} ${face.box.width()} ${face.box.height()}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
setupCamera(lensFacing) | ||
} | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
) | ||
) | ||
} | ||
|
||
toggleCameraButton.setOnClickListener { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert removing these lines. |
||
viewfinder.toggleFacing() | ||
} | ||
|
||
|
||
} | ||
|
||
//111.375 338.5481 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this! |
||
} | ||
} | ||
.addOnFailureListener { exception -> | ||
synchronized(lock) { | ||
|
There was a problem hiding this comment.
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).