Skip to content

Commit 388fda0

Browse files
yptheangelsaudet
authored andcommitted
HouseNumberDetection: Draw Bbox and label text in diff colours (#913)
* add the feature to draw bbox and label text in different colors to show different classes Signed-off-by: yptheangel <[email protected]> * Made changes to improve simplicity Signed-off-by: yptheangel <[email protected]> * Remove extra semicolons and suffixes Signed-off-by: Samuel Audet <[email protected]>
1 parent 391ec1b commit 388fda0

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/objectdetection/HouseNumberDetection.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import java.util.Random;
5555

5656
import org.bytedeco.opencv.opencv_core.*;
57-
import org.bytedeco.opencv.opencv_imgproc.*;
5857
import static org.bytedeco.opencv.global.opencv_core.*;
5958
import static org.bytedeco.opencv.global.opencv_imgproc.*;
6059

@@ -73,6 +72,18 @@
7372
public class HouseNumberDetection {
7473
private static final Logger log = LoggerFactory.getLogger(HouseNumberDetection.class);
7574

75+
// Enable different colour bounding box for different classes
76+
public static final Scalar RED = RGB(255.0, 0, 0);
77+
public static final Scalar GREEN = RGB(0, 255.0, 0);
78+
public static final Scalar BLUE = RGB(0, 0, 255.0);
79+
public static final Scalar YELLOW = RGB(255.0, 255.0, 0);
80+
public static final Scalar CYAN = RGB(0, 255.0, 255.0);
81+
public static final Scalar MAGENTA = RGB(255.0, 0.0, 255.0);
82+
public static final Scalar ORANGE = RGB(255.0, 128.0, 0);
83+
public static final Scalar PINK = RGB(255.0, 192.0, 203.0);
84+
public static final Scalar LIGHTBLUE = RGB(153.0, 204.0, 255.0);
85+
public static final Scalar VIOLET = RGB(238.0, 130.0, 238.0);
86+
7687
public static void main(String[] args) throws java.lang.Exception {
7788

7889
// parameters matching the pretrained TinyYOLO model
@@ -196,6 +207,8 @@ public static void main(String[] args) throws java.lang.Exception {
196207
(org.deeplearning4j.nn.layers.objdetect.Yolo2OutputLayer)model.getOutputLayer(0);
197208
List<String> labels = train.getLabels();
198209
test.setCollectMetaData(true);
210+
Scalar[] colormap = {RED,BLUE,GREEN,CYAN,YELLOW,MAGENTA,ORANGE,PINK,LIGHTBLUE,VIOLET};
211+
199212
while (test.hasNext() && frame.isVisible()) {
200213
org.nd4j.linalg.dataset.DataSet ds = test.next();
201214
RecordMetaDataImageURI metadata = (RecordMetaDataImageURI)ds.getExampleMetaData().get(0);
@@ -220,8 +233,9 @@ public static void main(String[] args) throws java.lang.Exception {
220233
int y1 = (int) Math.round(h * xy1[1] / gridHeight);
221234
int x2 = (int) Math.round(w * xy2[0] / gridWidth);
222235
int y2 = (int) Math.round(h * xy2[1] / gridHeight);
223-
rectangle(image, new Point(x1, y1), new Point(x2, y2), Scalar.RED);
224-
putText(image, label, new Point(x1 + 2, y2 - 2), FONT_HERSHEY_DUPLEX, 1, Scalar.GREEN);
236+
rectangle(image, new Point(x1, y1), new Point(x2, y2), colormap[obj.getPredictedClass()]);
237+
putText(image, label, new Point(x1 + 2, y2 - 2), FONT_HERSHEY_DUPLEX, 1, colormap[obj.getPredictedClass()]);
238+
225239
}
226240
frame.setTitle(new File(metadata.getURI()).getName() + " - HouseNumberDetection");
227241
frame.setCanvasSize(w, h);

0 commit comments

Comments
 (0)