Repository navigation
Expand file tree
/
Copy pathCoordinate.java
More file actions
281 lines (245 loc) · 11.9 KB
/
Copy pathCoordinate.java
File metadata and controls
281 lines (245 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package coordinate;
import java.awt.*;
import java.awt.geom.CubicCurve2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
/**
*
* @author seema
*/
public class Coordinate extends JFrame {
static String outLoc = "F:\\emotion final yr\\final _output\\";
static String inLoc = "F:\\InputImages\\dlib_final_outputs\\";
static String filename = "";
static String extension = ".tiff";
static String[] emotions={"neutral","happy","sad","surprise","disgust"};
static String excel_file="";
/**
* @param args the command line arguments
*/
static int height=0;
static int width=0;
public static int[] getControlPoints(int[] coord)
{
int[] ctrl=new int[12];
ctrl[0]=(int)(Math.round((double)(8/9.0)*(double)(3*coord[0]-coord[2])-(double)(1/9.0)*(double)(10*coord[4]-3*coord[6])));
ctrl[1]=(int)(Math.round((double)(8/9.0)*(double)(3*coord[1]-coord[3])-(double)(1/9.0)*(double)(10*coord[5]-3*coord[7])));
ctrl[2]=(int)Math.round((double)(8/9.0)*(double)(3*coord[2]-coord[0])+(double)(1/9.0)*(double)(3*coord[4]-10*coord[6]));
ctrl[3]=(int)Math.round((double)(8/9.0)*(double)(3*coord[3]-coord[1])+(double)(1/9.0)*(double)(3*coord[5]-10*coord[7]));
ctrl[4]=(int)Math.round((double)(8/9.0)*(double)(3*coord[8]-coord[10])-(double)(1/9.0)*(double)(10*coord[12]-3*coord[14]));
ctrl[5]=(int)Math.round((double)(8/9.0)*(double)(3*coord[9]-coord[11])-(double)(1/9.0)*(double)(10*coord[13]-3*coord[15]));
ctrl[6]=(int)Math.round((double)(8/9.0)*(double)(3*coord[10]-coord[8])+(double)(1/9.0)*(double)(3*coord[12]-10*coord[14]));
ctrl[7]=(int)Math.round((double)(8/9.0)*(double)(3*coord[11]-coord[9])+(double)(1/9.0)*(double)(3*coord[13]-10*coord[15]));
ctrl[8]=(int)Math.round((double)(8/9.0)*(double)(3*coord[18]-coord[20])-(double)(1/9.0)*(double)(10*coord[16]-3*coord[22]));
ctrl[9]=(int)Math.round((double)(8/9.0)*(double)(3*coord[19]-coord[21])-(double)(1/9.0)*(double)(10*coord[17]-3*coord[23]));
ctrl[10]=(int)Math.round((double)(8/9.0)*(double)(3*coord[20]-coord[18])+(double)(1/9.0)*(double)(3*coord[16]-10*coord[22]));
ctrl[11]=(int)Math.round((double)(8/9.0)*(double)(3*coord[21]-coord[19])+(double)(1/9.0)*(double)(3*coord[17]-10*coord[23]));
return ctrl;
}
public static int[] getCoordinates(String path) throws IOException
{
int k=0;
int[] data1=new int[8];
int[] data2=new int[8];
int[] data3=new int[8];
int[] result=new int[data1.length+data2.length+data3.length];
Mat img = Imgcodecs.imread(path);
//System.out.println(img.dump());
int i,j;
int count=0;
//System.out.println("height"+img.height());
//System.out.println("height"+img.width());
for (i = 0; i <= img.height()/2; i++)
{
for (j = 0; j <= img.width()/2; j++)
{
double[] data = img.get(i, j);
//System.out.println(data[0]+" "+data[1]+" "+data[2]);
if (data[0] ==255&& data[1] == 0 && data[2] == 0)
{
//System.out.println("i= " + i + " j= " + j);
data1[k++]=j;
data1[k++]=i;
//System.out.println("hey");
count++;
}
if(count==4)
break;
}
if(count==4)
break;
}
count =0;
k=0;
for(i=0;i<=img.height()/2;i++)
{
for(j=img.width()/2;j<img.width();j++)
{
double[] data = img.get(i, j);
//System.out.println(data[0]+" "+data[1]+" "+data[2]);
if (data[0] ==255 && data[1] == 0 && data[2] == 0)
{
//System.out.println("i= " + i + " j= " + j);
data2[k++]=j;
data2[k++]=i;
//System.out.println("hey");
count++;
}
if(count==4)
break;
}
if(count==4)
break;
}
//mouth
count =0;
k=0;
for(j=0;j<img.width();j++)
{
for(i=img.height()/2;i<img.height();i++)
{
double[] data=img.get(i, j);
if (data[0] ==255 && data[1] == 0 && data[2] == 0)
{
//System.out.println("i= " + i + " j= " + j);
data3[k++]=j;
data3[k++]=i;
data[0]=0;
data[1]=255;
data[2]=0;
//img.put(j, count, data)
System.out.println("hey"+j+" "+i);
count++;
break;
}
}
if(count==4)
break;
}
System.arraycopy(data1, 0, result, 0,data1.length);
System.arraycopy(data2, 0, result, data1.length, data2.length);
System.arraycopy(data3,0,result,data1.length+data2.length,data3.length);
return result;
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
CascadeClassifier faceDetector = new CascadeClassifier
(Coordinate.class.getResource("face_detection.xml").getPath().substring(1).replace("%20", " "));
for(int i=0;i<emotions.length;i++)
{
File folder = new File(inLoc +emotions[i]);
File[] listOfFiles = folder.listFiles();
for(int j=0;j<listOfFiles.length;j++)
{
filename=listOfFiles[j].getName();
int pos=filename.lastIndexOf(".");
if(pos>0)
{
filename=filename.substring(0,pos);
}
System.out.println(filename);
Mat src = null, image = Imgcodecs.imread(inLoc+emotions[i]+"\\"+filename+extension);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s face", faceDetections.toArray().length));
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(255, 255, 255));
src=new Mat(image,rect);
Imgcodecs.imwrite(outLoc+emotions[i]+"\\"+filename+"_cropped"+".png", src);
}
height=src.height();
width=src.width();
System.out.println(String.format("Writing %s", filename));
Imgcodecs.imwrite(outLoc+emotions[i]+"\\"+filename+"_DetectedFace"+".tiff", image);
int[] coordinates=getCoordinates(outLoc+emotions[i]+"\\"+filename+"_cropped"+".png");
int k;
for(k=0;k<24;k++)
System.out.print("coordi"+coordinates[k]+" ");
System.out.println();
int[] ctrlpts=getControlPoints(coordinates);
BufferedImage img=ImageIO.read(new File(outLoc+emotions[i]+"\\"+filename+"_cropped"+".png"));
Color color = Color.GREEN;
Graphics2D g=img.createGraphics();
CubicCurve2D.Double cubicCurve,cubicCurve2;
cubicCurve = new CubicCurve2D.Double( coordinates[4],coordinates[5], ctrlpts[0], ctrlpts[1], ctrlpts[2], ctrlpts[3], coordinates[6], coordinates[7]);
cubicCurve2=new CubicCurve2D.Double(coordinates[16],coordinates[17],ctrlpts[8],ctrlpts[9],ctrlpts[10],ctrlpts[11],coordinates[22],coordinates[23]);
g.setColor(color);
g.draw(cubicCurve2);
g.draw(cubicCurve);
System.out.println("control points"+ctrlpts[0]+" "+ctrlpts[1]+" "+ctrlpts[2]+" "+ctrlpts[3]);
ImageIcon ii = new ImageIcon(img);
if (ImageIO.write(img, "png", new File(outLoc+emotions[i]+"\\"+filename+"_bezier"+".png")))
{
System.out.println("-- saved");
}
int[] final_cord=new int[24];
final_cord[0]=coordinates[4];
final_cord[1]=coordinates[5];
final_cord[2]=ctrlpts[0];
final_cord[3]=ctrlpts[1];
final_cord[4]=ctrlpts[2];
final_cord[5]=ctrlpts[3];
final_cord[6]=coordinates[6];
final_cord[7]=coordinates[7];
final_cord[8]=coordinates[12];
final_cord[9]=coordinates[13];
final_cord[10]=ctrlpts[4];
final_cord[11]=ctrlpts[5];
final_cord[12]=ctrlpts[6];
final_cord[13]=ctrlpts[7];
final_cord[14]=coordinates[14];
final_cord[15]=coordinates[15];
final_cord[16]=coordinates[16];
final_cord[17]=coordinates[17];
final_cord[18]=ctrlpts[8];
final_cord[19]=ctrlpts[9];
final_cord[20]=ctrlpts[10];
final_cord[21]=ctrlpts[11];
final_cord[22]=coordinates[22];
final_cord[23]=coordinates[23];
try(FileWriter fw = new FileWriter("F:\\\\emotion final yr\\\\test.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw))
{
for(int h=0;h<24;h++)
{
out.print(final_cord[h]+",");
}
out.println(emotions[i]);
}
catch (IOException e)
{
//exception handling left as an exercise for the reader
}
}
}
/*
*/
}
}