Created
July 28, 2013 08:26
-
-
Save BinRoot/6097939 to your computer and use it in GitHub Desktop.
this method will return true is eyes are found, else false if blinked
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public boolean eyesFound(Bitmap b) { | |
| int width = b.getWidth(); | |
| int height = b.getHeight(); | |
| Log.d(TAG, "finding eyes... "+b.getConfig()+", "+width+"x"+height); | |
| FaceDetector faceDetector = new FaceDetector(width, height, 1); | |
| FaceDetector.Face[] faces = new FaceDetector.Face[1]; | |
| faceDetector.findFaces(b, faces); | |
| FaceDetector.Face f = faces[0]; | |
| if(f == null) { | |
| Log.d(TAG, "no face found"); | |
| return false; | |
| } | |
| PointF midPoint = new PointF(); | |
| f.getMidPoint(midPoint); | |
| Float dist = f.eyesDistance(); | |
| int leftX = (int) (midPoint.x - dist/2); | |
| int leftY = (int) midPoint.y; | |
| int rightX = (int) (midPoint.x + dist/2); | |
| int rightY = (int) midPoint.y; | |
| int eyeWidth = (int) (dist/2); | |
| int eyeHeight = (int) (dist/3); | |
| int startEyeX = leftX - eyeWidth/2; | |
| int startEyeY = leftY - eyeHeight/2; | |
| int [] pixels = new int[eyeWidth*eyeHeight]; | |
| b.getPixels(pixels, 0, eyeWidth, startEyeX, startEyeY, eyeWidth, eyeHeight); | |
| int leftEyeColCount = 0; | |
| for (int i = 0; i < pixels.length; i+=eyeWidth) { | |
| pixels[i] ^= RGB_MASK; | |
| int pix = pixels[i]; | |
| int avg = (int) (0.33333* Color.red(pix) + 0.33333*Color.green(pix) + 0.33333*Color.blue(pix)); | |
| pixels[i] = Color.argb(Color.alpha(pix), avg, avg, avg); | |
| int sumColors = Color.red(pixels[i]) + Color.green(pixels[i]) + Color.blue(pixels[i]); | |
| int threshold = 255; | |
| if (sumColors < 150*3) { | |
| threshold = 0; | |
| leftEyeColCount++; | |
| } | |
| pixels[i] = Color.argb(255, threshold, threshold, threshold); | |
| } | |
| b.setPixels(pixels, 0, eyeWidth, startEyeX, startEyeY, eyeWidth, eyeHeight); | |
| // right eye | |
| startEyeX = rightX - eyeWidth/2; | |
| startEyeY = rightY - eyeHeight/2; | |
| ; | |
| b.getPixels(pixels, 0, eyeWidth, startEyeX, startEyeY, eyeWidth, eyeHeight); | |
| int rightEyeColCount = 0; | |
| for (int i = eyeWidth-1; i < pixels.length; i+=eyeWidth) { | |
| pixels[i] ^= RGB_MASK; | |
| int pix = pixels[i]; | |
| int avg = (int) (0.2125* Color.red(pix) + 0.7154*Color.green(pix) + 0.0721*Color.blue(pix)); | |
| pixels[i] = Color.argb(Color.alpha(pix), avg, avg, avg); | |
| int sumColors = Color.red(pixels[i]) + Color.green(pixels[i]) + Color.blue(pixels[i]); | |
| int threshold = 255; | |
| if (sumColors < 150*3) { | |
| threshold = 0; | |
| rightEyeColCount++; | |
| } | |
| pixels[i] = Color.argb(255, threshold, threshold, threshold); | |
| } | |
| b.setPixels(pixels, 0, eyeWidth, startEyeX, startEyeY, eyeWidth, eyeHeight); | |
| // -- | |
| imageView.setImageBitmap(b); | |
| Log.d(TAG, "leftEyeColCount: "+leftEyeColCount+", rightEyeColCount: "+rightEyeColCount+", eyeHeight: "+eyeHeight); | |
| if (eyeHeight-leftEyeColCount > eyeHeight*0.4) { | |
| // left eye is open | |
| return true; | |
| } | |
| if (eyeHeight-rightEyeColCount > eyeHeight*0.4) { | |
| // right eye is open | |
| return true; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment