Java Code Examples for android.media.FaceDetector#Face

The following examples show how to use android.media.FaceDetector#Face . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitmapUtil.java    From haxsync with GNU General Public License v2.0 6 votes vote down vote up
private static PointF findFaceMid(Bitmap in){
	PointF mid = new PointF();
	Bitmap bitmap565 = in.copy(Bitmap.Config.RGB_565, true);

	FaceDetector fd = new FaceDetector(in.getWidth(), in.getHeight(), 1);
	FaceDetector.Face[] faces = new FaceDetector.Face[1];
	fd.findFaces(bitmap565, faces);
	

	FaceDetector.Face face = faces[0];
	if (face != null){
		try{
			face.getMidPoint(mid);
			return mid;
		} catch (NullPointerException n){}
	}
	return null;

}
 
Example 2
Source File: FaceUtils.java    From RairDemo with Apache License 2.0 5 votes vote down vote up
public static Bitmap detectionFace(Bitmap b) {
    // 检测前必须转化为RGB_565格式。文末有详述连接
    Bitmap bitmap = b.copy(Bitmap.Config.RGB_565, true);
    b.recycle();
    // 设定最大可查的人脸数量
    int MAX_FACES = 5;
    FaceDetector faceDet = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), MAX_FACES);
    // 将人脸数据存储到faceArray 中
    FaceDetector.Face[] faceArray = new FaceDetector.Face[MAX_FACES];
    // 返回找到图片中人脸的数量,同时把返回的脸部位置信息放到faceArray中,过程耗时
    int findFaceCount = faceDet.findFaces(bitmap, faceArray);
    // 获取传回的脸部数组中的第一张脸的信息
    FaceDetector.Face face1 = faceArray[0];
    // 获取双眼的中心点,用一个PointF来接收其x、y坐标
    PointF point = new PointF();
    face1.getMidPoint(point);
    // 获取该部位为人脸的可信度,0~1
    float confidence = face1.confidence();
    // 获取双眼间距
    float eyesDistance = face1.eyesDistance();
    // 获取面部姿势
    // 传入X则获取到x方向上的角度,传入Y则获取到y方向上的角度,传入Z则获取到z方向上的角度
    float angle = face1.pose(FaceDetector.Face.EULER_X);

    // todo 在bitmap上绘制一个Rect框住脸,因为返回的是眼睛位置,所以还要做一些处理

    return bitmap;
}
 
Example 3
Source File: CropImageActivity.java    From YiBo with Apache License 2.0 5 votes vote down vote up
private void handleFace(FaceDetector.Face f) {
    PointF midPoint = new PointF();

    int r = ((int) (f.eyesDistance() * mScale)) * 2;
    f.getMidPoint(midPoint);
    midPoint.x *= mScale;
    midPoint.y *= mScale;

    int midX = (int) midPoint.x;
    int midY = (int) midPoint.y;

    HighlightView hv = new HighlightView(mImageView);

    int width = mBitmap.getWidth();
    int height = mBitmap.getHeight();

    Rect imageRect = new Rect(0, 0, width, height);

    RectF faceRect = new RectF(midX, midY, midX, midY);
    faceRect.inset(-r, -r);
    if (faceRect.left < 0) {
        faceRect.inset(-faceRect.left, -faceRect.left);
    }

    if (faceRect.top < 0) {
        faceRect.inset(-faceRect.top, -faceRect.top);
    }

    if (faceRect.right > imageRect.right) {
        faceRect.inset(faceRect.right - imageRect.right, faceRect.right - imageRect.right);
    }

    if (faceRect.bottom > imageRect.bottom) {
        faceRect.inset(faceRect.bottom - imageRect.bottom, faceRect.bottom - imageRect.bottom);
    }

    hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop, mAspectX != 0 && mAspectY != 0);

    mImageView.add(hv);
}
 
Example 4
Source File: PUtil.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Detect faces in a bitmap", example = "")
@PhonkMethodParam(params = {"Bitmap", "numFaces"})
public int detectFaces(Bitmap bmp, int num_faces) {
    FaceDetector face_detector = new FaceDetector(bmp.getWidth(), bmp.getHeight(), num_faces);
    FaceDetector.Face[] faces = new FaceDetector.Face[num_faces];
    int face_count = face_detector.findFaces(bmp, faces);

    return face_count;
}
 
