forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YOLONet.java
288 lines (235 loc) · 9.9 KB
/
YOLONet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
MIT License
Copyright (c) 2021 Florian Bruggisser
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
YOLONet Example Information
---------------------------
This is a basic implementation of a YOLO object detection network inference example.
It works with most variants of YOLO (YOLOv2, YOLOv2-tiny, YOLOv3, YOLOv3-tiny, YOLOv3-tiny-prn, YOLOv4, YOLOv4-tiny).
YOLO9000 is not support by OpenCV DNN.
To run the example download the following files and place them in the root folder of your project:
YOLOv4 Configuration: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg
YOLOv4 Weights: https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
COCO Names: https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names
Dog Demo Image: https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/dog.jpg
For faster inferencing CUDA is highly recommended.
On CPU it is recommended to decrease the width & height of the network or use the tiny variants.
*/
import org.bytedeco.javacpp.FloatPointer;
import org.bytedeco.javacpp.IntPointer;
import org.bytedeco.javacpp.indexer.FloatIndexer;
import org.bytedeco.opencv.global.opencv_dnn;
import org.bytedeco.opencv.opencv_core.*;
import org.bytedeco.opencv.opencv_dnn.Net;
import org.bytedeco.opencv.opencv_text.FloatVector;
import org.bytedeco.opencv.opencv_text.IntVector;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import static org.bytedeco.opencv.global.opencv_core.CV_32F;
import static org.bytedeco.opencv.global.opencv_core.getCudaEnabledDeviceCount;
import static org.bytedeco.opencv.global.opencv_dnn.*;
import static org.bytedeco.opencv.global.opencv_highgui.imshow;
import static org.bytedeco.opencv.global.opencv_highgui.waitKey;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgproc.LINE_8;
import static org.bytedeco.opencv.global.opencv_imgproc.rectangle;
public class YOLONet {
public static void main(String[] args) {
Mat image = imread("dog.jpg");
YOLONet yolo = new YOLONet(
"yolov4.cfg",
"yolov4.weights",
"coco.names",
608, 608);
yolo.setup();
List<ObjectDetectionResult> results = yolo.predict(image);
System.out.printf("Detected %d objects:\n", results.size());
for(ObjectDetectionResult result : results) {
System.out.printf("\t%s - %.2f%%\n", result.className, result.confidence * 100f);
// annotate on image
rectangle(image,
new Point(result.x, result.y),
new Point(result.x + result.width, result.y + result.height),
Scalar.MAGENTA, 2, LINE_8, 0);
}
imshow("YOLO", image);
waitKey();
}
private Path configPath;
private Path weightsPath;
private Path namesPath;
private int width;
private int height;
private float confidenceThreshold = 0.5f;
private float nmsThreshold = 0.4f;
private Net net;
private StringVector outNames;
private List<String> names;
/**
* Creates a new YOLO network.
* @param configPath Path to the configuration file.
* @param weightsPath Path to the weights file.
* @param namesPath Path to the names file.
* @param width Width of the network as defined in the configuration.
* @param height Height of the network as defined in the configuration.
*/
public YOLONet(String configPath, String weightsPath, String namesPath, int width, int height) {
this.configPath = Paths.get(configPath);
this.weightsPath = Paths.get(weightsPath);
this.namesPath = Paths.get(namesPath);
this.width = width;
this.height = height;
}
/**
* Initialises the network.
*
* @return True if the network initialisation was successful.
*/
public boolean setup() {
net = readNetFromDarknet(
configPath.toAbsolutePath().toString(),
weightsPath.toAbsolutePath().toString());
// setup output layers
outNames = net.getUnconnectedOutLayersNames();
// enable cuda backend if available
if (getCudaEnabledDeviceCount() > 0) {
net.setPreferableBackend(opencv_dnn.DNN_BACKEND_CUDA);
net.setPreferableTarget(opencv_dnn.DNN_TARGET_CUDA);
}
// read names file
try {
names = Files.readAllLines(namesPath);
} catch (IOException e) {
System.err.println("Could not read names file!");
e.printStackTrace();
}
return !net.empty();
}
/**
* Runs the object detection on the frame.
*
* @param frame Input frame.
* @return List of objects that have been detected.
*/
public List<ObjectDetectionResult> predict(Mat frame) {
Mat inputBlob = blobFromImage(frame,
1 / 255.0,
new Size(width, height),
new Scalar(0.0),
true, false, CV_32F);
net.setInput(inputBlob);
// run detection
MatVector outs = new MatVector(outNames.size());
net.forward(outs, outNames);
// evaluate result
List<ObjectDetectionResult> result = postprocess(frame, outs);
// cleanup
outs.releaseReference();
inputBlob.release();
return result;
}
/**
* Remove the bounding boxes with low confidence using non-maxima suppression
*
* @param frame Input frame
* @param outs Network outputs
* @return List of objects
*/
private List<ObjectDetectionResult> postprocess(Mat frame, MatVector outs) {
final IntVector classIds = new IntVector();
final FloatVector confidences = new FloatVector();
final RectVector boxes = new RectVector();
for (int i = 0; i < outs.size(); ++i) {
// extract the bounding boxes that have a high enough score
// and assign their highest confidence class prediction.
Mat result = outs.get(i);
FloatIndexer data = result.createIndexer();
for (int j = 0; j < result.rows(); j++) {
// minMaxLoc implemented in java because it is 1D
int maxIndex = -1;
float maxScore = Float.MIN_VALUE;
for (int k = 5; k < result.cols(); k++) {
float score = data.get(j, k);
if (score > maxScore) {
maxScore = score;
maxIndex = k - 5;
}
}
if (maxScore > confidenceThreshold) {
int centerX = (int) (data.get(j, 0) * frame.cols());
int centerY = (int) (data.get(j, 1) * frame.rows());
int width = (int) (data.get(j, 2) * frame.cols());
int height = (int) (data.get(j, 3) * frame.rows());
int left = centerX - width / 2;
int top = centerY - height / 2;
classIds.push_back(maxIndex);
confidences.push_back(maxScore);
boxes.push_back(new Rect(left, top, width, height));
}
}
data.release();
result.release();
}
// remove overlapping bounding boxes with NMS
IntPointer indices = new IntPointer(confidences.size());
FloatPointer confidencesPointer = new FloatPointer(confidences.size());
confidencesPointer.put(confidences.get());
NMSBoxes(boxes, confidencesPointer, confidenceThreshold, nmsThreshold, indices, 1.f, 0);
// create result list
List<ObjectDetectionResult> detections = new ArrayList<>();
for (int i = 0; i < indices.limit(); ++i) {
final int idx = indices.get(i);
final Rect box = boxes.get(idx);
final int clsId = classIds.get(idx);
detections.add(new ObjectDetectionResult() {{
classId = clsId;
className = names.get(clsId);
confidence = confidences.get(idx);
x = box.x();
y = box.y();
width = box.width();
height = box.height();
}});
box.releaseReference();
}
// cleanup
indices.releaseReference();
confidencesPointer.releaseReference();
classIds.releaseReference();
confidences.releaseReference();
boxes.releaseReference();
return detections;
}
/**
* Dataclass for object detection result.
*/
public static class ObjectDetectionResult {
public int classId;
public String className;
public float confidence;
public int x;
public int y;
public int width;
public int height;
}
}