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

108
LINES

< > BotCompany Repo | #1009812 // Put troll faces in webcam image [with threshold]

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

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

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

Author comment

Began life as a copy of #1009013

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1009812
Snippet name: Put troll faces in webcam image [with threshold]
Eternal ID of this version: #1009812/6
Text MD5: 803f5e927d272f684ce822b8f14b6a43
Transpilation MD5: 9a3f2e73e43d2cf4f614de85ae679fa3
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-12-29 09:11:22
Source code size: 3178 bytes / 108 lines
Pitched / IR pitched: No / No
Views / Downloads: 631 / 1278
Version history: 5 change(s)
Referenced in: #1013245 - Face detection without webcam window