Example 5
Source File: PreProcessor.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 5 votes vote down vote up
public void setFaces(PreProcessorFactory.PreprocessingMode preprocessingMode) {
    List<Mat> images = getImages();

    PreferencesHelper preferencesHelper = new PreferencesHelper(context);
    if (preferencesHelper.getDetectionMethod()){
        faces = faceDetection.getFaces(images.get(0));
        angle = faceDetection.getAngle();
    } else {
        Mat img = images.get(0);
        FaceDetector faceDetector = new FaceDetector(img.cols(), img.rows(), 1);
        Bitmap bmp = Bitmap.createBitmap(img.cols(), img.rows(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(img, bmp);
        FaceDetector.Face[] facesAndroid = new FaceDetector.Face[1];
        if (faceDetector.findFaces(bmp, facesAndroid) > 0){
            faces = new Rect[facesAndroid.length];
            for (int i=0; i<facesAndroid.length; i++){
                PointF pointF = new PointF();
                facesAndroid[i].getMidPoint(pointF);
                int xWidth = (int) (1.34 * facesAndroid[i].eyesDistance());
                int yWidth = (int) (1.12 * facesAndroid[i].eyesDistance());
                int dist = (int) (2.77 * facesAndroid[i].eyesDistance());
                Rect face = new Rect((int) pointF.x - xWidth, (int) pointF.y - yWidth, dist, dist);
                faces[i] = face;
            }
        }
    }

    if (preprocessingMode == PreProcessorFactory.PreprocessingMode.RECOGNITION && preferencesHelper.getDetectionMethod()){
        // Change the image rotation to the angle where the face was detected
        images.remove(0);
        images.add(faceDetection.getImg());
        setImages(images);
    }
}
 
Example 6
Source File: CropImage.java    From droidddle with Apache License 2.0 5 votes vote down vote up
private void handleFace(FaceDetector.Face f) {
    PointF midPoint = new PointF();

    int r = ((int) (f.eyesDistance() * mScale)) * 2;
    f.getMidPoint(midPoint);
    midPoint.x *= mScale;
    midPoint.y *= mScale;

    int midX = (int) midPoint.x;
    int midY = (int) midPoint.y;

    HighlightView hv = new HighlightView(mImageView);

    int width = mBitmap.getWidth();
    int height = mBitmap.getHeight();

    Rect imageRect = new Rect(0, 0, width, height);

    RectF faceRect = new RectF(midX, midY, midX, midY);
    faceRect.inset(-r, -r);
    if (faceRect.left < 0) {
        faceRect.inset(-faceRect.left, -faceRect.left);
    }

    if (faceRect.top < 0) {
        faceRect.inset(-faceRect.top, -faceRect.top);
    }

    if (faceRect.right > imageRect.right) {
        faceRect.inset(faceRect.right - imageRect.right, faceRect.right - imageRect.right);
    }

    if (faceRect.bottom > imageRect.bottom) {
        faceRect.inset(faceRect.bottom - imageRect.bottom, faceRect.bottom - imageRect.bottom);
    }

    hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop, mAspectX != 0 && mAspectY != 0);

    mImageView.add(hv);
}
 
Example 7
Source File: CameraSurfaceView.java    From Android-Screen-to-Face-Distance-Measurement with MIT License 5 votes vote down vote up
private void updateMeasurement(final FaceDetector.Face currentFace) {
	if (currentFace == null) {
		// _facesFoundInMeasurement--;
		return;
	}

	_foundFace = _currentFaceDetectionThread.getCurrentFace();

	_points.add(new Point(_foundFace.eyesDistance(),
			CALIBRATION_DISTANCE_A4_MM
					* (_distanceAtCalibrationPoint / _foundFace
							.eyesDistance())));

	while (_points.size() > _threashold) {
		_points.remove(0);
	}

	float sum = 0;
	for (Point p : _points) {
		sum += p.getEyeDistance();
	}

	_currentAvgEyeDistance = sum / _points.size();

	_currentDistanceToFace = CALIBRATION_DISTANCE_A4_MM
			* (_distanceAtCalibrationPoint / _currentAvgEyeDistance);

	_currentDistanceToFace = Util.MM_TO_CM(_currentDistanceToFace);

	MeasurementStepMessage message = new MeasurementStepMessage();
	message.setConfidence(currentFace.confidence());
	message.setCurrentAvgEyeDistance(_currentAvgEyeDistance);
	message.setDistToFace(_currentDistanceToFace);
	message.setEyesDistance(currentFace.eyesDistance());
	message.setMeasurementsLeft(_calibrationsLeft);
	message.setProcessTimeForLastFrame(_processTimeForLastFrame);

	MessageHUB.get().sendMessage(MessageHUB.MEASUREMENT_STEP, message);
}
 
Example 8
Source File: CropImage.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unused")
private void handleFace(FaceDetector.Face f) {
	PointF midPoint = new PointF();

	int r = ((int) (f.eyesDistance() * mScale)) * 2;
	f.getMidPoint(midPoint);
	midPoint.x *= mScale;
	midPoint.y *= mScale;

	int midX = (int) midPoint.x;
	int midY = (int) midPoint.y;

	HighlightView hv = new HighlightView(mImageView);

	int width = mBitmap.getWidth();
	int height = mBitmap.getHeight();

	Rect imageRect = new Rect(0, 0, width, height);

	RectF faceRect = new RectF(midX, midY, midX, midY);
	faceRect.inset(-r, -r);
	if (faceRect.left < 0) {
		faceRect.inset(-faceRect.left, -faceRect.left);
	}

	if (faceRect.top < 0) {
		faceRect.inset(-faceRect.top, -faceRect.top);
	}

	if (faceRect.right > imageRect.right) {
		faceRect.inset(faceRect.right - imageRect.right, faceRect.right
				- imageRect.right);
	}

	if (faceRect.bottom > imageRect.bottom) {
		faceRect.inset(faceRect.bottom - imageRect.bottom,
				faceRect.bottom - imageRect.bottom);
	}

	hv.setup(mImageMatrix, imageRect, faceRect, false, true);

	mImageView.add(hv);
}
 
Example 9
Source File: FaceUtils.java    From RairDemo with Apache License 2.0 4 votes vote down vote up
public static Bitmap DetectionBitmap(Bitmap b) {
    Bitmap bitmap = null;
    // 检测前必须转化为RGB_565格式。文末有详述连接
    bitmap = b.copy(Bitmap.Config.RGB_565, true);
    b.recycle();
    // 设置你想检测的数量,数值越大错误率越高,所以需要置信度来判断,但有时候置信度也会出问题
    int MAX_FACES = 30;// I found it can detect number of face at least 27,
    FaceDetector faceDet = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), MAX_FACES);
    // 将人脸数据存储到faceArray中
    FaceDetector.Face[] faceArray = new FaceDetector.Face[MAX_FACES];
    // 返回找到图片中人脸的数量,同时把返回的脸部位置信息放到faceArray中,过程耗时,图片越大耗时越久
    int findFaceNum = faceDet.findFaces(bitmap, faceArray);
    Log.e("tag", "找到脸部数量:" + findFaceNum);
    // 获取传回的第一张脸信息
    FaceDetector.Face face1 = faceArray[0];
    // 获取双眼的中心点,用一个PointF来接收其x、y坐标
    PointF point = new PointF();
    face1.getMidPoint(point);
    // 获取该部位为人脸的可信度,0~1
    float confidence = face1.confidence();
    // 获取双眼的间距
    float eyesDistance = face1.eyesDistance();
    // 传入X则获取到x方向上的角度,传入Y则获取到y方向上的角度,传入Z则获取到z方向上的角度
    float angle = face1.pose(FaceDetector.Face.EULER_X);
    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setStrokeWidth(1);
    p.setStyle(Paint.Style.STROKE);
    p.setColor(Color.GREEN);
    Canvas canvas = new Canvas(bitmap);
    PointF pf = new PointF();
    RectF r = new RectF();
    for (FaceDetector.Face face : faceArray) {
        if (face == null) {
            continue;
        }
        face.getMidPoint(pf);
        Log.e("tag", "FaceSDK : DetectionBitmap + " + face.confidence());
        // 这里的框,参数分别是:左上角的X,Y 右下角的X,Y
        // 也就是左上角(r.left,r.top),右下角( r.right,r.bottom)。
        // 该宽度是两眼珠黑色外边距
        float eyesDistance1 = face.eyesDistance();
        r.left = pf.x - eyesDistance1 / 1.2f;
        r.right = pf.x + eyesDistance1 / 1.2f;
        r.top = pf.y - eyesDistance1;
        r.bottom = pf.y + eyesDistance1 * 1.5f;
        Log.d(TAG, r.toString());
        // 画框
        canvas.drawRect(r, p);
    }
    return bitmap;
}
 
