-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
SceneGraphImageObject.java
127 lines (101 loc) · 3.8 KB
/
SceneGraphImageObject.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
package edu.stanford.nlp.scenegraph.image;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.util.Generics;
import edu.stanford.nlp.util.StringUtils;
/**
*
* @author Sebastian Schuster
*
*/
public class SceneGraphImageObject {
public SceneGraphImageBoundingBox boundingBox;
public Set<String> names;
public Set<List<CoreLabel>> labels;
public SceneGraphImageObject(SceneGraphImageBoundingBox boundingBox, List<String> names, List<List<CoreLabel>> labels) {
this.boundingBox = boundingBox;
this.names = new HashSet<String>(names);
if (labels != null) {
this.labels = new HashSet<List<CoreLabel>>(labels);
} else {
this.labels = new HashSet<List<CoreLabel>>();
}
}
@SuppressWarnings("unchecked")
public static SceneGraphImageObject fromJSONObject(SceneGraphImage img, JsonObject obj) {
List<String> names = SceneGraphImageUtils.getJsonStringList(obj, "names");
JsonArray labelArrays = obj.getJsonArray("labels");
List<List<CoreLabel>> labelsList = null;
if (labelArrays != null) {
labelsList = Generics.newArrayList(labelArrays.size());
for (JsonArray arr : labelArrays.getValuesAs(JsonArray.class)) {
List<CoreLabel> tokens = Generics.newArrayList(arr.size());
for (JsonString str : arr.getValuesAs(JsonString.class)) {
tokens.add(SceneGraphImageUtils.labelFromString(str.getString()));
}
labelsList.add(tokens);
}
}
JsonObject boundingBoxObj = obj.getJsonObject("bbox");
if (boundingBoxObj == null) {
throw new NullPointerException("object did not have bbox field");
}
int h = boundingBoxObj.getInt("h");
int w = boundingBoxObj.getInt("w");
int x = boundingBoxObj.getInt("x");
int y = boundingBoxObj.getInt("y");
SceneGraphImageBoundingBox boundingBox = new SceneGraphImageBoundingBox(h, w, x, y);
return new SceneGraphImageObject(boundingBox, names, labelsList);
}
@SuppressWarnings("unchecked")
public JsonObject toJSONObject(SceneGraphImage sceneGraphImage) {
JsonObjectBuilder obj = Json.createObjectBuilder();
JsonObjectBuilder bbox = Json.createObjectBuilder();
bbox.add("h", this.boundingBox.h);
bbox.add("w", this.boundingBox.w);
bbox.add("x", this.boundingBox.x);
bbox.add("y", this.boundingBox.y);
obj.add("bbox", bbox.build());
JsonArrayBuilder names = Json.createArrayBuilder();
for (String name : this.names) {
names.add(name);
}
obj.add("names", names.build());
if (this.labels != null && ! this.labels.isEmpty()) {
JsonArrayBuilder labelsList = Json.createArrayBuilder();
JsonArrayBuilder lemmataList = Json.createArrayBuilder();
for (List<CoreLabel> list : this.labels) {
JsonArrayBuilder labels = Json.createArrayBuilder();
for (CoreLabel lbl : list) {
labels.add(SceneGraphImageUtils.labelToString(lbl));
}
labelsList.add(labels.build());
lemmataList.add(StringUtils.join(list.stream().map(x -> x.lemma() != null ? x.lemma() : x.word()), " "));
}
obj.add("labels", labelsList.build());
obj.add("lemmata", lemmataList.build());
}
return obj.build();
}
public boolean equals(Object other) {
if (!(other instanceof SceneGraphImageObject)) {
return false;
}
SceneGraphImageObject obj = (SceneGraphImageObject) other;
if (!(this.boundingBox.equals(obj.boundingBox)))
return false;
if (!(this.names.equals(obj.names)))
return false;
if (!(this.labels.equals(obj.labels)))
return false;
return true;
}
}