Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
const assert = require("assert");
 
Feature("OCR scenarios");
 
const createConfig = ({ shapes = ["Rectangle"], textareaProps = "" } = {}) => {
  return `<View>
    <Image name="image" value="$image" zoomcontrol="true"></Image>
    ${shapes
      .map((shapeName) => `<${shapeName} name="image${shapeName}" toName="image"/>`)
      .join(`
    `)}
    <Labels name="imageLabels" toName="image" allowEmpty="true">
        <Label value="Paragraph" background="#4E86C8"/>
        <Label value="Header" background="#944BFF"/>
        <Label value="List" background="#F88B16"/>
    </Labels>
    <TextArea name="ocr" toName="image" editable="true" perRegion="true"
              placeholder="Recognized Text" displayMode="region-list" ${textareaProps} />
</View>`;
};
 
const data = {
  image: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Html_headers.png/640px-Html_headers.png",
};
 
const H3_POINTS = [
  [1.25, 50.887573964497044],
  [21.875, 50.69033530571992],
  [22.03125, 55.226824457593686],
  [1.40625, 55.226824457593686],
];
 
Scenario("Basic scenario", async ({ I, LabelStudio, AtImageView, AtSettings, AtLabels, AtOutliner }) => {
  I.amOnPage("/");
 
  LabelStudio.init({
    config: createConfig({ shapes: ["Polygon"] }),
    data,
    settings: {
      preserveSelectedTool: false,
    },
  });
  LabelStudio.waitForObjectsReady();
  AtSettings.open();
  AtSettings.setGeneralSettings({
    [AtSettings.GENERAL_SETTINGS.AUTO_SELECT_REGION]: true,
    [AtSettings.GENERAL_SETTINGS.SHOW_LABELS]: true,
  });
  AtSettings.close();
  AtLabels.clickLabel("Paragraph");
  const canvasSize = await AtImageView.getCanvasSize();
 
  await AtImageView.lookForStage();
  AtImageView.drawByClickingPoints(
    [...H3_POINTS, H3_POINTS[0]].map(([x, y]) => [(x * canvasSize.width) / 100, (y * canvasSize.height) / 100]),
  );
  AtOutliner.seeRegions(1);
  AtOutliner.seeElement('[placeholder="Recognized Text"]');
  const Text = 'The "H3" header';
 
  I.pressKey("Enter");
  I.seeFocusedElement("input");
  for (const key of 'The "H3" header') {
    I.pressKey(key);
  }
  I.pressKey("Enter");
  const results = await LabelStudio.serialize();
  const hasText = results.find(
    (result) => result && result.value && result.value.text && result.value.text[0] === Text,
  );
 
  assert(hasText, true);
});
 
const REGIONS = [
  {
    label: "Header",
    x: 0.625,
    y: 1.183431952662722,
    width: 34.375,
    height: 5.719921104536489,
    text: 'The "H1" Header',
  },
  {
    label: "Paragraph",
    x: 0.78125,
    y: 10.453648915187376,
    width: 98.4375,
    height: 10.650887573964496,
    text: 'This is a paragraph contained with the "p" tag. This content repeats itself for demonstration purposes. This content repeats itself for demonstration purposes. This content repeats itself for demonstration purposes. This content repeats itself for demonstration purposes.',
  },
  {
    label: "Header",
    x: 0.625,
    y: 23.471400394477318,
    width: 27.1875,
    height: 6.31163708086785,
    text: 'The "H2" Header',
  },
  {
    label: "Paragraph",
    x: 0.3125,
    y: 31.558185404339252,
    width: 62.34375,
    height: 4.339250493096647,
    text: 'This paragraph, also contained with the "p" tag, contains an unordered list:',
  },
  {
    label: "List",
    x: 2.96875,
    y: 37.8698224852071,
    width: 41.5625,
    height: 11.637080867850099,
  },
  {
    label: "Header",
    x: 0.3125,
    y: 50.69033530571992,
    width: 22.65625,
    height: 4.733727810650888,
    text: 'The "H3" Header',
  },
  {
    label: "Paragraph",
    x: 0.625,
    y: 57.790927021696255,
    width: 98.28125,
    height: 11.242603550295858,
    text: "This is a paragraph",
  },
  {
    label: "Header",
    x: 0.15625,
    y: 71.20315581854044,
    width: 19.0625,
    height: 4.339250493096647,
    text: 'The "H4" Header',
  },
  {
    label: "Paragraph",
    x: 0.46875,
    y: 78.10650887573965,
    width: 64.53125,
    height: 4.930966469428008,
    text: "This is a paragraph",
  },
  {
    label: "Header",
    x: 0.625,
    y: 85.20710059171597,
    width: 15.3125,
    height: 4.1420118343195265,
    text: 'The "H5" Header',
  },
  {
    label: "Paragraph",
    x: 0.46875,
    y: 92.11045364891518,
    width: 41.40625,
    height: 4.536489151873767,
  },
];
 
Scenario(
  "Drawing multiple blank regions and then attaching labels",
  async ({ I, LabelStudio, AtImageView, AtSettings, AtLabels, AtOutliner }) => {
    I.amOnPage("/");
    LabelStudio.init({ config: createConfig(), data });
    LabelStudio.waitForObjectsReady();
    AtSettings.open();
    AtSettings.setGeneralSettings({
      [AtSettings.GENERAL_SETTINGS.SHOW_LABELS]: true,
    });
    AtSettings.close();
    AtLabels.clickLabel("blank");
    const canvasSize = await AtImageView.getCanvasSize();
 
    await AtImageView.lookForStage();
    const regions = REGIONS.map((r) => ({
      ...r,
      x: (r.x * canvasSize.width) / 100,
      y: (r.y * canvasSize.height) / 100,
      width: (r.width * canvasSize.width) / 100,
      height: (r.height * canvasSize.height) / 100,
    }));
 
    I.say("Drawing");
    for (const region of regions) {
      AtImageView.drawByDrag(region.x, region.y, region.width, region.height);
    }
    AtOutliner.seeRegions(regions.length);
 
    I.say("Labeling");
    for (const region of Object.values(regions)) {
      AtImageView.dblClickAt(region.x + region.width / 2, region.y + region.height / 2);
      AtLabels.clickLabel(region.label);
      if (region.text) {
        I.fillField(AtOutliner.locateSelectedItem().find(".lsf-textarea-tag__input"), region.text);
      }
    }
    const results = await LabelStudio.serialize();
 
    for (const region of regions) {
      if (region.text) {
        const hasText = results.find(
          (result) => result && result.value && result.value.text && result.value.text[0] === region.text,
        );
 
        assert(hasText, true);
      }
    }
    session("Deserialization", () => {
      I.amOnPage("/");
      LabelStudio.init({ config: createConfig(), data, annotations: [{ id: "test", result: results }] });
      LabelStudio.waitForObjectsReady();
      AtOutliner.seeRegions(regions.length);
      for (const [idx, region] of Object.entries(regions)) {
        if (region.text) {
          I.seeInField(AtOutliner.locateRegionIndex(+idx + 1).find(".lsf-textarea-tag__input"), region.text);
        }
      }
    });
  },
);