!7 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; /** * Paint troll smile on all detected faces. * * @author Bartosz Firyn (SarXos) */ sclass FacePainterExample extends JFrame implements Runnable, WebcamPanel.Painter { private static final long serialVersionUID = 1L; private static final Executor EXECUTOR = Executors.newSingleThreadExecutor(); private 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); private Webcam webcam = null; private WebcamPanel.Painter painter = null; private List faces = null; private BufferedImage troll = null; public FacePainterExample() throws IOException { super(); 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); setTitle("Face Detector Example"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); EXECUTOR.execute(this); } @Override public void run() { while (true) { if (!webcam.isOpen()) { return; } faces = detector.detectFaces(ImageUtilities.createFImage(webcam.getImage())); } } @Override 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; } Iterator dfi = faces.iterator(); while (dfi.hasNext()) { DetectedFace face = dfi.next(); 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); } } } p { new FacePainterExample(); }