Example 10
Source File: FaceCropper.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public Bitmap cropFace(Bitmap original) {
    Bitmap fixedBitmap = BitmapUtils.forceEvenBitmapSize(original);
    fixedBitmap = BitmapUtils.forceConfig565(fixedBitmap);

    FaceDetector faceDetector = new FaceDetector(
            fixedBitmap.getWidth(), fixedBitmap.getHeight(),
            mMaxFaces);

    FaceDetector.Face[] faces = new FaceDetector.Face[mMaxFaces];

    // The bitmap must be in 565 format (for now).
    int faceCount = faceDetector.findFaces(fixedBitmap, faces);

    Logs.d("face crop");

    if (faceCount == 0) {
        return fixedBitmap;
    }

    int initX = fixedBitmap.getWidth();
    int initY = fixedBitmap.getHeight();
    int endX = 0;
    int endY = 0;

    PointF centerFace = new PointF();

    // Calculates minimum box to fit all detected faces
    for (int i = 0; i < faceCount; i++) {
        FaceDetector.Face face = faces[i];

        // Eyes distance * 3 usually fits an entire face
        int faceSize = (int) (face.eyesDistance() * 3);
        if (SizeMode.FaceMarginPx.equals(mSizeMode)) {
            faceSize += mFaceMarginPx * 2; // *2 for top and down/right and left effect
        } else if (SizeMode.EyeDistanceFactorMargin.equals(mSizeMode)) {
            faceSize += face.eyesDistance() * mEyeDistanceFactorMargin;
        }

        faceSize = Math.max(faceSize, mFaceMinSize);

        face.getMidPoint(centerFace);

        int tInitX = (int) (centerFace.x - faceSize / 2);
        int tInitY = (int) (centerFace.y - faceSize / 2);
        tInitX = Math.max(0, tInitX);
        tInitY = Math.max(0, tInitY);

        int tEndX = tInitX + faceSize;
        int tEndY = tInitY + faceSize;
        tEndX = Math.min(tEndX, fixedBitmap.getWidth());
        tEndY = Math.min(tEndY, fixedBitmap.getHeight());

        initX = Math.min(initX, tInitX);
        initY = Math.min(initY, tInitY);
        endX = Math.max(endX, tEndX);
        endY = Math.max(endY, tEndY);
    }

    int sizeX = endX - initX;
    int sizeY = endY - initY;

    if (sizeX + initX > fixedBitmap.getWidth()) {
        sizeX = fixedBitmap.getWidth() - initX;
    }
    if (sizeY + initY > fixedBitmap.getHeight()) {
        sizeY = fixedBitmap.getHeight() - initY;
    }

    Bitmap croppedBitmap = Bitmap.createBitmap(fixedBitmap, initX, initY, sizeX, sizeY);
    if (fixedBitmap != croppedBitmap) {
        fixedBitmap.recycle();
    }

    return croppedBitmap;
}
 
