Skip to content

Commit

Permalink
source code for java openCV CloudFaceCV
Browse files Browse the repository at this point in the history
  • Loading branch information
pindarvanarman committed Apr 16, 2017
1 parent ed3568a commit a8ff1aa
Show file tree
Hide file tree
Showing 91 changed files with 57,826 additions and 0 deletions.
Binary file added CloudFaceCV/lines/picasso_50_5_90_10_10_4_.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/10.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/11.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/12.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/6.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/7.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/8.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/9.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4,394 changes: 4,394 additions & 0 deletions CloudFaceCV/mary/FootballDataSet.csv

Large diffs are not rendered by default.

4,581 changes: 4,581 additions & 0 deletions CloudFaceCV/mary/football_nocommas.csv

Large diffs are not rendered by default.

Binary file added CloudFaceCV/mary/marydream.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/marypassort400.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/marysquare.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/marysquare400.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/1.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/10.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/11.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/12.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/2.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/3.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/4.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/5.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CloudFaceCV/mary/r/6.jpg
Binary file added CloudFaceCV/mary/r/7.jpg
Binary file added CloudFaceCV/mary/r/8.jpg
Binary file added CloudFaceCV/mary/r/9.jpg
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_1 (2).png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_1.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_10.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_11.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_12.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_2.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_4.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_5.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_6.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_7.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_8.png
Binary file added CloudFaceCV/mary/r/ratingsfaceDetection_9.png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_1 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_10 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_11 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_12 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_2 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_4 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_5 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_7.png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_8 (2).png
Binary file added CloudFaceCV/mary/ratingsfaceDetection_9 (2).png
Binary file added CloudFaceCV/pindar/001.jpg
Binary file added CloudFaceCV/pindar/002.jpg
Binary file added CloudFaceCV/pindar/003.jpg
Binary file added CloudFaceCV/pindar/cropfaceDetection001.png
Binary file added CloudFaceCV/pindar/cropfaceDetection002.png
Binary file added CloudFaceCV/pindar/cropfaceDetection003.png
Binary file added CloudFaceCV/pindar/faceDetection001.png
Binary file added CloudFaceCV/pindar/faceDetection002.png
Binary file added CloudFaceCV/pindar/faceDetection003.png
Binary file added CloudFaceCV/pindar/pindarProcessed/001.jpg
Binary file added CloudFaceCV/pindar/pindarProcessed/002.jpg
Binary file added CloudFaceCV/pindar/pindarProcessed/003.jpg
Binary file added CloudFaceCV/pindar/pindarProcessed/pindar.png
12,213 changes: 12,213 additions & 0 deletions CloudFaceCV/resources/haarcascade_eye.xml

Large diffs are not rendered by default.

21,991 changes: 21,991 additions & 0 deletions CloudFaceCV/resources/haarcascade_mcs_mouth.xml

Large diffs are not rendered by default.

1,505 changes: 1,505 additions & 0 deletions CloudFaceCV/resources/lbpcascade_frontalface.xml

Large diffs are not rendered by default.

1,275 changes: 1,275 additions & 0 deletions CloudFaceCV/resources/lbpcascade_profileface.xml

Large diffs are not rendered by default.

Binary file added CloudFaceCV/resources/lena1.png
Binary file added CloudFaceCV/resources/libopencv_java248.dylib
Binary file not shown.
Binary file added CloudFaceCV/resources/pindar.png
2,063 changes: 2,063 additions & 0 deletions CloudFaceCV/src/Base64.java

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions CloudFaceCV/src/CloudFaceCV.java
@@ -0,0 +1,30 @@
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Scalar;

