-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFaceOff.pde
More file actions
422 lines (345 loc) · 9.86 KB
/
FaceOff.pde
File metadata and controls
422 lines (345 loc) · 9.86 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import processing.javafx.*;
import ch.bildspur.vision.*;
import ch.bildspur.vision.result.*;
import processing.video.Capture;
Capture cam;
PImage yeniYuz;
ArrayList<Nokta> yeniNoktalar = new ArrayList<Nokta>();
ArrayList<Ucgen> ucgenler = new ArrayList<Ucgen>();
int kaymaMiktari = 640;
class Ucgen {
public int i1;
public int i2;
public int i3;
public Ucgen(int i1, int i2, int i3) {
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
}
}
class Nokta {
public int x;
public int y;
public Nokta(int x, int y) {
this.x = x;
this.y = y;
}
}
DeepVision vision = new DeepVision(this);
CascadeClassifierNetwork faceNetwork;
FacemarkLBFNetwork facemark;
ResultList<ObjectDetectionResult> detections;
ResultList<FacialLandmarkResult> markedFaces;
public void setup() {
points = new ArrayList<PVector>();
yeniYuz = loadImage("doom.jpg");
textureMode(IMAGE);
size(1000, 480, P3D);
colorMode(HSB, 360, 100, 100);
println("creating network...");
faceNetwork = vision.createCascadeFrontalFace();
facemark = vision.createFacemarkLBF();
println("loading model...");
faceNetwork.setup();
facemark.setup();
println("setup camera...");
String[] cams = Capture.list();
cam = new Capture(this, cams[0]);
cam.start();
detections = faceNetwork.run(yeniYuz);
markedFaces = facemark.runByDetections(yeniYuz, detections);
for (int i = 0; i < detections.size(); i++) {
FacialLandmarkResult landmarks = markedFaces.get(i);
if (landmarks.getKeyPoints().size() > 0) {
for (int j = 0; j < landmarks.getKeyPoints().size(); j++) {
KeyPointResult kp = landmarks.getKeyPoints().get(j);
yeniNoktalar.add(new Nokta(kp.getX(), kp.getY()));
points.add(new PVector(kp.getX(), kp.getY()));
}
}
}
UcgenleriTespitEt();
}
FacialLandmarkResult landmarks;
public void draw() {
background(55);
if (cam.available()) {
cam.read();
cam.loadPixels();
}
image(cam, 0, 0);
image(yeniYuz, kaymaMiktari, 0);
detections = faceNetwork.run(cam);
markedFaces = facemark.runByDetections(cam, detections);
for (int i = 0; i < detections.size(); i++) {
landmarks = markedFaces.get(i);
noFill();
strokeWeight(2f);
stroke(200, 80, 100);
noStroke();
fill(100, 80, 200);
for (int j = 0; j < landmarks.getKeyPoints().size(); j++) {
KeyPointResult kp = landmarks.getKeyPoints().get(j);
ellipse(kp.getX(), kp.getY(), 5, 5);
}
if (landmarks.getKeyPoints().size() > 0) {
/*ucluEkle(0, 1, 36);
ucluEkle(45, 16, 26);
dortluEkle(36, 1, 2, 41);
dortluEkle(2, 3, 40, 41);
dortluEkle(3, 4, 39, 40);
ucluEkle(4, 5, 48);
dortluEkle(4, 48, 31, 39);
dortluEkle(48, 60, 49, 31);
ucluEkle(60, 59, 49);
ucluEkle(49, 50, 61);
ucluEkle(59, 61, 49);
ucluEkle(59, 66, 61);
dortluEkle(59, 58, 66, 67);
dortluEkle(67, 66, 62, 61);
dortluEkle(61, 62, 52, 50);
dortluEkle(49, 50, 32, 31);
dortluEkle(50, 51, 33, 32);
dortluEkle(39, 31, 28, 27);
ucluEkle(39, 27, 21);*/
for (int u = 0; u < ucgenler.size(); u++) {
ucluEkle(ucgenler.get(u));
}
}
}
for (int i = 0; i < yeniNoktalar.size(); i++) {
ellipse(yeniNoktalar.get(i).x + kaymaMiktari, yeniNoktalar.get(i).y, 5, 5);
}
}
public void ucluEkle(Ucgen u) {
beginShape();
texture(yeniYuz);
noktaEkle(u.i1);
noktaEkle(u.i2);
noktaEkle(u.i3);
endShape();
}
public void ucluEkle(int i1, int i2, int i3) {
beginShape();
texture(yeniYuz);
noktaEkle(i1);
noktaEkle(i2);
noktaEkle(i3);
endShape();
}
public void dortluEkle(int i1, int i2, int i3, int i4) {
beginShape();
texture(yeniYuz);
noktaEkle(i1);
noktaEkle(i2);
noktaEkle(i3);
noktaEkle(i4);
endShape();
}
public void noktaEkle(int index) {
vertex(landmarks.getKeyPoints().get(index).getX(), landmarks.getKeyPoints().get(index).getY(),
yeniNoktalar.get(index).x, yeniNoktalar.get(index).y);
}
public void UcgenleriTespitEt() {
DelaunayTriangulator dt = Triangulate();
for (int i = 0; i < dt.triangles.size(); i++) {
int i1 = indeksBul(dt.triangles.get(i).points[0]);
int i2 = indeksBul(dt.triangles.get(i).points[1]);
int i3 = indeksBul(dt.triangles.get(i).points[2]);
ucgenler.add(new Ucgen(i1, i2, i3));
}
}
public int indeksBul(PVector p) {
for (int i = 0; i < points.size(); i++) {
if (points.get(i).x == p.x && points.get(i).y == p.y) {
return i;
}
}
return -1;
}
// kodun geri kalanı şuradan alıntıdır: https://github.com/robu3/delaunay
ArrayList<PVector> points;
DelaunayTriangulator dt;
DelaunayTriangulator Triangulate() {
dt = new DelaunayTriangulator();
dt.points = points.toArray(new PVector[points.size()]);
dt.triangles = dt.Calculate();
return dt;
}
public class DelaunayTriangulator {
PVector[] points;
ArrayList<Triangle> triangles;
public Triangle GetSuperTriangle() {
// find min & max x and y values
float xMin = points[0].x;
float yMin = points[0].y;
float xMax = xMin;
float yMax = yMin;
for (int i = 0; i < points.length; i++) {
PVector p = points[i];
if (p.x < xMin) {
xMin = p.x;
}
if (p.x > xMax) {
xMax = p.x;
}
if (p.y < xMin) {
xMin = p.y;
}
if (p.y > xMax) {
xMax = p.y;
}
}
float dx = xMax - xMin;
float dy = yMax - yMin;
float dMax = dx > dy ? dx : dy;
float xMid = (xMin + xMax) / 2f;
float yMid = (yMin + yMax) / 2f;
Triangle superTri = new Triangle(
new PVector(xMid - 2f * dMax, yMid - dMax),
new PVector(xMid, yMid + 2f * dMax),
new PVector(xMid + 2f * dMax, yMid - dMax)
);
return superTri;
}
public ArrayList<Triangle> Calculate()
{
ArrayList<Triangle> triangleBuffer = new ArrayList<Triangle>();
ArrayList<Triangle> completed = new ArrayList<Triangle>();
Triangle superTriangle = GetSuperTriangle();
triangleBuffer.add(superTriangle);
PVector point;
ArrayList<Edge> edgeBuffer = new ArrayList<Edge>();
for (int i = 0; i < points.length; i++) {
point = points[i];
edgeBuffer.clear();
for (int j = triangleBuffer.size() - 1; j >= 0; j--) {
Triangle tri = triangleBuffer.get(j);
PVector circumcenter = tri.GetCircumcenter();
float rad = circumcenter.dist(tri.points[0]);
if (circumcenter.x + rad < point.x) {
completed.add(tri);
}
if (circumcenter.dist(point) < rad) {
edgeBuffer.add(new Edge(tri.points[0], tri.points[1]));
edgeBuffer.add(new Edge(tri.points[1], tri.points[2]));
edgeBuffer.add(new Edge(tri.points[2], tri.points[0]));
triangleBuffer.remove(j);
}
}
for (int j = 0; j < edgeBuffer.size() - 1; j++) {
Edge edgeA = edgeBuffer.get(j);
if (edgeA != null) {
for (int k = j + 1; k < edgeBuffer.size(); k++) {
Edge edgeB = edgeBuffer.get(k);
if (edgeA.IsEqual(edgeB)) {
edgeBuffer.set(j, null);
edgeBuffer.set(k, null);
}
}
}
}
for (int j = 0; j < edgeBuffer.size(); j++) {
Edge edge = edgeBuffer.get(j);
if (edge == null) {
continue;
}
Triangle tri = new Triangle(edge.p1, edge.p2, point);
triangleBuffer.add(tri);
}
}
for (int i = triangleBuffer.size() - 1; i >= 0; i--) {
if (triangleBuffer.get(i).SharesVertex(superTriangle)) {
triangleBuffer.remove(i);
}
}
triangles = triangleBuffer;
return triangleBuffer;
}
public DelaunayTriangulator() {
triangles = new ArrayList<Triangle>();
}
}
class Triangle {
PVector[] points;
public PVector GetCircumcenter(PVector a, PVector b, PVector c) {
PVector midAB = Midpoint(a, b);
PVector midBC = Midpoint(b, c);
float slopeAB = -1 / Slope(a, b);
float slopeBC = -1 / Slope(b, c);
float bAB = midAB.y - slopeAB * midAB.x;
float bBC = midBC.y - slopeBC * midBC.x;
float x = (bAB - bBC) / (slopeBC - slopeAB);
PVector circumcenter = new PVector(
x,
(slopeAB * x) + bAB
);
return circumcenter;
}
public PVector GetCircumcenter() {
return GetCircumcenter(points[0], points[1], points[2]);
}
public boolean CircumcircleContains(PVector p) {
PVector center = GetCircumcenter();
float rad = center.dist(points[0]);
return center.dist(p) <= rad;
}
public ArrayList<PVector> GetContainedPoints(PVector[] points) {
ArrayList<PVector> contained = new ArrayList<PVector>();
for (int i = 0; i < points.length; i++) {
if (CircumcircleContains(points[i])) {
contained.add(points[i]);
}
}
return contained;
}
public PVector Midpoint(PVector a, PVector b) {
return new PVector(
(a.x + b.x) / 2,
(a.y + b.y) / 2
);
}
public float Slope(PVector from, PVector to) {
return (to.y - from.y) / (to.x - from.x);
}
public boolean IsInCircle(PVector point, PVector center, float radius) {
return point.dist(center) < radius;
}
public boolean SharesVertex(Triangle other) {
for (int i = 0; i < points.length; i++) {
for (int j = 0; j < other.points.length; j++) {
if (points[i] == other.points[j]) {
return true;
}
}
}
return false;
}
public Triangle() {
points = new PVector[3];
}
public Triangle(PVector[] pts) {
points = pts;
}
public Triangle(PVector a, PVector b, PVector c) {
points = new PVector[3];
points[0] = a;
points[1] = b;
points[2] = c;
}
}
class Edge {
public PVector p1;
public PVector p2;
public boolean IsEqual(Edge other) {
if (other == null) {
return false;
} else {
return (p1 == other.p1 && p2 == other.p2) || (p2 == other.p1 && p1 == other.p2);
}
}
public Edge(PVector a, PVector b) {
p1 = a;
p2 = b;
}
}