Example 11
Source File: FaceCropper.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public Bitmap cropFace(Bitmap original) {
    Bitmap fixedBitmap = BitmapUtils.forceEvenBitmapSize(original);
    fixedBitmap = BitmapUtils.forceConfig565(fixedBitmap);

    FaceDetector faceDetector = new FaceDetector(
            fixedBitmap.getWidth(), fixedBitmap.getHeight(),
            mMaxFaces);

    FaceDetector.Face[] faces = new FaceDetector.Face[mMaxFaces];

    // The bitmap must be in 565 format (for now).
    int faceCount = faceDetector.findFaces(fixedBitmap, faces);

    Logs.d("face crop");

    if (faceCount == 0) {
        return fixedBitmap;
    }

    int initX = fixedBitmap.getWidth();
    int initY = fixedBitmap.getHeight();
    int endX = 0;
    int endY = 0;

    PointF centerFace = new PointF();

    // Calculates minimum box to fit all detected faces
    for (int i = 0; i < faceCount; i++) {
        FaceDetector.Face face = faces[i];

        // Eyes distance * 3 usually fits an entire face
        int faceSize = (int) (face.eyesDistance() * 3);
        if (SizeMode.FaceMarginPx.equals(mSizeMode)) {
            faceSize += mFaceMarginPx * 2; // *2 for top and down/right and left effect
        } else if (SizeMode.EyeDistanceFactorMargin.equals(mSizeMode)) {
            faceSize += face.eyesDistance() * mEyeDistanceFactorMargin;
        }

        faceSize = Math.max(faceSize, mFaceMinSize);

        face.getMidPoint(centerFace);

        int tInitX = (int) (centerFace.x - faceSize / 2);
        int tInitY = (int) (centerFace.y - faceSize / 2);
        tInitX = Math.max(0, tInitX);
        tInitY = Math.max(0, tInitY);

        int tEndX = tInitX + faceSize;
        int tEndY = tInitY + faceSize;
        tEndX = Math.min(tEndX, fixedBitmap.getWidth());
        tEndY = Math.min(tEndY, fixedBitmap.getHeight());

        initX = Math.min(initX, tInitX);
        initY = Math.min(initY, tInitY);
        endX = Math.max(endX, tEndX);
        endY = Math.max(endY, tEndY);
    }

    int sizeX = endX - initX;
    int sizeY = endY - initY;

    if (sizeX + initX > fixedBitmap.getWidth()) {
        sizeX = fixedBitmap.getWidth() - initX;
    }
    if (sizeY + initY > fixedBitmap.getHeight()) {
        sizeY = fixedBitmap.getHeight() - initY;
    }

    Bitmap croppedBitmap = Bitmap.createBitmap(fixedBitmap, initX, initY, sizeX, sizeY);
    if (fixedBitmap != croppedBitmap) {
        fixedBitmap.recycle();
    }

    return croppedBitmap;
}
 
