Sonntag, 21. Dezember 2014

Programmierung Gesichtserkennung mit der Web-Camera

Programmierung Gesichtserkennung mit der Web-Camera
Author D.Selzer-McKenzie
 
import processing.video.*;
import monclubelec.javacvPro.*;
import java.awt.*; // pour classes Point , Rectangle..
import java.util.*;
// Für WebCam:
Capture cam1;
//GSMovie cam1;
 
OpenCV opencv; // deklariert ein OpenCV Objekt
Rectangle[] faceRect;
 
// A list of my Face objects
ArrayList faceList;
 
// how many have I found over all time
int faceCount = 0;
 
float  sum=0;
int scl=1;
int frame=0;
boolean delFaces;
 
void setup() {
 
// Für WebCam:
cam1= new Capture(this, 1280, 720);
//cam1 = new GSMovie(this, "maus.mov");
 
// für WebCam auskommentieren:
//cam1.play();
cam1.start();
 
// initialisiert OpenCV ---
opencv = new OpenCV(this);
 
//Vorsicht: bei der Arbeit mit einer Datei muss die Größe genau passen!!!
//opencv.allocate(640, 360);
opencv.allocate(cam1.width, cam1.height);
 
// Für WebCam:
// opencv.allocate(cam1.getSourceWidth(), cam1.getSourceHeight()); // initialisiert die Buffer von OpenCV
size (opencv.width(), opencv.height());
 
// Laden Beschreibungsdatei
opencv.cascade("/usr/share/opencv/haarcascades/", "haarcascade_frontalface_alt_tree.xml");
 
// Liste mit den Gesichtsobjekten
faceList = new ArrayList();
}
 
void draw() {
 
// Dateien und die WebCam brauchen etwas Zeit zum Laden
if (cam1.available()) {
 
// Einzelne Frames werden gelesen
cam1.read();
opencv.copy(cam1);
 
// Schnitt oder Kameraschwenk erkennen
println(abs(opencv.sum()-sum)/1000000);
if (abs(opencv.sum()-sum)/1000000 >7) delFaces=true;
else delFaces=false;
sum=opencv.sum();
 
// Erkennen
faceRect = opencv.detect(false);
 
image(opencv.getBuffer(), 0, 0);
 
//Rechteck zeichnen
//opencv.drawRectDetect(true);
 
// Code ab hier von Daniel Shiffman
// SCENARIO 1: faceList is empty
if (faceList.isEmpty()) {
// Just make a Face object for every face Rectangle
for (int i = 0; i < faceRect.length; i++) {
faceList.add(new Face(faceRect[i].x, faceRect[i].y, faceRect[i].width, faceRect[i].height));
}
// SCENARIO 2: We have fewer Face objects than face Rectangles found from OPENCV
}
else if (faceList.size() <= faceRect.length) {
boolean[] used = new boolean[faceRect.length];
// Match existing Face objects with a Rectangle
for (Face f : faceList) {
// Find faces[index] that is closest to face f
// set used[index] to true so that it can't be used twice
float record = 50000;
int index = -1;
for (int i = 0; i < faceRect.length; i++) {
float d = dist(faceRect[i].x, faceRect[i].y, f.r.x, f.r.y);
if (d < record && !used[i]) {
record = d;
index = i;
}
}
// Update Face object location
used[index] = true;
f.update(faceRect[index]);
}
// Add any unused faces
for (int i = 0; i < faceRect.length; i++) {
if (!used[i]) {
faceList.add(new Face(faceRect[i].x, faceRect[i].y, faceRect[i].width, faceRect[i].height));
}
}
// SCENARIO 3: We have more Face objects than face Rectangles found
}
else {
// All Face objects start out as available
for (Face f : faceList) {
f.available = true;
}
// Match Rectangle with a Face object
for (int i = 0; i < faceRect.length; i++) {
// Find face object closest to faces[i] Rectangle
// set available to false
float record = 50000;
int index = -1;
for (int j = 0; j < faceList.size(); j++) {
Face f = faceList.get(j);
float d = dist(faceRect[i].x, faceRect[i].y, f.r.x, f.r.y);
if (d < record && f.available) {
record = d;
index = j;
}
}
// Update Face object location
Face f = faceList.get(index);
f.available = false;
f.update(faceRect[i]);
}
// Start to kill any left over Face objects
for (Face f : faceList) {
if (f.available) {
f.countDown();
if (f.dead()) {
f.delete = true;
}
}
}
}
 
// Delete any that should be deleted
for (int i = faceList.size()-1; i >= 0; i--) {
Face f = faceList.get(i);
// Bei einem Schnitt werden alle Gesichter sofort gelöscht
if (f.delete || delFaces) {
faceList.remove(i);
}
}
 
// Draw all the faces
for (int i = 0; i < faceRect.length; i++) {
noFill();
stroke(255, 0, 0);
rect(faceRect[i].x*scl, faceRect[i].y*scl, faceRect[i].width*scl, faceRect[i].height*scl);
}
 
for (Face f : faceList) {
f.display();
}
}
}
 
void keyReleased() {
if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
 
// for Movie
//frame+=500;
//cam1.jump(frame);
//cam1.play();
}
 
// timestamp
String timestamp() {
Calendar now = Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}
 
// Which Face Is Which
// Daniel Shiffman
// April 25, 2011
 
class Face {
 
// A Rectangle
Rectangle r;
 
// Am I available to be matched?
boolean available;
 
// Should I be deleted?
boolean delete;
 
// How long should I live if I have disappeared?
int timer = 63;
 
// Assign a number to each face
int id;
 
// Make me
Face(int x, int y, int w, int h) {
r = new Rectangle(x, y, w, h);
available = true;
delete = false;
id = faceCount;
faceCount++;
}
 
// Show me
void display() {
fill(0, 0, 255, timer*2);
stroke(0, 0, 255);
rect(r.x*scl, r.y*scl, r.width*scl, r.height*scl);
fill(255, timer*2);
text(""+id, r.x*scl+10, r.y*scl+30);
}
 
// Give me a new location / size
// Oooh, it would be nice to lerp here!
void update(Rectangle newR) {
r = (Rectangle) newR.clone();
}
 
// Count me down, I am gone
void countDown() {
timer--;
}
 
// I am deed, delete me
boolean dead() {
if (timer < 0) return true;
return false;
}
}

Keine Kommentare:

Kommentar veröffentlichen

Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.