Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

125
LINES

< > BotCompany Repo | #1009013 // Put troll faces in webcam image [WORKS]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 24318K of libraries. Click here for Pure Java version (599L/5K).

!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<DetectedFace> 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<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);
		}
	}
}

p {
	new FacePainterExample();
}

Author comment

Began life as a copy of #1009011

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, bmzxzaunwzbi, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
1324 stefan Successfully tested on Linux and Windows 2017-06-28 17:53:23

add comment

Snippet ID: #1009013
Snippet name: Put troll faces in webcam image [WORKS]
Eternal ID of this version: #1009013/10
Text MD5: be91c46a26f46d5aff0787f35324b939
Transpilation MD5: db25616b3aeb98c63f609fa7a2ad4ba3
Author: stefan
Category: javax / desktop / camera
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-06-28 20:22:50
Source code size: 3261 bytes / 125 lines
Pitched / IR pitched: No / No
Views / Downloads: 691 / 1355
Version history: 9 change(s)
Referenced in: [show references]