!7 static double confidenceThreshold = 10; sbool setViewSize = false; static double timeout = 10; static int skipFirst = 9; static int nn = 0, totalSamples; lib 1008985 // webcam lib 1008987 // bridj lib 1004016 // slf4j api // OpenIMAJ lib 1004905 lib 1004906 lib 1004908 lib 1004909 lib 1004910 lib 1004911 lib 1004912 lib 1004913 lib 1004914 lib 1004915 lib 1004919 lib 1004920 lib 1009014 // processing.face lib 1009015 // object detection lib 1009016 // xpp (xmlpull implementation) import com.github.sarxos.webcam.*; import org.openimaj.image.ImageUtilities; import org.openimaj.image.processing.face.detection.DetectedFace; import org.openimaj.image.processing.face.detection.HaarCascadeDetector; import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector; p { final new FaceDetector d; d.onDetect.add(new Runnable { new LinkedList> samples; public void run() { int n = l(d.faces); long now = sysNow(); samples.add(pair(now, n)); while (first(samples).a <= now-toMS(timeout)) removeFirst(samples); ++totalSamples; if (totalSamples > skipFirst) { double avg = 0; for (Pair p : samples) avg += p.b; avg /= l(samples); double threshold = nn == 0 ? 0.7 : 0.3; print(avg); int newNN = iceil(avg-threshold); consoleTitle(newNN + " " + formatDouble(avg, 2)); if (newNN != nn) { nn = newNN; onNNChange(); } } } }); d.start(); } svoid onNNChange() { playWAVSnippet( //#1013246 nn > 0 ? #1013246 : #1013251 ); } // original author Bartosz Firyn (SarXos) sclass FaceDetector { new FKEFaceDetector detector; Webcam webcam; L faces; L onDetect = synchroList(); void start() { //webcam = Webcam.getDefault(); // TODO: better web cam selection webcam = last(webcam.getWebcams()); if (setViewSize) webcam.setViewSize(WebcamResolution.VGA.getSize()); webcam.open(true); thread "Face Detection" { while licensed { if (!webcam.isOpen()) return; L _faces = synchroList(); for (DetectedFace f : detector.detectFaces(ImageUtilities.createFImage(webcam.getImage()))) if (f.getConfidence() >= confidenceThreshold) _faces.add(f); faces = _faces; print("Have " + n2(faces, "face")); pnl(map faceToString(faces)); pcallFAll(onDetect); //_print(); } } } } sS faceToString(DetectedFace f) { ret f == null ? "" : f.getBounds() + "*" + f.getConfidence(); }