!7 static double confidenceThreshold = 10; 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.math.geometry.shape.Rectangle; p { new FacePainterExample; } /** * Paint troll smile on all detected faces. * * @author Bartosz Firyn (SarXos) */ sclass FacePainterExample extends JFrame implements WebcamPanel.Painter { static final Executor EXECUTOR = Executors.newSingleThreadExecutor(); static final HaarCascadeDetector detector = new HaarCascadeDetector(); private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f); Webcam webcam; WebcamPanel.Painter painter; L faces; BufferedImage troll; public FacePainterExample() throws IOException { super("Face Detector Example"); troll = loadImage2(#1009012); //webcam = Webcam.getDefault(); webcam = last(webcam.getWebcams()); // Hope this prefers external webcams webcam.setViewSize(WebcamResolution.VGA.getSize()); webcam.open(true); WebcamPanel panel = new WebcamPanel(webcam, false); panel.setPreferredSize(WebcamResolution.VGA.getSize()); panel.setPainter(this); panel.setFPSDisplayed(true); panel.setFPSLimited(true); panel.setFPSLimit(20); panel.setPainter(this); panel.start(); painter = panel.getDefaultPainter(); add(panel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); EXECUTOR.execute(r { 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; pnl(faces); _print(); } }); } public void paintPanel(WebcamPanel panel, Graphics2D g2) { if (painter != null) painter.paintPanel(panel, g2); } @Override public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) { if (painter != null) painter.paintImage(panel, image, g2); if (faces == null) return; for (DetectedFace face : faces) { Rectangle bounds = face.getBounds(); int dx = (int) (0.1 * bounds.width); int dy = (int) (0.2 * bounds.height); int x = (int) bounds.x - dx; int y = (int) bounds.y - dy; int w = (int) bounds.width + 2 * dx; int h = (int) bounds.height + dy; g2.drawImage(troll, x, y, w, h, null); g2.setStroke(STROKE); g2.setColor(Color.RED); g2.drawRect(x, y, w, h); } } }