Skip to content

Commit 422f92e

Browse files
committed
Throw IOException if classifier file is invalid
1 parent a4a89d8 commit 422f92e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

core/src/main/java/edu/wpi/grip/core/sources/ClassifierSource.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313

1414
import org.bytedeco.javacpp.opencv_objdetect.CascadeClassifier;
1515

16+
import java.io.IOException;
17+
import java.nio.file.Files;
18+
import java.nio.file.Paths;
1619
import java.util.List;
1720
import java.util.Properties;
1821

22+
import static com.google.common.base.Preconditions.checkArgument;
23+
1924
/**
2025
* A source for the path to a XML classifier file (e.g. haarcascade_face_default.xml).
2126
*/
@@ -75,8 +80,16 @@ public Properties getProperties() {
7580
}
7681

7782
@Override
78-
public void initialize() {
79-
classifierSocket.setValue(new CascadeClassifier(filePath));
83+
public void initialize() throws IOException {
84+
if (!Files.exists(Paths.get(filePath))) {
85+
throw new IOException("File does not exist: " + filePath);
86+
}
87+
try {
88+
classifierSocket.setValue(new CascadeClassifier(filePath));
89+
} catch (Exception e) {
90+
// OpenCV will throw an exception if given malformed XML
91+
throw new IOException("Could not load cascade classifier XML", e);
92+
}
8093
}
8194

8295
public interface Factory {

ui/src/main/java/edu/wpi/grip/ui/pipeline/AddSourceButton.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public class AddSourceButton extends MenuButton {
272272
File file = fc.showOpenDialog(getScene().getWindow());
273273
if (file != null) {
274274
ClassifierSource source = classifierSourceFactory.create(file.getAbsolutePath());
275-
source.initialize();
275+
source.initializeSafely();
276276
eventBus.post(new SourceAddedEvent(source));
277277
}
278278
});

0 commit comments

Comments
 (0)