質問編集履歴
3
運営による修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
JavaFXを用いた映像から顔を検出しモザイクをかけるプログラム
|
test
CHANGED
@@ -1 +1,215 @@
|
|
1
|
+
###前提・実現したいこと
|
2
|
+
|
3
|
+
JavaFXを用いた映像から顔を検出し顔にモザイクをかけるプログラムを作成しています。
|
4
|
+
|
5
|
+
実行するとウィンドウが出てきてそこにカメラが映している映像が流れ、顔の部分に長方形が出るようになっています。今回お聞きしたいのは、この映像をモザイクにするメソッドを作成しモザイク処理ができるようにしたいです。よろしくお願いします。
|
6
|
+
|
7
|
+
###該当のソースコード
|
8
|
+
|
9
|
+
//記述言語 java
|
10
|
+
|
11
|
+
```
|
12
|
+
|
13
|
+
import java.io.ByteArrayInputStream;
|
14
|
+
|
15
|
+
import javafx.animation.AnimationTimer;
|
16
|
+
|
17
|
+
import javafx.application.Application;
|
18
|
+
|
19
|
+
import javafx.event.EventHandler;
|
20
|
+
|
21
|
+
import javafx.scene.Group;
|
22
|
+
|
23
|
+
import javafx.scene.Scene;
|
24
|
+
|
25
|
+
import javafx.scene.canvas.Canvas;
|
26
|
+
|
27
|
+
import javafx.scene.canvas.GraphicsContext;
|
28
|
+
|
29
|
+
import javafx.scene.image.Image;
|
30
|
+
|
31
|
+
import javafx.stage.Stage;
|
32
|
+
|
33
|
+
import javafx.stage.WindowEvent;
|
34
|
+
|
35
|
+
import org.opencv.core.Core;
|
36
|
+
|
37
|
+
import org.opencv.core.Mat;
|
38
|
+
|
39
|
+
import org.opencv.core.MatOfByte;
|
40
|
+
|
41
|
+
import org.opencv.core.MatOfRect;
|
42
|
+
|
43
|
+
import org.opencv.core.Point;
|
44
|
+
|
45
|
+
import org.opencv.core.Rect;
|
46
|
+
|
47
|
+
import org.opencv.core.Scalar;
|
48
|
+
|
49
|
+
import org.opencv.imgcodecs.Imgcodecs;
|
50
|
+
|
51
|
+
import org.opencv.videoio.VideoCapture;
|
52
|
+
|
53
|
+
import org.opencv.videoio.Videoio;
|
54
|
+
|
55
|
+
import org.opencv.imgproc.Imgproc;
|
56
|
+
|
57
|
+
import org.opencv.objdetect.CascadeClassifier;
|
58
|
+
|
59
|
+
public class NewClass extends Application{
|
60
|
+
|
61
|
+
private int camWidth;
|
62
|
+
|
63
|
+
private int camHeight;
|
64
|
+
|
65
|
+
VideoCapture capture;
|
66
|
+
|
67
|
+
Canvas canvas;
|
68
|
+
|
69
|
+
GraphicsContext gc;
|
70
|
+
|
71
|
+
Stage stage;
|
72
|
+
|
73
|
+
AnimationTimer timer;
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
@Override
|
80
|
+
|
81
|
+
public void start(Stage stage){
|
82
|
+
|
83
|
+
this.stage=stage;
|
84
|
+
|
85
|
+
initOpenCv();
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
canvas = new Canvas(camWidth,camHeight);
|
90
|
+
|
91
|
+
gc = canvas.getGraphicsContext2D();
|
92
|
+
|
93
|
+
Group group = new Group(canvas);
|
94
|
+
|
95
|
+
Scene scene = new Scene(group,camWidth,camHeight);
|
96
|
+
|
97
|
+
stage.setScene(scene);
|
98
|
+
|
99
|
+
stage.show();
|
100
|
+
|
101
|
+
timer = new CameraAnimation();
|
102
|
+
|
103
|
+
timer.start();
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
private static Mat dObj(CascadeClassifier objDetector,Mat src){
|
108
|
+
|
109
|
+
Mat dst = src.clone();
|
110
|
+
|
111
|
+
MatOfRect objDetections = new MatOfRect();
|
112
|
+
|
113
|
+
objDetector.detectMultiScale(dst,objDetections);
|
114
|
+
|
115
|
+
System.out.println("Detected"+objDetections.toArray().length+"face");
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
if(objDetections.toArray().length<=0) return src;
|
120
|
+
|
121
|
+
for(Rect rect : objDetections.toArray()){
|
122
|
+
|
123
|
+
Imgproc.rectangle(dst,new Point(rect.x,rect.y),
|
124
|
+
|
125
|
+
new Point(rect.x+rect.width,rect.height),
|
126
|
+
|
127
|
+
new Scalar(0,0,255),2);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
return dst;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
public void initOpenCv(){
|
136
|
+
|
137
|
+
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
138
|
+
|
139
|
+
capture = new VideoCapture(0);
|
140
|
+
|
141
|
+
System.out.println("Camera open: " + capture.isOpened());
|
142
|
+
|
143
|
+
camWidth = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
|
144
|
+
|
145
|
+
camHeight = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
|
146
|
+
|
147
|
+
stage .setOnCloseRequest(new EventHandler<WindowEvent>(){
|
148
|
+
|
149
|
+
@Override
|
150
|
+
|
151
|
+
public void handle(WindowEvent event){
|
152
|
+
|
153
|
+
timer.stop();
|
154
|
+
|
155
|
+
capture.release();
|
156
|
+
|
157
|
+
System.out.println("Camera released");
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
});
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
public class CameraAnimation extends AnimationTimer{
|
166
|
+
|
167
|
+
Mat capImg = new Mat();
|
168
|
+
|
169
|
+
Mat dst = new Mat();
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
@Override
|
174
|
+
|
175
|
+
public void handle(long now){
|
176
|
+
|
177
|
+
Imgproc.rectangle(capImg,new Point(),new Point(),new Scalar(0,0,255,2));
|
178
|
+
|
1
|
-
m
|
179
|
+
CascadeClassifier objDetector = new CascadeClassifier("haarcascade_frontalface_default.xml");
|
180
|
+
|
181
|
+
if(objDetector.empty()) throw new RuntimeException("no xml file");
|
182
|
+
|
183
|
+
capture.read(capImg);
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
capImg = dObj(objDetector,capImg);
|
188
|
+
|
189
|
+
Image img = mat2Image(capImg);
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
gc.drawImage(img,0,0);
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
public static Image mat2Image(Mat mat){
|
204
|
+
|
205
|
+
MatOfByte buffer = new MatOfByte();
|
206
|
+
|
207
|
+
Imgcodecs.imencode(".png",mat,buffer);
|
208
|
+
|
209
|
+
return new Image(new ByteArrayInputStream(buffer.toArray()));
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
```
|
2
んklんlkんkんkんkn
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
l,m;kmlんkんlんlkm。
|
test
CHANGED
File without changes
|
1
m
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,221 +1 @@
|
|
1
|
-
###前提・実現したいこと
|
2
|
-
|
3
|
-
JavaFXを用いた映像から顔を検出し顔にモザイクをかけるプログラムを作成しています。
|
4
|
-
|
5
|
-
実行するとウィンドウが出てきてそこにカメラが映している映像が流れ、顔の部分に長方形が出るようになっています。今回お聞きしたいのは、この映像をモザイクにするメソッドを作成しモザイク処理ができるようにしたいです。よろしくお願いします。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
###該当のソースコード
|
12
|
-
|
13
|
-
//記述言語 java
|
14
|
-
|
15
|
-
import java.io.ByteArrayInputStream;
|
16
|
-
|
17
|
-
import javafx.animation.AnimationTimer;
|
18
|
-
|
19
|
-
import javafx.application.Application;
|
20
|
-
|
21
|
-
import javafx.event.EventHandler;
|
22
|
-
|
23
|
-
import javafx.scene.Group;
|
24
|
-
|
25
|
-
import javafx.scene.Scene;
|
26
|
-
|
27
|
-
import javafx.scene.canvas.Canvas;
|
28
|
-
|
29
|
-
import javafx.scene.canvas.GraphicsContext;
|
30
|
-
|
31
|
-
import javafx.scene.image.Image;
|
32
|
-
|
33
|
-
import javafx.stage.Stage;
|
34
|
-
|
35
|
-
import javafx.stage.WindowEvent;
|
36
|
-
|
37
|
-
import org.opencv.core.Core;
|
38
|
-
|
39
|
-
import org.opencv.core.Mat;
|
40
|
-
|
41
|
-
import org.opencv.core.MatOfByte;
|
42
|
-
|
43
|
-
import org.opencv.core.MatOfRect;
|
44
|
-
|
45
|
-
import org.opencv.core.Point;
|
46
|
-
|
47
|
-
import org.opencv.core.Rect;
|
48
|
-
|
49
|
-
import org.opencv.core.Scalar;
|
50
|
-
|
51
|
-
import org.opencv.imgcodecs.Imgcodecs;
|
52
|
-
|
53
|
-
import org.opencv.videoio.VideoCapture;
|
54
|
-
|
55
|
-
import org.opencv.videoio.Videoio;
|
56
|
-
|
57
|
-
import org.opencv.imgproc.Imgproc;
|
58
|
-
|
59
|
-
import org.opencv.objdetect.CascadeClassifier;
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
public class NewClass extends Application{
|
66
|
-
|
67
|
-
private int camWidth;
|
68
|
-
|
69
|
-
private int camHeight;
|
70
|
-
|
71
|
-
VideoCapture capture;
|
72
|
-
|
73
|
-
Canvas canvas;
|
74
|
-
|
75
|
-
GraphicsContext gc;
|
76
|
-
|
77
|
-
Stage stage;
|
78
|
-
|
79
|
-
AnimationTimer timer;
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
@Override
|
88
|
-
|
89
|
-
public void start(Stage stage){
|
90
|
-
|
91
|
-
this.stage=stage;
|
92
|
-
|
93
|
-
initOpenCv();
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
canvas = new Canvas(camWidth,camHeight);
|
98
|
-
|
99
|
-
gc = canvas.getGraphicsContext2D();
|
100
|
-
|
101
|
-
Group group = new Group(canvas);
|
102
|
-
|
103
|
-
Scene scene = new Scene(group,camWidth,camHeight);
|
104
|
-
|
105
|
-
stage.setScene(scene);
|
106
|
-
|
107
|
-
stage.show();
|
108
|
-
|
109
|
-
timer = new CameraAnimation();
|
110
|
-
|
111
|
-
timer.start();
|
112
|
-
|
113
|
-
}
|
114
|
-
|
115
|
-
private static Mat dObj(CascadeClassifier objDetector,Mat src){
|
116
|
-
|
117
|
-
Mat dst = src.clone();
|
118
|
-
|
119
|
-
MatOfRect objDetections = new MatOfRect();
|
120
|
-
|
121
|
-
objDetector.detectMultiScale(dst,objDetections);
|
122
|
-
|
123
|
-
System.out.println("Detected"+objDetections.toArray().length+"face");
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
if(objDetections.toArray().length<=0) return src;
|
128
|
-
|
129
|
-
for(Rect rect : objDetections.toArray()){
|
130
|
-
|
131
|
-
Imgproc.rectangle(dst,new Point(rect.x,rect.y),
|
132
|
-
|
133
|
-
new Point(rect.x+rect.width,rect.height),
|
134
|
-
|
135
|
-
new Scalar(0,0,255),2);
|
136
|
-
|
137
|
-
}
|
138
|
-
|
139
|
-
return dst;
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
public void initOpenCv(){
|
144
|
-
|
145
|
-
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
146
|
-
|
147
|
-
capture = new VideoCapture(0);
|
148
|
-
|
149
|
-
System.out.println("Camera open: " + capture.isOpened());
|
150
|
-
|
151
|
-
camWidth = (int)capture.get(Videoio.CAP_PROP_FRAME_WIDTH);
|
152
|
-
|
153
|
-
camHeight = (int)capture.get(Videoio.CAP_PROP_FRAME_HEIGHT);
|
154
|
-
|
155
|
-
stage .setOnCloseRequest(new EventHandler<WindowEvent>(){
|
156
|
-
|
157
|
-
@Override
|
158
|
-
|
159
|
-
public void handle(WindowEvent event){
|
160
|
-
|
161
|
-
timer.stop();
|
162
|
-
|
163
|
-
capture.release();
|
164
|
-
|
165
|
-
System.out.println("Camera released");
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
});
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
public class CameraAnimation extends AnimationTimer{
|
174
|
-
|
175
|
-
Mat capImg = new Mat();
|
176
|
-
|
177
|
-
Mat dst = new Mat();
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
@Override
|
182
|
-
|
183
|
-
public void handle(long now){
|
184
|
-
|
185
|
-
Imgproc.rectangle(capImg,new Point(),new Point(),new Scalar(0,0,255,2));
|
186
|
-
|
187
|
-
|
1
|
+
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
|
188
|
-
|
189
|
-
if(objDetector.empty()) throw new RuntimeException("no xml file");
|
190
|
-
|
191
|
-
capture.read(capImg);
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
capImg = dObj(objDetector,capImg);
|
196
|
-
|
197
|
-
Image img = mat2Image(capImg);
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
gc.drawImage(img,0,0);
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
public static Image mat2Image(Mat mat){
|
212
|
-
|
213
|
-
MatOfByte buffer = new MatOfByte();
|
214
|
-
|
215
|
-
Imgcodecs.imencode(".png",mat,buffer);
|
216
|
-
|
217
|
-
return new Image(new ByteArrayInputStream(buffer.toArray()));
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
}
|