我想在相机预览中检测人脸。
我在 OpenCV 示例中看到了这个例子:
@Override
protected Bitmap processFrame(VideoCapture capture) {
capture.retrieve(mRgba, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA);
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
if (mCascade != null) {
int height = mGray.rows();
int faceSize = Math.round(height * FdActivity.minFaceSize);
List<Rect> faces = new LinkedList<Rect>();
mCascade.detectMultiScale(mGray, faces, 1.1, 2, 2 // TODO: objdetect.CV_HAAR_SCALE_IMAGE
, new Size(faceSize, faceSize));
for (Rect r : faces)
Core.rectangle(mRgba, r.tl(), r.br(), new Scalar(0, 255, 0, 255), 3);
}
Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888);
if (Utils.matToBitmap(mRgba, bmp))
return bmp;
bmp.recycle();
return null;
}
我为我的项目重写了这段代码(从 PreviewCallback 的 onPreviewFrame() 输入 byte[] 数据):
public Highlighting[] get(byte[] data) {
matYuv = new Mat(480, 320, CvType.CV_8UC1);
matYuv.put(0, 0, data);
Imgproc.cvtColor(matYuv, matRgb, Imgproc.COLOR_YUV420sp2RGB, 4);
Highlighting[] hl = null;
Imgproc.cvtColor(matRgb, matGray, Imgproc.COLOR_RGB2GRAY, 0);
if (cascade != null) {
int faceSize = 50;
List<Rect> faces = new LinkedList<Rect>();
cascade.detectMultiScale(matGray, faces, 1.1, 2, 2, new Size(
faceSize, faceSize));
hl = new Highlighting[faces.size()];
int i = 0;
for (Rect r : faces) {
hl[i] = new Highlighting((int) r.tl().x, (int) r.tl().y,
(int) r.br().x, (int) r.br().y, "");
i++;
}
Log.i("FACES", String.valueOf(faces.size()));
}
return hl;
}
但我有问题,我的代码不能像原来一样工作 - 它不能检测人脸。转换字节数组时会出现问题吗?