package com.github.sarxos.webcam; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Stroke; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import javax.imageio.ImageIO; import javax.swing.JFrame; 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) */ public class 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<DetectedFace> faces = null; private BufferedImage troll = null; public FacePainterExample() throws IOException { super(); troll = ImageIO.read(getClass().getResourceAsStream("/troll-face.png")); webcam = Webcam.getDefault(); 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<DetectedFace> 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); } } public static void main(String[] args) throws IOException { new FacePainterExample(); } }