使用OpenCV进行人脸检测Java Android的摄像头onPreviewFrame()的byte []数据不起作用

时间:2013-11-15 14:06:51

标签: java android opencv face-detection portrait

我正在尝试使用OpenCV java包装器来检测应用中的面部。

1)该应用程序在potrait模式下使用前置摄像头。

2)预览显示在SurfaceView中。

3)我正在使用Android的onPreviewFrame()的byte[]数据并将其更改为OpenCV格式以使用OpenCV检测面部。

4)我将mJavaDetector检测到的坐标发送到View类,以便在drawingView表面的一个实例上绘制。

当我启动应用程序时,没有错误但没有绘制矩形。

Log.v(TAG, "Length of facesArray" + facesArray.length);

内部SurfaceChanged中的

也不会显示。是因为我没有调用onPreviewFrame()或者我的OpenCV实现或其他问题有问题吗?

我在下面附上了我的代码。

 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

         /**if(previewing){
               //mCamera.stopFaceDetection();
               mCamera.stopPreview();
               previewing = false;
              }**/

              if (mCamera != null){
               try {
                   mCamera.setPreviewDisplay(surfaceHolder);
                   mCamera.startPreview();
                   mCamera.setPreviewCallback(new PreviewCallback() {
                        public void onPreviewFrame(byte[] data, Camera mCamera) {
                            Log.d(TAG, "ON Preview frame");
                            int mAbsoluteFaceSize   = 0;


                            int width = surfaceView.getWidth();
                            int height = surfaceView.getHeight();
                            Mat img = new Mat(height, width, CvType.CV_8UC1);
                            Mat gray = new Mat(height, width, CvType.CV_8UC1);
                            img.put(0, 0, data);        

                            Imgproc.cvtColor(img, gray, Imgproc.COLOR_YUV420sp2GRAY);

                            MatOfRect faces = new MatOfRect();

                            if (mJavaDetector != null)
                                mJavaDetector.detectMultiScale(gray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                                        new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());

                            Rect[] facesArray = faces.toArray();

                            Log.v(TAG, "Length of facesArray" + facesArray.length);

                            if(facesArray != null){
                                drawingView.setHaveFace(true);
                            }

                            for (int i = 0; i < facesArray.length; i++){
                                double l = facesArray[i].tl().x;
                                double t = facesArray[i].tl().y;
                                double r = facesArray[i].br().x;
                                double b = facesArray[i].br().y;
                                drawingView.setCoordinates(l, t, r, b);
                            }

                        }
                        });

                /**prompt.setText(String.valueOf(
                  "Max Face: " + mCamera.getParameters().getMaxNumDetectedFaces()));
                  mCamera.startFaceDetection();
                previewing = true;**/
               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
               }
              }
    }

类DrawingView:

private class DrawingView extends View{

          boolean haveFace;
          Paint drawingPaint;
          float left;
          float top;
          float right;
          float bottom;

          public DrawingView(Context context) {
           super(context);
           haveFace = false;
           drawingPaint = new Paint();
           drawingPaint.setColor(Color.GREEN);
           drawingPaint.setStyle(Paint.Style.STROKE); 
           drawingPaint.setStrokeWidth(2);
          }

          public void setHaveFace(boolean h){
           haveFace = h;
          }

          public void setCoordinates(double l,double t, double r, double b){
              left = (float)l;
              top = (float)t;
              right = (float)r;
              bottom = (float)b;
          }
          @Override
          protected void onDraw(Canvas canvas) {

            if(haveFace){
                canvas.drawRect(
                           left, top, right, bottom,  
                           drawingPaint);
            }
            else{
                canvas.drawColor(Color.TRANSPARENT);
            }
          }
         }

onCreate()中的我的drawingView声明:

  drawingView = new DrawingView(this);
    LayoutParams layoutParamsDrawing 
     = new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.FILL_PARENT);
    this.addContentView(drawingView, layoutParamsDrawing);

0 个答案:

没有答案