Android,相机预览中的人脸检测

时间:2014-03-19 05:46:01

标签: android opencv camera android-camera face-recognition

当相机处于预览模式时,我需要找到用户的脸形(圆形,椭圆形等),并根据形状显示脸部轮廓。我在我的项目中使用Camera.FaceDetectionListener,我的应用程序支持的最小系统是14.根据文档的说法,此版本应该支持此功能但是没有呀为什么我的设备(SG2)不支持此功能,而其版本为4.1.2。

这是我的代码:

private Camera.FaceDetectionListener mFaceDetectionListener = new Camera.FaceDetectionListener() {
        @Override
        public void onFaceDetection(Camera.Face[] faces, Camera camera) {
            Log.d(TAG, "Face detected.");

            Camera.Face face[] = new Camera.Face[MAX_FACES];
            Log.d(TAG, face.length + " face(s) detected.");

            Rect rect = face[0].rect;
            Log.d(TAG, "width: " + rect.width());
            Log.d(TAG, "height: " + rect.height());
        }
    };

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Get context of application
        mContext = getActivity().getApplicationContext();
        if(!Device.checkCameraHardware(mContext))
            return null;

        // Assign layout to fragment
        View view = inflater.inflate(R.layout.fragment_face_camera, container, false);

        // Create an instance of Camera
        mCamera = getCameraInstance();
        mCamera.setFaceDetectionListener(mFaceDetectionListener);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new CameraPreview(mContext, mCamera);
        FrameLayout preview = (FrameLayout) view.findViewById(R.id.camera_preview);
        preview.addView(mPreview);

        ImageButton captureButton = (ImageButton) view.findViewById(R.id.ibCaptureButton);
        captureButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // get an image from the camera
                        mCamera.takePicture(null, null, mPicture);
                    }
                }
        );

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if(this.getView() == null) {
            Toast.makeText(mContext, "Device doesn't have camera!", Toast.LENGTH_LONG).show();
            return;
        }

        Camera.Parameters param = mCamera.getParameters();
        if(param.getMaxNumDetectedFaces() > 0) {
            // start detecting number of faces
            mCamera.startFaceDetection();
        } else
            Log.d(TAG, "The device doesn't support face detection preview feature.");
    }

@Override
    public void onPause() {
        super.onPause();

        releaseCamera();    // release the camera immediately on pause event
    }

我的输出始终是"设备不支持面部检测预览功能。"。我的代码是对的吗?

根据我的搜索,我发现此功能并不总是可信任的(即使在每个Android设备中都没有> v.14)。所以,我发现OpenCVJavaCv是替代品。

我尝试使用OpenCV的样本,但它最大的缺点是迫使用户下载OpenCV Manager应用程序。请查看this sample以供参考。

还有一些其他库/技术可以在图像中找到面孔,但我这样看。那么,你的建议是什么?我需要让相机处于预览模式并在相机打开时进行人脸识别,然后找到用户的脸部形状并显示正确的脸部轮廓(图像显示为预览顶部的叠加)。

任何建议都将不胜感激。感谢

0 个答案:

没有答案
相关问题