-
Notifications
You must be signed in to change notification settings - Fork 5
/
SetRealValues2.java
224 lines (191 loc) · 8.12 KB
/
SetRealValues2.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
package org.genericsystem.cv.watch;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.comparator.ComputeTrainedScores;
import org.genericsystem.cv.model.Doc;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass;
import org.genericsystem.cv.model.ImgFilter;
import org.genericsystem.cv.model.ImgFilter.ImgFilterInstance;
import org.genericsystem.cv.model.ZoneGeneric;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
import org.genericsystem.cv.watch.SetRealValues2.DocumentDiv;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.annotations.BindAction;
import org.genericsystem.reactor.annotations.BindText;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.DependsOnModel;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.SetText;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.annotations.Style.FlexDirectionStyle;
import org.genericsystem.reactor.annotations.StyleClass;
import org.genericsystem.reactor.appserver.ApplicationServer;
import org.genericsystem.reactor.context.ContextAction;
import org.genericsystem.reactor.context.ContextAction.CANCEL;
import org.genericsystem.reactor.context.ObservableListExtractor;
import org.genericsystem.reactor.context.TextBinding;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlButton;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlDiv;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlH1;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlImg;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlLabel;
import org.genericsystem.reactor.gscomponents.InputTextWithConversion.InputTextEditorWithConversion;
import org.genericsystem.reactor.gscomponents.AppHeader;
import org.genericsystem.reactor.gscomponents.FlexDirection;
import org.genericsystem.reactor.gscomponents.FlexDiv;
import org.genericsystem.reactor.gscomponents.RootTagImpl;
import org.genericsystem.reactor.gscomponents.AppHeader.AppTitleDiv;
import org.genericsystem.reactor.gscomponents.AppHeader.Logo;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
/**
* The SetRealValues class can be used to provide accurate values for the text
* fields (zones).
*
* These real values are stored in GS, and used by {@link ComputeTrainedScores}
* to compute the scores for each zone/filter pairs.
*
* @author Pierrik Lassalas
*
*/
@Deprecated
@DependsOnModel({ Doc.class, DocClass.class, ZoneGeneric.class, ZoneText.class })
@Style(name = "background-color", value = "#ffffff")
@Children({ AppHeader.class, DocumentDiv.class })
@Style(path = AppHeader.class, name = "background-color", value = "#00afeb")
@Children(path = AppHeader.class, value = { Logo.class, AppTitleDiv.class })
@SetText(path = { AppHeader.class, AppTitleDiv.class, HtmlH1.class }, value = "OCR results comparator")
public class SetRealValues2 extends RootTagImpl {
private static final String docClass = "id-fr-front";
private static final String gsPath = "/gs-cv_model";
public static void main(String[] mainArgs) {
ApplicationServer.startSimpleGenericApp(mainArgs, SetRealValues2.class, gsPath);
}
// For each document in the doc class, create a div with an image and the
// input text
@ForEach(DOC_CLASS_SELECTOR.class)
@Children({ Image.class, TextDiv.class })
@StyleClass("document-div")
public static class DocumentDiv extends HtmlDiv {
}
// Map the image source to the filename stored in GS
public static class Image extends HtmlImg {
@Override
public void init() {
bindAttribute("src", "imgadr",
context -> new SimpleStringProperty((String) context.getGeneric().getValue()));
}
}
// Define the textdiv
@Children({ ZoneLabelInput.class, Validate.class, Cancel.class })
public static class TextDiv extends HtmlDiv {
}
// For each zone, create label + inputText + print the results for all the
// filters
@Children({ ZoneLabel.class, ZoneInput.class, FiltersDiv.class })
@ForEach(SELECTOR.class)
public static class ZoneLabelInput extends HtmlDiv {
}
// Define the zone label
@BindText(ZONE_LABEL.class)
public static class ZoneLabel extends HtmlLabel {
}
// Define the inputText
@BindText
public static class ZoneInput extends InputTextEditorWithConversion {
}
// For each filter, create a row with the filtername and the results of the
// ocr
@FlexDirectionStyle(FlexDirection.ROW)
@StyleClass("ocr-row")
@Children({ FiltersList.class, FiltersTextList.class })
@ForEach(OCR_SELECTOR.class)
public static class FiltersDiv extends FlexDiv {
}
// Print the filtername
@FlexDirectionStyle(FlexDirection.COLUMN)
@Style(name = "flex", value = "1")
@BindText
@StyleClass({ "ocr", "ocr-label" })
public static class FiltersList extends FlexDiv {
}
// Print the ocr text for the corresponding filter
@FlexDirectionStyle(FlexDirection.COLUMN)
@Style(name = "flex", value = "4")
@BindText(OCR_LABEL.class)
@StyleClass({ "ocr", "ocr-text" })
public static class FiltersTextList extends FlexDiv {
}
// Create a validate button to persist the changes
@SetText("Save")
@BindAction(value = SAVE.class)
public static class Validate extends HtmlButton {
}
// Create a cancel button to cancel the changes
@SetText("Cancel")
@BindAction(value = CANCEL.class)
public static class Cancel extends HtmlButton {
}
public static class SELECTOR implements ObservableListExtractor {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Generic currentDoc = generics[0];
Root root = currentDoc.getRoot();
System.out.println("Document : " + currentDoc);
Snapshot<ZoneTextInstance> zoneTextInstances = (Snapshot) currentDoc.getHolders(root.find(ZoneText.class));
return (ObservableList) zoneTextInstances.toObservableList()
.filtered(zt -> "reality".equals(((ZoneTextInstance) zt).getImgFilter().getValue()))
.sorted((g1, g2) -> Integer.compare(g1.getZoneNum(), g2.getZoneNum()));
}
}
public static class DOC_CLASS_SELECTOR implements ObservableListExtractor {
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Root root = generics[0].getRoot();
Generic currentDocClass = root.find(DocClass.class).getInstance(docClass);
System.out.println("Current doc class : " + currentDocClass);
Snapshot<Generic> docInstances = currentDocClass.getHolders(root.find(Doc.class));
return docInstances.toObservableList();
}
}
public static class OCR_SELECTOR implements ObservableListExtractor {
@Override
public ObservableList<Generic> apply(Generic[] generics) {
Root root = generics[0].getRoot();
ZoneTextInstance zti = (ZoneTextInstance) generics[0];
System.out.println("zti : " + zti.getZoneNum() + " " + zti.getImgFilter() + " " + zti.getDoc());
Snapshot<Generic> filters = root.find(ImgFilter.class).getInstances();
return filters.toObservableList();
}
}
public static class OCR_LABEL implements TextBinding {
@Override
public ObservableValue<String> apply(Context context, Tag tag) {
ImgFilterInstance ifi = (ImgFilterInstance) context.getGenerics()[0];
ZoneTextInstance zti = (ZoneTextInstance) context.getGenerics()[1];
DocInstance doc = zti.getDoc();
ZoneText zt = (ZoneText) ifi.getRoot().find(ZoneText.class);
ZoneTextInstance text = zt.getZoneText(doc, zti.getZone(), ifi);
return new SimpleStringProperty(text.getValue().toString());
}
}
public static class SAVE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Saving text for class " + docClass);
context.getGeneric().getRoot().getCurrentCache().flush();
}
}
public static class ZONE_LABEL implements TextBinding {
@Override
public ObservableValue<String> apply(Context context, Tag tag) {
return new SimpleStringProperty("Zone " + ((ZoneTextInstance) context.getGenerics()[0]).getZone());
}
}
}