class CloudFaceCV {

//static{ System.loadLibrary("lib"+Core.NATIVE_LIBRARY_NAME); }

public static void main(String[] args) {
System.out.println(System.getProperty("java.library.path"));
System.out.println("Welcome to OpenCV " + Core.VERSION);
System.out.println("Lib Library " + Core.NATIVE_LIBRARY_NAME);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
System.out.println("OpenCV Mat: " + m);
Mat mr1 = m.row(1);
mr1.setTo(new Scalar(1));
Mat mc5 = m.col(5);
mc5.setTo(new Scalar(5));
System.out.println("OpenCV Mat data:\n" + m.dump());



new CloudFaceDetect().run();
//new CloudFaceLines().run();

}

}
209 changes: 209 additions & 0 deletions CloudFaceCV/src/CloudFaceDetect.java
@@ -0,0 +1,209 @@
import java.net.URL;
import java.util.Vector;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
class CloudFaceDetect {
public void run() {
System.out.println("\nRunning DetectFaceDemo");

// Create a face detector from the cascade file in the resources
// directory.
for (int num=1;num<13; num++) {
//URL u = new URL("C:/development/workspace/CloudFaceCV/resources/lbpcascade_frontalface.xml");
try {
URL url2 = getClass().getResource("C:/development/workspace/CloudFaceCV/resources/lbpcascade_frontalface.xml");
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("resources/lbpcascade_frontalface.xml").getPath());
CascadeClassifier faceDetector = new CascadeClassifier("C:/development/workspace/CloudFaceCV/resources/lbpcascade_frontalface.xml");
CascadeClassifier eyeDetector = new CascadeClassifier("C:/development/workspace/CloudFaceCV/resources/haarcascade_eye.xml");
CascadeClassifier mouthDetector = new CascadeClassifier("C:/development/workspace/CloudFaceCV/resources/haarcascade_mcs_mouth.xml");

//Mat image = Highgui.imread(getClass().getResource("/lena.png").getPath());
Mat image = Highgui.imread("C:/development/workspace/CloudFaceCV/"+num+".jpg");
MatOfInt rejectLevels = new MatOfInt();
MatOfDouble weightLevels = new MatOfDouble();
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections, rejectLevels, weightLevels);

MatOfRect eyeDetections = new MatOfRect();
eyeDetector.detectMultiScale(image, eyeDetections, rejectLevels, weightLevels);

MatOfRect mouthDetections = new MatOfRect();
mouthDetector.detectMultiScale(image, mouthDetections, rejectLevels, weightLevels);

System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

// Draw a bounding box around each face.

Rect faceRect = new Rect();
for (Rect rect : faceDetections.toArray()) {
if(faceRect==null || rect.width>faceRect.width) {
faceRect = rect;
}
}
Core.rectangle(image, new Point(faceRect.x, faceRect.y), new Point(faceRect.x + faceRect.width, faceRect.y + faceRect.height), new Scalar(0, 255, 0), 10);

Vector eyeRects = new Vector();
int eyeDifference = 0;
for (Rect rect : eyeDetections.toArray()) {
if (rect.width>faceRect.width/10 &&
rect.x > faceRect.x &&
rect.y > faceRect.y &&
rect.x < faceRect.x+faceRect.width &&
rect.y < faceRect.y+faceRect.height
) {
eyeRects.add(rect);
}
}

//Hunters First Algorithm - programmed by Pindar
//if more than 2 eyes
//erase smallest eye
int smallestEyeIndex = 0;
Rect smallestEyeFound = new Rect(0,0,10000,10000);
if (eyeRects.size()>2) {
for (int i=0;i<eyeRects.size();i++) {
if ( ((Rect)eyeRects.elementAt(i)).width < smallestEyeFound.width) {
smallestEyeFound = (Rect)eyeRects.elementAt(i);
smallestEyeIndex = i;
}
}
eyeRects.remove(smallestEyeIndex);
}


for (int i=0;i<eyeRects.size();i++) {
Rect rect = (Rect)eyeRects.elementAt(i);
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0 , 0), 10);
}


int averageEyeY = 0;
int averageEyeHeight = 0;
for (int i=0; i<eyeRects.size(); i++){
averageEyeY += (((Rect)eyeRects.get(i)).y+(((Rect)eyeRects.get(i)).height/2));
averageEyeHeight += (((Rect)eyeRects.get(i)).height);
}
if (eyeRects.size()>=1) {
averageEyeY = averageEyeY/eyeRects.size();
averageEyeHeight = averageEyeHeight/eyeRects.size();
}
Rect mouthRect = new Rect();
for (Rect rect : mouthDetections.toArray()) {
if (rect.width<faceRect.width/2 &&
rect.width>faceRect.width/5 &&
rect.y > averageEyeY+averageEyeHeight &&
rect.x > faceRect.x &&
rect.y > faceRect.y &&
rect.x < faceRect.x+faceRect.width &&
rect.y < faceRect.y+faceRect.height
) {
if (mouthRect.width == 0) {
mouthRect = rect;
} else {
if (rect.y < mouthRect.y) {
mouthRect = rect;
}
}

}
}
Core.rectangle(image, new Point(mouthRect.x, mouthRect.y), new Point(mouthRect.x + mouthRect.width, mouthRect.y + mouthRect.height), new Scalar(255, 0 , 0), 10);

int sym = CalculateFaceSymmetry(faceRect, (Rect)eyeRects.elementAt(0), (Rect)eyeRects.elementAt(1), mouthRect);
// Save the visualized detection.

int facing = CalculateDirectionOffFace(faceRect, (Rect)eyeRects.elementAt(0), (Rect)eyeRects.elementAt(1), mouthRect);
// Save the visualized detection.
String facingDir = "";
if (facing<-20) {
facingDir = "right";
} else if (facing>20) {
facingDir = "left";
} else {
facingDir = "forward";
}
Rect crop = getCropping(faceRect, facing);

Core.rectangle(image, new Point(crop.x, crop.y), new Point(crop.x + crop.width, crop.y + crop.height), new Scalar(0, 0 , 255), 10);
Core.putText(image, "rating: "+sym+"" , new Point(50, 100), 2, 3, new Scalar(0, 0 , 0), 5);//(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0 , 255), 10);
Core.putText(image, "looking "+facingDir+": "+facing+"" , new Point(50, 200), 2, 3, new Scalar(0, 0 , 0), 5);//(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0 , 255), 10);




String filename = "ratingsfaceDetection_"+num+".png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);


} catch (Exception e) {
}
}
}