Example 12
Source File: CropImage.java    From Pi-Locker with GNU General Public License v2.0 4 votes vote down vote up
private void handleFace(FaceDetector.Face f) {

            PointF midPoint = new PointF();

            int r = ((int) (f.eyesDistance() * mScale)) * 2;
            f.getMidPoint(midPoint);
            midPoint.x *= mScale;
            midPoint.y *= mScale;

            int midX = (int) midPoint.x;
            int midY = (int) midPoint.y;

            HighlightView hv = new HighlightView(mImageView);

            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();

            Rect imageRect = new Rect(0, 0, width, height);

            RectF faceRect = new RectF(midX, midY, midX, midY);
            faceRect.inset(-r, -r);
            if (faceRect.left < 0) {
                faceRect.inset(-faceRect.left, -faceRect.left);
            }

            if (faceRect.top < 0) {
                faceRect.inset(-faceRect.top, -faceRect.top);
            }

            if (faceRect.right > imageRect.right) {
                faceRect.inset(faceRect.right - imageRect.right,
                        faceRect.right - imageRect.right);
            }

            if (faceRect.bottom > imageRect.bottom) {
                faceRect.inset(faceRect.bottom - imageRect.bottom,
                        faceRect.bottom - imageRect.bottom);
            }

            hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop,
                    mAspectX != 0 && mAspectY != 0);

            mImageView.add(hv);
        }
 
Example 13
Source File: CropImage.java    From reader with MIT License 4 votes vote down vote up
private void handleFace(FaceDetector.Face f) {
    PointF midPoint = new PointF();

    int r = ((int) (f.eyesDistance() * mScale)) * 2;
    f.getMidPoint(midPoint);
    midPoint.x *= mScale;
    midPoint.y *= mScale;

    int midX = (int) midPoint.x;
    int midY = (int) midPoint.y;

    HighlightView hv = new HighlightView(mImageView);

    int width = mBitmap.getWidth();
    int height = mBitmap.getHeight();

    Rect imageRect = new Rect(0, 0, width, height);

    RectF faceRect = new RectF(midX, midY, midX, midY);
    faceRect.inset(-r, -r);
    if (faceRect.left < 0) {
        faceRect.inset(-faceRect.left, -faceRect.left);
    }

    if (faceRect.top < 0) {
        faceRect.inset(-faceRect.top, -faceRect.top);
    }

    if (faceRect.right > imageRect.right) {
        faceRect.inset(faceRect.right - imageRect.right,
                       faceRect.right - imageRect.right);
    }

    if (faceRect.bottom > imageRect.bottom) {
        faceRect.inset(faceRect.bottom - imageRect.bottom,
                       faceRect.bottom - imageRect.bottom);
    }

    hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop,false
             /*mAspectX != 0 && mAspectY != 0*/);

    mImageView.add(hv);
}
 
