-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.java
45 lines (39 loc) · 1.44 KB
/
main.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
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.TensorFlow;
import org.tensorflow.framework.GraphDef;
import org.tensorflow.framework.TensorProto;
import org.tensorflow.framework.TensorShapeProto;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class PigeonSpeciesDetector {
private static byte[] readAllBytesOrExit(String path) {
try {
return Files.readAllBytes(Paths.get(path));
} catch (IOException e) {
System.err.println("Failed to read [" + path + "]: " + e.getMessage());
System.exit(1);
return null;
}
}
private static GraphDef loadGraphDef(String modelPath) {
byte[] graphDef = readAllBytesOrExit(modelPath);
try {
return GraphDef.parseFrom(graphDef);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String modelPath = "path/to/your/model.pb";
GraphDef graphDef = loadGraphDef(modelPath);
try (Graph graph = new Graph()) {
graph.importGraphDef(graphDef.toByteArray());
try (Session session = new Session(graph)) {
// The model is now loaded and ready to be used for predictions.
// Continue to the next steps to preprocess image data and perform predictions.
}
}
}
}