1 | function load_gv_dococr_json(json_file, callback) {
|
---|
2 |
|
---|
3 | var http_assocfilepath = gs.collectionMetadata["httpPath"]+"/index/assoc/"+gs.documentMetadata["assocfilepath"];
|
---|
4 | var json_url = http_assocfilepath +"/"+json_file;
|
---|
5 |
|
---|
6 | $.ajax({
|
---|
7 | method: "GET",
|
---|
8 | url: json_url
|
---|
9 | })
|
---|
10 | .fail(function(jqXHR,textStatus) {
|
---|
11 | console.error("load_gv_dococr_json(): failed to retrieve url '" + json_url +"'");
|
---|
12 | console.error(textStatus);
|
---|
13 | })
|
---|
14 | .done(function(gv_ocr_json_result) {
|
---|
15 | callback(gv_ocr_json_result);
|
---|
16 |
|
---|
17 | });
|
---|
18 |
|
---|
19 | }
|
---|
20 |
|
---|
21 | function display_gv_ocr_bounding_boxes(gv_ocr_json)
|
---|
22 | {
|
---|
23 | console.log(gv_ocr_json);
|
---|
24 |
|
---|
25 |
|
---|
26 | var full_text_annotation = gv_ocr_json.fullTextAnnotation;
|
---|
27 | var pages = full_text_annotation.pages;
|
---|
28 | var num_pages = pages.length;
|
---|
29 |
|
---|
30 | if (num_pages == 1) {
|
---|
31 | var page = pages[0];
|
---|
32 |
|
---|
33 | var bounding_box_rects = [];
|
---|
34 | var page_x_dim = page.width;
|
---|
35 | var page_y_dim = page.height;
|
---|
36 |
|
---|
37 | var blocks = page.blocks;
|
---|
38 | var num_blocks = blocks.length;
|
---|
39 |
|
---|
40 | for (b=0; b<num_blocks; b++) {
|
---|
41 | var block = blocks[b];
|
---|
42 |
|
---|
43 | var boundingBox = block.boundingBox;
|
---|
44 |
|
---|
45 | var min_x = Number.MAX_SAFE_INTEGER;
|
---|
46 | var min_y = Number.MAX_SAFE_INTEGER;
|
---|
47 | var max_x = Number.MIN_SAFE_INTEGER;
|
---|
48 | var max_y = Number.MIN_SAFE_INTEGER;
|
---|
49 |
|
---|
50 | var vertices = boundingBox.vertices;
|
---|
51 | var num_vertices = vertices.length;
|
---|
52 |
|
---|
53 | for (v=0; v<num_vertices; v++) {
|
---|
54 | var x = vertices[v].x;
|
---|
55 | var y = vertices[v].y;
|
---|
56 |
|
---|
57 | min_x = Math.min(min_x,x);
|
---|
58 | min_y = Math.min(min_y,y);
|
---|
59 | max_x = Math.max(max_x,x);
|
---|
60 | max_y = Math.max(max_y,y);
|
---|
61 | }
|
---|
62 |
|
---|
63 | var x_org = min_x;
|
---|
64 | var y_org = min_y;
|
---|
65 | var x_dim = max_x - min_x +1;
|
---|
66 | var y_dim = max_y - min_y +1;
|
---|
67 |
|
---|
68 | var rect = { "x_org": x_org, "y_org": y_org, "x_dim": x_dim, "y_dim": y_dim};
|
---|
69 | bounding_box_rects.push(rect);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | display_scaled_div_bounding_boxes(bounding_box_rects, page_x_dim,page_y_dim);
|
---|
74 |
|
---|
75 | }
|
---|
76 | else {
|
---|
77 | console.error("display_gv_ocr_bounding_boxes(): incorrect number of pages found.")
|
---|
78 | console.error(" Expected 1 page, found " + num_pages +" page(s)");
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | function display_scaled_div_bounding_boxes(bounding_box_rects,fullsize_x_dim,fullsize_y_dim)
|
---|
85 | {
|
---|
86 | var screen_x_dim = gs.variables.screenImageWidth;
|
---|
87 | var screen_y_dim = gs.variables.screenImageHeight;
|
---|
88 |
|
---|
89 | var scale_x = screen_x_dim / fullsize_x_dim;
|
---|
90 | var scale_y = screen_y_dim / fullsize_y_dim;
|
---|
91 |
|
---|
92 | //console.log("scale x = " + scale_x);
|
---|
93 | //console.log("scale y = " + scale_y);
|
---|
94 |
|
---|
95 | var docID = gs.variables.d;
|
---|
96 | var screen_image_id = "small"+docID;
|
---|
97 |
|
---|
98 | var $boundingbox_overlay = $("<div>")
|
---|
99 | .attr("id","ocr-boundingbox-overlay-"+docID)
|
---|
100 | .attr("class","ocr-boundingbox-overlay");
|
---|
101 |
|
---|
102 | var $screen_div = $('#'+screen_image_id);
|
---|
103 | //var $screen_img = $screen_div.find("img");
|
---|
104 |
|
---|
105 | $screen_div.append($boundingbox_overlay);
|
---|
106 | //$screen_img.append($boundingbox_overlay);
|
---|
107 |
|
---|
108 |
|
---|
109 | var num_bb_rects = bounding_box_rects.length;
|
---|
110 |
|
---|
111 | console.log("Block bounding boxes:")
|
---|
112 |
|
---|
113 | for (r=0; r<num_bb_rects; r++) {
|
---|
114 | var rect = bounding_box_rects[r];
|
---|
115 | console.log(" " + JSON.stringify(rect));
|
---|
116 |
|
---|
117 | var scaled_x_org = rect.x_org * scale_x;
|
---|
118 | var scaled_y_org = rect.y_org * scale_y;
|
---|
119 | var scaled_x_dim = rect.x_dim * scale_x;
|
---|
120 | var scaled_y_dim = rect.y_dim * scale_y;
|
---|
121 |
|
---|
122 | var $boundingbox_div = $("<div>")
|
---|
123 | .attr("class","ocr-boundingbox")
|
---|
124 | .css("left", scaled_x_org)
|
---|
125 | .css("top", scaled_y_org)
|
---|
126 | .css("width", scaled_x_dim)
|
---|
127 | .css("height",scaled_y_dim);
|
---|
128 |
|
---|
129 | $boundingbox_overlay.append($boundingbox_div)
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 |
|
---|
135 | https://stackoverflow.com/questions/68395710/building-a-bounding-box-surrounding-text-in-google-vision-api-to-extract-the-tex
|
---|
136 |
|
---|
137 | def get_text_within(document, x1, y1, x2, y2):
|
---|
138 | text = ""
|
---|
139 | for page in document.pages:
|
---|
140 | for block in page.blocks:
|
---|
141 | for paragraph in block.paragraphs:
|
---|
142 | for word in paragraph.words:
|
---|
143 | for symbol in word.symbols:
|
---|
144 | min_x = min(symbol.bounding_box.vertices[0].x, symbol.bounding_box.vertices[1].x,
|
---|
145 | symbol.bounding_box.vertices[2].x, symbol.bounding_box.vertices[3].x)
|
---|
146 | max_x = max(symbol.bounding_box.vertices[0].x, symbol.bounding_box.vertices[1].x,
|
---|
147 | symbol.bounding_box.vertices[2].x, symbol.bounding_box.vertices[3].x)
|
---|
148 | min_y = min(symbol.bounding_box.vertices[0].y, symbol.bounding_box.vertices[1].y,
|
---|
149 | symbol.bounding_box.vertices[2].y, symbol.bounding_box.vertices[3].y)
|
---|
150 | max_y = max(symbol.bounding_box.vertices[0].y, symbol.bounding_box.vertices[1].y,
|
---|
151 | symbol.bounding_box.vertices[2].y, symbol.bounding_box.vertices[3].y)
|
---|
152 | if (min_x >= x1 and max_x <= x2 and min_y >= y1 and max_y <= y2):
|
---|
153 | text += symbol.text
|
---|
154 | if (symbol.property.detected_break.type == 1 or
|
---|
155 | symbol.property.detected_break.type == 3):
|
---|
156 | text += ' '
|
---|
157 | if (symbol.property.detected_break.type == 2):
|
---|
158 | text += '\t'
|
---|
159 | if (symbol.property.detected_break.type == 5):
|
---|
160 | text += '\n'
|
---|
161 | return text
|
---|
162 |
|
---|
163 | */
|
---|
164 |
|
---|
165 |
|
---|
166 | /*
|
---|
167 |
|
---|
168 | https://stackoverflow.com/questions/57071788/google-vision-api-text-detection-display-words-by-block
|
---|
169 |
|
---|
170 |
|
---|
171 | https://gist.github.com/UBISOFT-1/f00e4d22790f4af378d70b237fa56ca9
|
---|
172 |
|
---|
173 | response = client.text_detection(image=image)
|
---|
174 | # The actual response for the first page of the input file.
|
---|
175 | breaks = vision.enums.TextAnnotation.DetectedBreak.BreakType
|
---|
176 | paragraphs = []
|
---|
177 | lines = []
|
---|
178 | # extract text by block of detection
|
---|
179 | for page in response.full_text_annotation.pages:
|
---|
180 | for block in page.blocks:
|
---|
181 | for paragraph in block.paragraphs:
|
---|
182 | para = ""
|
---|
183 | line = ""
|
---|
184 | suppose = str(paragraph.bounding_box)
|
---|
185 | suppose = suppose.replace('vertices ','')
|
---|
186 | print(suppose)
|
---|
187 | for word in paragraph.words:
|
---|
188 | for symbol in word.symbols:
|
---|
189 | line += symbol.text
|
---|
190 | if symbol.property.detected_break.type == breaks.SPACE:
|
---|
191 | line += ' '
|
---|
192 | if symbol.property.detected_break.type == breaks.EOL_SURE_SPACE:
|
---|
193 | line += ' '
|
---|
194 | lines.append(line)
|
---|
195 | para += line
|
---|
196 | line = ''
|
---|
197 | if symbol.property.detected_break.type == breaks.LINE_BREAK:
|
---|
198 | lines.append(line)
|
---|
199 | para += line
|
---|
200 | line = ''
|
---|
201 | paragraphs.append(para)
|
---|
202 |
|
---|
203 |
|
---|
204 | return "\n".join(paragraphs)
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|
209 | https://blog.searce.com/tips-tricks-for-using-google-vision-api-for-text-detection-2d6d1e0c6361
|
---|
210 |
|
---|
211 | def draw_boxes(image, bounds, color,width=5):
|
---|
212 | draw = ImageDraw.Draw(image)
|
---|
213 | for bound in bounds:
|
---|
214 | draw.line([
|
---|
215 | bound.vertices[0].x, bound.vertices[0].y,
|
---|
216 | bound.vertices[1].x, bound.vertices[1].y,
|
---|
217 | bound.vertices[2].x, bound.vertices[2].y,
|
---|
218 | bound.vertices[3].x, bound.vertices[3].y,
|
---|
219 | bound.vertices[0].x, bound.vertices[0].y],fill=color, width=width)
|
---|
220 | return image
|
---|
221 | def get_document_bounds(response, feature):
|
---|
222 | for i,page in enumerate(document.pages):
|
---|
223 | for block in page.blocks:
|
---|
224 | if feature==FeatureType.BLOCK:
|
---|
225 | bounds.append(block.bounding_box)
|
---|
226 | for paragraph in block.paragraphs:
|
---|
227 | if feature==FeatureType.PARA:
|
---|
228 | bounds.append(paragraph.bounding_box)
|
---|
229 | for word in paragraph.words:
|
---|
230 | for symbol in word.symbols:
|
---|
231 | if (feature == FeatureType.SYMBOL):
|
---|
232 | bounds.append(symbol.bounding_box)
|
---|
233 | if (feature == FeatureType.WORD):
|
---|
234 | bounds.append(word.bounding_box)
|
---|
235 | return bounds
|
---|
236 | bounds=get_document_bounds(response, FeatureType.WORD)
|
---|
237 | draw_boxes(image,bounds, 'yellow')
|
---|
238 |
|
---|
239 | */
|
---|