Example 14
Source File: CropImage.java    From reader with MIT License 4 votes vote down vote up
private void handleFace(FaceDetector.Face f) {
    PointF midPoint = new PointF();

    int r = ((int) (f.eyesDistance() * mScale)) * 2;
    f.getMidPoint(midPoint);
    midPoint.x *= mScale;
    midPoint.y *= mScale;

    int midX = (int) midPoint.x;
    int midY = (int) midPoint.y;

    HighlightView hv = new HighlightView(mImageView);

    int width = mBitmap.getWidth();
    int height = mBitmap.getHeight();

    Rect imageRect = new Rect(0, 0, width, height);

    RectF faceRect = new RectF(midX, midY, midX, midY);
    faceRect.inset(-r, -r);
    if (faceRect.left < 0) {
        faceRect.inset(-faceRect.left, -faceRect.left);
    }

    if (faceRect.top < 0) {
        faceRect.inset(-faceRect.top, -faceRect.top);
    }

    if (faceRect.right > imageRect.right) {
        faceRect.inset(faceRect.right - imageRect.right,
                       faceRect.right - imageRect.right);
    }

    if (faceRect.bottom > imageRect.bottom) {
        faceRect.inset(faceRect.bottom - imageRect.bottom,
                       faceRect.bottom - imageRect.bottom);
    }

    hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop,false
             /*mAspectX != 0 && mAspectY != 0*/);

    mImageView.add(hv);
}
 
Example 15
Source File: FaceCropper.java    From AndroidFaceCropper with Apache License 2.0 4 votes vote down vote up
protected CropResult cropFace(Bitmap original, boolean debug) {
    Bitmap fixedBitmap = BitmapUtils.forceEvenBitmapSize(original);
    fixedBitmap = BitmapUtils.forceConfig565(fixedBitmap);
    Bitmap mutableBitmap = fixedBitmap.copy(Bitmap.Config.RGB_565, true);

    if (fixedBitmap != mutableBitmap) {
        fixedBitmap.recycle();
    }

    FaceDetector faceDetector = new FaceDetector(
            mutableBitmap.getWidth(), mutableBitmap.getHeight(),
            mMaxFaces);

    FaceDetector.Face[] faces = new FaceDetector.Face[mMaxFaces];

    // The bitmap must be in 565 format (for now).
    int faceCount = faceDetector.findFaces(mutableBitmap, faces);

    if (BuildConfig.DEBUG) {
        Log.d(LOG_TAG, faceCount + " faces found");
    }

    if (faceCount == 0) {
        return new CropResult(mutableBitmap);
    }

    int initX = mutableBitmap.getWidth();
    int initY = mutableBitmap.getHeight();
    int endX = 0;
    int endY = 0;

    PointF centerFace = new PointF();

    Canvas canvas = new Canvas(mutableBitmap);
    canvas.drawBitmap(mutableBitmap, new Matrix(), null);

    // Calculates minimum box to fit all detected faces
    for (int i = 0; i < faceCount; i++) {
        FaceDetector.Face face = faces[i];

        // Eyes distance * 3 usually fits an entire face
        int faceSize = (int) (face.eyesDistance() * 3);

        if (SizeMode.FaceMarginPx.equals(mSizeMode)) {
            faceSize += mFaceMarginPx * 2; // *2 for top and down/right and left effect
        }
        else if (SizeMode.EyeDistanceFactorMargin.equals(mSizeMode)) {
            faceSize += face.eyesDistance() * mEyeDistanceFactorMargin;
        }

        faceSize = Math.max(faceSize, mFaceMinSize);

        face.getMidPoint(centerFace);

        if (debug) {
            canvas.drawPoint(centerFace.x, centerFace.y, mDebugPainter);
            canvas.drawCircle(centerFace.x, centerFace.y, face.eyesDistance() * 1.5f, mDebugPainter);
        }

        int tInitX = (int) (centerFace.x - faceSize / 2);
        int tInitY = (int) (centerFace.y - faceSize / 2);
        tInitX = Math.max(0, tInitX);
        tInitY = Math.max(0, tInitY);

        int tEndX = tInitX + faceSize;
        int tEndY = tInitY + faceSize;
        tEndX = Math.min(tEndX, mutableBitmap.getWidth());
        tEndY = Math.min(tEndY, mutableBitmap.getHeight());

        initX = Math.min(initX, tInitX);
        initY = Math.min(initY, tInitY);
        endX = Math.max(endX, tEndX);
        endY = Math.max(endY, tEndY);
    }

    int sizeX = endX - initX;
    int sizeY = endY - initY;

    if (sizeX + initX > mutableBitmap.getWidth()) {
        sizeX = mutableBitmap.getWidth() - initX;
    }
    if (sizeY + initY > mutableBitmap.getHeight()) {
        sizeY = mutableBitmap.getHeight() - initY;
    }

    Point init = new Point(initX, initY);
    Point end = new Point(initX + sizeX, initY + sizeY);

    return new CropResult(mutableBitmap, init, end);
}
 
