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

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

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: 706 / 1386
Version history: 9 change(s)
Referenced in: [show references]