脸部周围的矩形未显示

时间:2019-09-07 02:35:31

标签: java android face-recognition opencv4android

我已经使用OpenCV库在android studio中创建了人脸识别应用。该代码运行无错误,调试显示为〜0.994122,但我不知道我的代码中是否有错误,我也没有收到任何错误,但是在过程结束时,它仅显示图像,并且未绘制围绕面部的矩形,这样我就可以确保在imageview中识别出该面部。

首先,我从db获取图像并将其放入Imageview;

   Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
   imageView.setImageBitmap(bmp);
   imageView.setVisibility(View.VISIBLE);

下一步,我将位图转换为Mat;

Mat loadedImage = new Mat();
Bitmap bmp32 = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp32,loadedImage);
Imgproc.cvtColor(loadedImage, loadedImage, Imgproc.COLOR_RGBA2BGR);

然后这就是我试图检测矩形并将其放置在脸上的方式;

Mat imageblob = Dnn.blobFromImage(loadedImage, 1.0, new Size(300, 300), new Scalar(104.0, 177.0, 123.0), true, false, CvType.CV_32F);
detector.setInput(imageblob);
Mat detections = detector.forward();

int cols = loadedImage.cols();
int rows = loadedImage.rows();
double THRESHOLD = 0.55;

detections = detections.reshape(1, (int) detections.total() / 7);

Log.d("EXPERIMENT5:ROWS", detections.rows()+"");

for (int j = 1; j < detections.rows(); ++j) {
double confidence = detections.get(j, 2)[0];
Log.d("EXPERIMENT6", j +" "+confidence+" "+THRESHOLD);
if (confidence > THRESHOLD) {
int left = (int) (detections.get(j, 3)[0] * cols);
int top = (int) (detections.get(j, 4)[0] * rows);
int right = (int) (detections.get(j, 5)[0] * cols);
int bottom = (int) (detections.get(j, 6)[0] * rows);
//rectangle draw
if (left < 0) {
 left = 0;
}
if (top < 0) {
top = 0;
}
if (right < 0) {
right = 0;
}
if (bottom < 0) {
bottom = 0;
}
int xLim = loadedImage.size(1);
int yLim = loadedImage.size(0);
if (left >= xLim) {
left = xLim - 2;
}
if (right >= xLim) {
right = xLim - 2;
}
if (top >= yLim) {
top = yLim - 2;
}
if (bottom >= yLim) {
bottom = yLim - 2;
}

Imgproc.rectangle(loadedImage, new Point(left, top), new Point(right, bottom), new Scalar(255, 255, 0), 2);

                                        }
                                    }
                                }

如果有人用云帮助我,我将非常感激。

此致

0 个答案:

没有答案
相关问题