Android:实时使用传感器检测眼睛的移动

时间:2017-04-15 08:36:37

标签: android background android-camera face-detection proximitysensor

我正在准备一个Android应用程序,我必须检测到眼睛的运动。不知何故,我能够在图像上实现上述功能,但我想在现场直播中使用它。

如果我们可以使用接近传感器来检测眼睛,我无法理解。就像smartStay功能一样。

请建议实施相同的想法。

2 个答案:

答案 0 :(得分:1)

我们可以使用前置摄像头来检测眼睛和眼睛的眨眼。使用Vision api检测眼睛。

眼动追踪代码:

public class FaceTracker extends Tracker<Face> {

private static final float PROB_THRESHOLD = 0.7f;
private static final String TAG = FaceTracker.class.getSimpleName();
private boolean leftClosed;
private boolean rightClosed;

@Override
public void onUpdate(Detector.Detections<Face> detections, Face face) {
    if (leftClosed && face.getIsLeftEyeOpenProbability() > PROB_THRESHOLD) {
        leftClosed = false;
    } else if (!leftClosed &&  face.getIsLeftEyeOpenProbability() < PROB_THRESHOLD){
        leftClosed = true;
    }
    if (rightClosed && face.getIsRightEyeOpenProbability() > PROB_THRESHOLD) {
        rightClosed = false;
    } else if (!rightClosed && face.getIsRightEyeOpenProbability() < PROB_THRESHOLD) {
        rightClosed = true;
    }

    if (leftClosed && !rightClosed) {
        EventBus.getDefault().post(new LeftEyeClosedEvent());
    } else if (rightClosed && !leftClosed) {
        EventBus.getDefault().post(new RightEyeClosedEvent());
    } else if (!leftClosed && !rightClosed) {
        EventBus.getDefault().post(new NeutralFaceEvent());
    }
}
}


//method to call the FaceTracker 
 private void createCameraResources() {
    Context context = getApplicationContext();

    // create and setup the face detector
    mFaceDetector = new FaceDetector.Builder(context)
            .setProminentFaceOnly(true) // optimize for single, relatively large face
            .setTrackingEnabled(true) // enable face tracking
            .setClassificationType(/* eyes open and smile */ FaceDetector.ALL_CLASSIFICATIONS)
            .setMode(FaceDetector.FAST_MODE) // for one face this is OK
            .build();

    // now that we've got a detector, create a processor pipeline to receive the detection
    // results
    mFaceDetector.setProcessor(new LargestFaceFocusingProcessor(mFaceDetector, new FaceTracker()));

    // operational...?
    if (!mFaceDetector.isOperational()) {
        Log.w(TAG, "createCameraResources: detector NOT operational");
    } else {
        Log.d(TAG, "createCameraResources: detector operational");
    }

    // Create camera source that will capture video frames
    // Use the front camera
    mCameraSource = new CameraSource.Builder(this, mFaceDetector)
            .setRequestedPreviewSize(640, 480)
            .setFacing(CameraSource.CAMERA_FACING_FRONT)
            .setRequestedFps(30f)
            .build();
}

答案 1 :(得分:0)

不,您无法使用接近传感器进行眼睛检测或跟踪。给OpenCV一个机会。
链接:OpenCv github:OpenCv github