Example 16
Source File: CropImageActivity.java    From Android-RTEditor with Apache License 2.0 4 votes vote down vote up
private void handleFace(FaceDetector.Face f) {

            PointF midPoint = new PointF();

            int r = ((int) (f.eyesDistance() * mScale)) * 2;
            f.getMidPoint(midPoint);
            midPoint.x *= mScale;
            midPoint.y *= mScale;

            int midX = (int) midPoint.x;
            int midY = (int) midPoint.y;

            HighlightView hv = new HighlightView(mImageView);

            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();

            Rect imageRect = new Rect(0, 0, width, height);

            RectF faceRect = new RectF(midX, midY, midX, midY);
            faceRect.inset(-r, -r);
            if (faceRect.left < 0) {
                faceRect.inset(-faceRect.left, -faceRect.left);
            }

            if (faceRect.top < 0) {
                faceRect.inset(-faceRect.top, -faceRect.top);
            }

            if (faceRect.right > imageRect.right) {
                faceRect.inset(faceRect.right - imageRect.right, faceRect.right
                        - imageRect.right);
            }

            if (faceRect.bottom > imageRect.bottom) {
                faceRect.inset(faceRect.bottom - imageRect.bottom,
                        faceRect.bottom - imageRect.bottom);
            }

            hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop,
                    mAspectX != 0 && mAspectY != 0);

            mImageView.add(hv);
        }
 
Example 17
Source File: CropImageActivity.java    From memoir with Apache License 2.0 4 votes vote down vote up
private void handleFace(FaceDetector.Face f) {

            PointF midPoint = new PointF();

            int r = ((int) (f.eyesDistance() * mScale)) * 2;
            f.getMidPoint(midPoint);
            midPoint.x *= mScale;
            midPoint.y *= mScale;

            int midX = (int) midPoint.x;
            int midY = (int) midPoint.y;

            HighlightView hv = new HighlightView(mImageView);

            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();

            Rect imageRect = new Rect(0, 0, width, height);

            RectF faceRect = new RectF(midX, midY, midX, midY);
            faceRect.inset(-r, -r);
            if (faceRect.left < 0) {
                faceRect.inset(-faceRect.left, -faceRect.left);
            }

            if (faceRect.top < 0) {
                faceRect.inset(-faceRect.top, -faceRect.top);
            }

            if (faceRect.right > imageRect.right) {
                faceRect.inset(faceRect.right - imageRect.right, faceRect.right
                        - imageRect.right);
            }

            if (faceRect.bottom > imageRect.bottom) {
                faceRect.inset(faceRect.bottom - imageRect.bottom,
                        faceRect.bottom - imageRect.bottom);
            }

            hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop,
                    mAspectX != 0 && mAspectY != 0);

            mImageView.add(hv);
        }
 
Example 18
Source File: CropImageActivity.java    From memoir with Apache License 2.0 4 votes vote down vote up
private void handleFace(FaceDetector.Face f) {

            PointF midPoint = new PointF();

            int r = ((int) (f.eyesDistance() * mScale)) * 2;
            f.getMidPoint(midPoint);
            midPoint.x *= mScale;
            midPoint.y *= mScale;

            int midX = (int) midPoint.x;
            int midY = (int) midPoint.y;

            HighlightView hv = new HighlightView(mImageView);

            int width = mBitmap.getWidth();
            int height = mBitmap.getHeight();

            Rect imageRect = new Rect(0, 0, width, height);

            RectF faceRect = new RectF(midX, midY, midX, midY);
            faceRect.inset(-r, -r);
            if (faceRect.left < 0) {
                faceRect.inset(-faceRect.left, -faceRect.left);
            }

            if (faceRect.top < 0) {
                faceRect.inset(-faceRect.top, -faceRect.top);
            }

            if (faceRect.right > imageRect.right) {
                faceRect.inset(faceRect.right - imageRect.right, faceRect.right
                        - imageRect.right);
            }

            if (faceRect.bottom > imageRect.bottom) {
                faceRect.inset(faceRect.bottom - imageRect.bottom,
                        faceRect.bottom - imageRect.bottom);
            }

            hv.setup(mImageMatrix, imageRect, faceRect, mCircleCrop,
                    mAspectX != 0 && mAspectY != 0);

            mImageView.add(hv);
        }
 