public int CalculateFaceSymmetry(Rect face, Rect eyeOne, Rect eyeTwo, Rect mouth) {
//calculate difference of eye levels
//calculate center of eyes vs middle of face horizontally
//calculate mouth to center of eyes
double differenceOfEyeLevel = Math.abs(eyeOne.y-eyeTwo.y)*2;
double distanceOfEyesFromCenterOfFace = Math.abs( (face.x+face.width/2) - (((eyeOne.x+(eyeOne.width/2)) + (eyeTwo.x+(eyeTwo.width/2)))/2) );
double distanceOfMouthFromCenterOfFace = Math.abs( (face.x+face.width/2) - (mouth.x+(mouth.width/2)) );
double distanceOfEyesFromCenterOfMouth = Math.abs( (mouth.x+(mouth.width/2)) - (((eyeOne.x+(eyeOne.width/2)) + (eyeTwo.x+(eyeTwo.width/2)))/2) );
double totalError = differenceOfEyeLevel + distanceOfEyesFromCenterOfFace + distanceOfMouthFromCenterOfFace + distanceOfEyesFromCenterOfMouth;
double symmetryScore = 40/totalError;
int percentage = (int)(symmetryScore*100);
return percentage;
}

public int CalculateDirectionOffFace(Rect face, Rect eyeOne, Rect eyeTwo, Rect mouth) {
int faceCenter = (face.x+(face.width/2));
int mouthCenter = (mouth.x+(mouth.width/2));
int eyeCenter = ( ((eyeOne.x+(eyeOne.width/2)) + (eyeTwo.x+(eyeTwo.width/2))) /2);

int distanceOfEyesFromCenter = (( mouthCenter - eyeCenter ));// + (faceCenter - eyeCenter);
return distanceOfEyesFromCenter;
}

public Rect getCropping(Rect faceRect, int facing) {
Rect newCrop = new Rect();
//Rect aspectRatio = new Rect(0,0,880,1144);
Rect aspectRatio = new Rect(0,0,1144,1144);
int widthDiff = aspectRatio.width - faceRect.width;
int widthShift = 0;
if (facing<-20) {
//widthShift = -(widthDiff/4)*3;
widthShift = (widthDiff/4);
} else if (facing>20) {
//widthShift = (widthDiff/4)*3;
widthShift = -(widthDiff/4);
} else {
widthShift = 0;
}
int val = (faceRect.x-(widthDiff/2))+widthShift;
aspectRatio.x = val;



int heightDiff = aspectRatio.height - faceRect.height;
aspectRatio.y = faceRect.y-(heightDiff/3);
return aspectRatio;
}


}
98 changes: 98 additions & 0 deletions CloudFaceCV/src/CloudFaceLines.java
@@ -0,0 +1,98 @@
import java.io.File;
import java.net.URL;
import java.util.Vector;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//

class CloudFaceLines {

public static void main(String[] args) {
CloudFaceLines cfl = new CloudFaceLines();
cfl.run();
}


public void run() {
System.out.println("\nRunning Line Detector");

//libopencv_java248.dylib

//System.load(new File("C:/development/workspace/CloudFaceCV/resources/libopencv_java248.dylib").getAbsolutePath());

//linesoutbw_50_5_90_10_10_4_

//mary
//marydream_75_5_90_10_10_2_
for (int lt = 75; lt<=75; lt=lt+25 ) {
for (int r = 5; r<=5; r+=2 ) {
for (int e1 = 90; e1<=90; e1+=90 ) {
for (int e2 = 5; e2<=30; e2+=10 ) {
for (int e3 = 5; e3<=30; e3+=10 ) {
for (int e4 = 2; e4<=2; e4+=2 ) {

//Mat img = Highgui.imread("data/topdown-6.jpg");
Mat img = Highgui.imread("C:/development/workspace/CloudFaceCV/1.jpg");


// load the image


// generate gray scale and blur
Mat gray = new Mat();
Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.blur(gray, gray, new Size(3, 3));

// detect the edges
Mat edges = new Mat();
int lowThreshold = lt;
int ratio = r;
Imgproc.Canny(gray, edges, lowThreshold, lowThreshold * ratio);

Mat lines = new Mat();
Imgproc.HoughLinesP(edges, lines, 1, Math.PI / e1, e2, e3, e4);



for(int i = 0; i < lines.cols(); i++) {
double[] val = lines.get(0, i);
Core.line(img, new Point(val[0], val[1]), new Point(val[2], val[3]), new Scalar(0, 0, 255), 2);

//SendStrokesToElasticSearchCluster(String indexName, int strokeNumber, double startX, double startY, double endX, double endY, int width, int height, String painting)
}

Highgui.imwrite("C:/development/workspace/CloudFaceCV/corinne11_1200_"+lt+"_"+r+"_"+e1+"_"+e2+"_"+e3+"_"+e4+"_.jpg", img);
//Mat image = Highgui.imread();
}
}
}
}
}
}

//ImgWindow.newWindow(edges);
//ImgWindow.newWindow(gray);
//ImgWindow.newWindow(img);


}



}

0 comments on commit a8ff1aa

Please sign in to comment.