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).

1  
!7
2  
3  
static double confidenceThreshold = 10;
4  
5  
lib 1008985 // webcam
6  
lib 1008987 // bridj
7  
lib 1004016 // slf4j api
8  
9  
// OpenIMAJ
10  
lib 1004905 lib 1004906 lib 1004908 lib 1004909 lib 1004910
11  
lib 1004911 lib 1004912 lib 1004913 lib 1004914 lib 1004915
12  
lib 1004919 lib 1004920
13  
lib 1009014 // processing.face
14  
lib 1009015 // object detection
15  
lib 1009016 // xpp (xmlpull implementation)
16  
17  
import com.github.sarxos.webcam.*;
18  
19  
import org.openimaj.image.ImageUtilities;
20  
import org.openimaj.image.processing.face.detection.DetectedFace;
21  
import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
22  
import org.openimaj.math.geometry.shape.Rectangle;
23  
24  
p { new FacePainterExample; }
25  
26  
/**
27  
 * Paint troll smile on all detected faces.
28  
 * 
29  
 * @author Bartosz Firyn (SarXos)
30  
 */
31  
sclass FacePainterExample extends JFrame implements WebcamPanel.Painter {
32  
	static final Executor EXECUTOR = Executors.newSingleThreadExecutor();
33  
	static final HaarCascadeDetector detector = new HaarCascadeDetector();
34  
	private static final Stroke STROKE = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[] { 1.0f }, 0.0f);
35  
36  
	Webcam webcam;
37  
	WebcamPanel.Painter painter;
38  
	L<DetectedFace> faces;
39  
	BufferedImage troll;
40  
41  
	public FacePainterExample() throws IOException {
42  
    super("Face Detector Example");
43  
    troll = loadImage2(#1009012);
44  
45  
		//webcam = Webcam.getDefault();
46  
		webcam = last(webcam.getWebcams()); // Hope this prefers external webcams
47  
		webcam.setViewSize(WebcamResolution.VGA.getSize());
48  
		webcam.open(true);
49  
50  
		WebcamPanel panel = new WebcamPanel(webcam, false);
51  
		panel.setPreferredSize(WebcamResolution.VGA.getSize());
52  
		panel.setPainter(this);
53  
		panel.setFPSDisplayed(true);
54  
		panel.setFPSLimited(true);
55  
		panel.setFPSLimit(20);
56  
		panel.setPainter(this);
57  
		panel.start();
58  
59  
		painter = panel.getDefaultPainter();
60  
		add(panel);
61  
62  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
63  
		pack();
64  
		setLocationRelativeTo(null);
65  
		setVisible(true);
66  
67  
		EXECUTOR.execute(r {
68  
  		while licensed {
69  
  			if (!webcam.isOpen()) return;
70  
  			
71  
  			L<DetectedFace> _faces = synchroList();
72  
  			for (DetectedFace f : detector.detectFaces(ImageUtilities.createFImage(webcam.getImage())))
73  
  			  if (f.getConfidence() >= confidenceThreshold)
74  
  			    _faces.add(f);
75  
  			faces = _faces;
76  
  			
77  
  			pnl(faces);
78  
  			_print();
79  
  		}
80  
  	});
81  
	}
82  
83  
	public void paintPanel(WebcamPanel panel, Graphics2D g2) {
84  
		if (painter != null) painter.paintPanel(panel, g2);
85  
	}
86  
87  
	@Override
88  
	public void paintImage(WebcamPanel panel, BufferedImage image, Graphics2D g2) {
89  
		if (painter != null) painter.paintImage(panel, image, g2);
90  
		if (faces == null) return;
91  
92  
		for (DetectedFace face : faces) {
93  
			Rectangle bounds = face.getBounds();
94  
95  
			int dx = (int) (0.1 * bounds.width);
96  
			int dy = (int) (0.2 * bounds.height);
97  
			int x = (int) bounds.x - dx;
98  
			int y = (int) bounds.y - dy;
99  
			int w = (int) bounds.width + 2 * dx;
100  
			int h = (int) bounds.height + dy;
101  
102  
			g2.drawImage(troll, x, y, w, h, null);
103  
			g2.setStroke(STROKE);
104  
			g2.setColor(Color.RED);
105  
			g2.drawRect(x, y, w, h);
106  
		}
107  
	}
108  
}

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: 630 / 1278
Version history: 5 change(s)
Referenced in: [show references]