Example 19
Source File: SystemFaceDetector.java    From ViseFace with Apache License 2.0 4 votes vote down vote up
/**
 * 计算识别框
 */
private void getFaceRect() {
    Rect[] faceRectList = new Rect[mDetectorData.getFacesCount()];
    Rect rect = null;
    int index = 0;
    float distance = 0;
    for (int i = 0; i < mDetectorData.getFacesCount(); i++) {
        faceRectList[i] = new Rect();
        FaceDetector.Face face = mFaces[i];
        if (face != null) {
            float eyeDistance = face.eyesDistance();
            eyeDistance = eyeDistance * mZoomRatio;
            if (eyeDistance > distance) {
                distance = eyeDistance;
                rect = faceRectList[i];
                index = i;
            }
            PointF midEyesPoint = new PointF();
            face.getMidPoint(midEyesPoint);
            midEyesPoint.x = midEyesPoint.x * mZoomRatio;
            midEyesPoint.y = midEyesPoint.y * mZoomRatio;
            ViseLog.i("eyeDistance:" + eyeDistance + ",midEyesPoint.x:" + midEyesPoint.x
                    + ",midEyesPoint.y:" + midEyesPoint.y);
            faceRectList[i].set((int) (midEyesPoint.x - eyeDistance),
                    (int) (midEyesPoint.y - eyeDistance),
                    (int) (midEyesPoint.x + eyeDistance),
                    (int) (midEyesPoint.y + eyeDistance));
            ViseLog.i("FaceRectList[" + i + "]:" + faceRectList[i]);
        }
    }
    int width = (int) (mPreviewHeight * mZoomRatio / 5);
    if (rect != null && mCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        int left = rect.left;
        rect.left = width - rect.right;
        rect.right = width - left;
        faceRectList[index].left = rect.left;
        faceRectList[index].right = rect.right;
    }
    mDetectorData.setLightIntensity(FaceUtil.getYUVLight(mDetectorData.getFaceData(), rect, width));
    mDetectorData.setFaceRectList(faceRectList);
    if (mCameraWidth > 0) {
        mDetectorData.setDistance(distance * 2.5f / mCameraWidth);
    }
}
 
Example 20
Source File: FaceRecognitionPlugin.java    From media-for-mobile with Apache License 2.0 2 votes vote down vote up
@Override
public RecognitionOutput recognize(RecognitionInput input) {
    FaceRecognitionInput recognitioInput = null;

    if (input instanceof FaceRecognitionInput) {
        recognitioInput = (FaceRecognitionInput) input;
    } else {
        throw new IllegalArgumentException("Invalid arguments.");
    }

    Bitmap bitmap = recognitioInput.getBitmap();

    if (lastBitmapWidth != bitmap.getWidth() || lastBitmapHeight != bitmap.getHeight()) {
        lastBitmapWidth = bitmap.getWidth();
        lastBitmapHeight = bitmap.getHeight();

        faceDetector = new FaceDetector(lastBitmapWidth, lastBitmapHeight, MAX_FACES);
    }

    FaceRecognitionOutput result = new FaceRecognitionOutput();

    FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES];

    int facesDetected = faceDetector.findFaces(bitmap, faces);

    PointF midPoint = new PointF();

    for (int i = 0; i < facesDetected; i++) {
        Face face = new Face();

        face.mId = 0;

        face.mConfidence = faces[i].confidence();
        face.mEyeDistance = faces[i].eyesDistance();

        faces[i].getMidPoint(midPoint);

        face.mMidPointX = midPoint.x;
        face.mMidPointY = midPoint.y;

        result.add(face);
    }

    return result;
}