如何绘制矩形到脸部位置?

时间:2018-08-25 08:32:52

标签: android android-camera face-detection

我正在制作一个人脸检测应用程序,通过相机预览中的每一帧,我都获得了该帧中的所有面孔。效果很好,我获得了所有面孔以及每个面孔的地标位置。

现在,我在每个帧上绘制一个矩形到我收到的位置。我有问题,我的矩形与脸部不符

这是屏幕截图:

enter image description here

我认为我收到的职位是图片,而不是在屏幕上。

这是我能找到的代码:

.addOnSuccessListener {
                    val faceId = ArrayList<Int>()
                    //get all face
                    for (i in it) {

                        // landmark (mouth, ears, eyes, cheeks, and nose available)
                        drawLandmarkRect(i)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.BOTTOM_MOUTH)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.LEFT_EAR)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.RIGHT_EAR)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.NOSE_BASE)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.LEFT_MOUTH)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.RIGHT_MOUTH)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.LEFT_CHEEK)
                        drawLandmarkPosition(i, FirebaseVisionFaceLandmark.RIGHT_CHEEK)

                    }
                    activity.graphicOverlay.drawAgain()
                    activity.checkMlCallback = 1
                    imageYUV.close()
                }

这是功能drawLandmarkRectdrawLandmarkPosition的代码:

fun drawLandmarkRect(face: FirebaseVisionFace){
    activity.graphicOverlay.rect.add(face.boundingBox)
}

fun drawLandmarkPosition(face: FirebaseVisionFace, idLandmark: Int){
    val pos = face.getLandmark(idLandmark)?.position
    pos?.let{
        activity.graphicOverlay.pX.add(it.x)
        activity.graphicOverlay.pY.add(it.y)
    }
}

这是我绘制的代码:

class GraphicOverlay(context: Context=): View(context) {

private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
var pX = ArrayList<Float>()
var pY = ArrayList<Float>()
var rect = ArrayList<Rect>()

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    drawCircle(canvas); 
    drawRect(canvas)
    }
}

fun drawCircle(canvas: Canvas){
    for (item in 0 until pX.size){
        canvas.drawCircle(pX[item], pY[item], 5f, paint)
    }
}

fun drawRect(canvas: Canvas){
    paint.apply {
        strokeWidth = 3f
        style = Paint.Style.STROKE
        color = Color.parseColor("#fdc51162")
    }
    for (item in rect){
        canvas.drawRect(item, paint)
    }
}

fun drawAgain(){
    postInvalidate()
  }
}

我该怎么办?

2 个答案:

答案 0 :(得分:0)

喜爱:

问题是在创建newInstance ImageReader时我的宽度,高度和数字的除法:

imageReader = ImageReader.newInstance(camOutputSizeWidth/quality, camOutputSizeHeight/quality, ImageFormat.YUV_420_888, 1)

答案 1 :(得分:-1)

您可以使用“画布”在脸上绘制矩形 检查it,它可能对您有用。