Repository navigation
Expand file tree
/
Copy pathdraw.R
More file actions
755 lines (612 loc) · 23.6 KB
/
Copy pathdraw.R
File metadata and controls
755 lines (612 loc) · 23.6 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#' Draw block bounding boxes
#'
#' @description Plots the block bounding boxes identified by
#' Document AI (DAI) onto images of the submitted document.
#' Generates an annotated .png file for each page in the original
#' document.
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @param prefix string to be prepended to the output png filename.
#' @param dir path to the desired output directory.
#' @param linecol color of the bounding box line.
#' @param linewd width of the bounding box line.
#' @param fontcol color of the box numbers.
#' @param fontsize size of the box numbers.
#' @return no return value, called for side effects.
#'
#' @details Not vectorized, but documents can be multi-page.
#'
#' @export
#' @examples
#' \dontrun{
#' resp <- dai_sync("page.pdf")
#' draw_blocks(resp)
#'
#' draw_blocks("page.json", type = "async")
#' }
draw_blocks <- function(object,
type = "sync",
prefix = NULL,
dir = getwd(),
linecol = "red",
linewd = 3,
fontcol = "blue",
fontsize = 4
) {
# checks
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (length(prefix) > 1 || is.numeric(prefix)) {
stop("Invalid prefix parameter.")
}
if (length(dir) > 1 || !(is.character(dir))) {
stop("Invalid dir parameter. Must be a valid folder path.")
}
if (!(length(linecol) == 1) || !(daiR::is_colour(linecol)) || is.na(linecol)) {
stop("Invalid linecol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(linewd) && length(linewd) == 1) || linewd <= 0) {
stop("Invalid linewd parameter. Must be a single positive number greater than 0.")
}
if (!(length(fontcol) == 1) || !(daiR::is_colour(fontcol)) || is.na(fontcol)) {
stop("Invalid fontcol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(fontsize) && length(fontsize) == 1) || fontsize <= 0) {
stop("Invalid fontsize parameter. Must be a single positive number greater than 0.")
}
dir <- normalizePath(dir, winslash = "/")
if (type == "sync") {
if (!(inherits(object, "response"))) {
stop("Object parameter not pointing to valid response object.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- httr::content(object)
pages <- parsed$document$pages
pages_blocks <- purrr::map(pages, ~.x$blocks)
pagewise_block_sets <- purrr::map(pages_blocks, get_vertices)
pagewise_block_sets <- purrr::map(pagewise_block_sets, transpose_page)
# decode base64 and save to temp images
page_imgs_base64 <- purrr::map_chr(pages, ~.x$image$content)
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
} else if (type == "async") {
if (!(is_json(object))) {
stop("Object parameter not pointing to valid JSON file.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- jsonlite::fromJSON(object)
pages_blocks <- parsed$pages$blocks
pagewise_block_sets <- purrr::map(pages_blocks, ~.x$layout$boundingPoly$normalizedVertices)
# decode base64 and save to temp images
page_imgs_base64 <- parsed$pages$image$content
if (is.null(page_imgs_base64) || any(is.na(page_imgs_base64))) {
stop(
"JSON file does not contain base64-encoded images. ",
"Drawing functions require files with image data."
)
}
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
}
# Plot bounding boxes
purrr::map2(pagewise_block_sets, seq_along(pagewise_block_sets), ~ process_image(.x, .y, imgs, type, object, prefix, dir, filename, dest, linecol, linewd, fontcol, fontsize, boxtype = "blocks"))
cli::cli_alert_success(glue::glue("Generated {length(pages_blocks)} image(s) with block bounding boxes."))
invisible(NULL)
}
#' Draw paragraph bounding boxes
#'
#' @description Plots the paragraph bounding boxes identified by
#' Document AI (DAI) onto images of the submitted document.
#' Generates an annotated .png file for each page in the original
#' document.
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @param prefix string to be prepended to the output png filename.
#' @param dir path to the desired output directory.
#' @param linecol color of the bounding box line.
#' @param linewd width of the bounding box line.
#' @param fontcol color of the box numbers.
#' @param fontsize size of the box numbers.
#' @return no return value, called for side effects.
#'
#' @details Not vectorized, but documents can be multi-page.
#'
#' @export
#' @examples
#' \dontrun{
#' resp <- dai_sync("page.pdf")
#' draw_paragraphs(resp)
#'
#' draw_paragraphs("page.json", type = "async")
#' }
draw_paragraphs <- function(
object,
type = "sync",
prefix = NULL,
dir = getwd(),
linecol = "red",
linewd = 3,
fontcol = "blue",
fontsize = 4
) {
# checks
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (length(prefix) > 1 || is.numeric(prefix)) {
stop("Invalid prefix parameter.")
}
if (length(dir) > 1 || !(is.character(dir))) {
stop("Invalid dir parameter. Must be a valid folder path.")
}
if (!(length(linecol) == 1) || !(daiR::is_colour(linecol)) || is.na(linecol)) {
stop("Invalid linecol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(linewd) && length(linewd) == 1) || linewd <= 0) {
stop("Invalid linewd parameter. Must be a single positive number greater than 0.")
}
if (!(length(fontcol) == 1) || !(daiR::is_colour(fontcol)) || is.na(fontcol)) {
stop("Invalid fontcol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(fontsize) && length(fontsize) == 1) || fontsize <= 0) {
stop("Invalid fontsize parameter. Must be a single positive number greater than 0.")
}
dir <- normalizePath(dir, winslash = "/")
if (type == "sync") {
if (!(inherits(object, "response"))) {
stop("Object parameter not pointing to valid response object.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- httr::content(object)
pages <- parsed$document$pages
pages_paragraphs <- purrr::map(pages, ~.x$paragraphs)
pagewise_block_sets <- purrr::map(pages_paragraphs, get_vertices)
pagewise_block_sets <- purrr::map(pagewise_block_sets, transpose_page)
# decode base64 and save to temp images
page_imgs_base64 <- purrr::map_chr(pages, ~.x$image$content)
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
} else if (type == "async") {
if (!(is_json(object))) {
stop("Object parameter not pointing to valid JSON file.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- jsonlite::fromJSON(object)
pages_paragraphs <- parsed$pages$paragraphs
pagewise_block_sets <- purrr::map(pages_paragraphs, ~.x$layout$boundingPoly$normalizedVertices)
# decode base64 and save to temp images
page_imgs_base64 <- parsed$pages$image$content
if (is.null(page_imgs_base64) || any(is.na(page_imgs_base64))) {
stop(
"JSON file does not contain base64-encoded images. ",
"Drawing functions require files with image data."
)
}
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
}
# Plot bounding boxes
purrr::map2(pagewise_block_sets, seq_along(pagewise_block_sets), ~ process_image(.x, .y, imgs, type, object, prefix, dir, filename, dest, linecol, linewd, fontcol, fontsize, boxtype = "paragraphs"))
cli::cli_alert_success(glue::glue("Generated {length(pages_paragraphs)} image(s) with paragraph bounding boxes."))
invisible(NULL)
}
#' Draw line bounding boxes
#'
#' @description Plots the line bounding boxes identified by
#' Document AI (DAI) onto images of the submitted document.
#' Generates an annotated .png file for each page in the original
#' document.
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @param prefix string to be prepended to the output png filename.
#' @param dir path to the desired output directory.
#' @param linecol color of the bounding box line.
#' @param linewd width of the bounding box line.
#' @param fontcol color of the box numbers.
#' @param fontsize size of the box numbers.
#' @return no return value, called for side effects.
#'
#' @details Not vectorized, but documents can be multi-page.
#'
#' @export
#' @examples
#' \dontrun{
#' resp <- dai_sync("page.pdf")
#' draw_lines(resp)
#'
#' draw_lines("page.json", type = "async")
#' }
draw_lines <- function(
object,
type = "sync",
prefix = NULL,
dir = getwd(),
linecol = "red",
linewd = 3,
fontcol = "blue",
fontsize = 4
) {
# checks
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (length(prefix) > 1 || is.numeric(prefix)) {
stop("Invalid prefix parameter.")
}
if (length(dir) > 1 || !(is.character(dir))) {
stop("Invalid dir parameter. Must be a valid folder path.")
}
if (!(length(linecol) == 1) || !(daiR::is_colour(linecol)) || is.na(linecol)) {
stop("Invalid linecol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(linewd) && length(linewd) == 1) || linewd <= 0) {
stop("Invalid linewd parameter. Must be a single positive number greater than 0.")
}
if (!(length(fontcol) == 1) || !(daiR::is_colour(fontcol)) || is.na(fontcol)) {
stop("Invalid fontcol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(fontsize) && length(fontsize) == 1) || fontsize <= 0) {
stop("Invalid fontsize parameter. Must be a single positive number greater than 0.")
}
dir <- normalizePath(dir, winslash = "/")
if (type == "sync") {
if (!(inherits(object, "response"))) {
stop("Object parameter not pointing to valid response object.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- httr::content(object)
pages <- parsed$document$pages
pages_lines <- purrr::map(pages, ~.x$lines)
pagewise_block_sets <- purrr::map(pages_lines, get_vertices)
pagewise_block_sets <- purrr::map(pagewise_block_sets, transpose_page)
# decode base64 and save to temp images
page_imgs_base64 <- purrr::map_chr(pages, ~.x$image$content)
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
} else if (type == "async") {
if (!(is_json(object))) {
stop("Object parameter not pointing to valid JSON file.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- jsonlite::fromJSON(object)
pages_lines <- parsed$pages$lines
pagewise_block_sets <- purrr::map(pages_lines, ~.x$layout$boundingPoly$normalizedVertices)
# decode base64 and save to temp images
page_imgs_base64 <- parsed$pages$image$content
if (is.null(page_imgs_base64) || any(is.na(page_imgs_base64))) {
stop(
"JSON file does not contain base64-encoded images. ",
"Drawing functions require files with image data."
)
}
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
}
# Plot bounding boxes
purrr::map2(pagewise_block_sets, seq_along(pagewise_block_sets), ~ process_image(.x, .y, imgs, type, object, prefix, dir, filename, dest, linecol, linewd, fontcol, fontsize, boxtype = "lines"))
cli::cli_alert_success(glue::glue("Generated {length(pages_lines)} image(s) with line bounding boxes."))
invisible(NULL)
}
#' Draw token bounding boxes
#'
#' @description Plots the token (i.e., word) bounding boxes identified
#' by Document AI (DAI) onto images of the submitted document.
#' Generates an annotated .png file for each page in the original
#' document.
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @param prefix string to be prepended to the output png filename.
#' @param dir path to the desired output directory.
#' @param linecol color of the bounding box line.
#' @param linewd width of the bounding box line.
#' @param fontcol color of the box numbers.
#' @param fontsize size of the box numbers.
#' @return no return value, called for side effects.
#'
#' @details Not vectorized, but documents can be multi-page.
#'
#' @export
#' @examples
#' \dontrun{
#' resp <- dai_sync("page.pdf")
#' draw_tokens(resp)
#'
#' draw_tokens("page.json", type = "async")
#'}
draw_tokens <- function(
object,
type = "sync",
prefix = NULL,
dir = getwd(),
linecol = "red",
linewd = 3,
fontcol = "blue",
fontsize = 4
) {
# checks
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (length(prefix) > 1 || is.numeric(prefix)) {
stop("Invalid prefix parameter.")
}
if (length(dir) > 1 || !(is.character(dir))) {
stop("Invalid dir parameter. Must be a valid folder path.")
}
if (!(length(linecol) == 1) || !(daiR::is_colour(linecol)) || is.na(linecol)) {
stop("Invalid linecol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(linewd) && length(linewd) == 1) || linewd <= 0) {
stop("Invalid linewd parameter. Must be a single positive number greater than 0.")
}
if (!(length(fontcol) == 1) || !(daiR::is_colour(fontcol)) || is.na(fontcol)) {
stop("Invalid fontcol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(fontsize) && length(fontsize) == 1) || fontsize <= 0) {
stop("Invalid fontsize parameter. Must be a single positive number greater than 0.")
}
dir <- normalizePath(dir, winslash = "/")
if (type == "sync") {
if (!(inherits(object, "response"))) {
stop("Object parameter not pointing to valid response object.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- httr::content(object)
pages <- parsed$document$pages
pages_tokens <- purrr::map(pages, ~.x$tokens)
pagewise_block_sets <- purrr::map(pages_tokens, get_vertices)
pagewise_block_sets <- purrr::map(pagewise_block_sets, transpose_page)
# decode base64 and save to temp images
page_imgs_base64 <- purrr::map_chr(pages, ~.x$image$content)
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
} else if (type == "async") {
if (!(is_json(object))) {
stop("Object parameter not pointing to valid JSON file.")
}
# extract a list with pagewise sets of block boundary boxes
parsed <- jsonlite::fromJSON(object)
pages_tokens <- parsed$pages$tokens
pagewise_block_sets <- purrr::map(pages_tokens, ~.x$layout$boundingPoly$normalizedVertices)
# decode base64 and save to temp images
page_imgs_base64 <- parsed$pages$image$content
if (is.null(page_imgs_base64) || any(is.na(page_imgs_base64))) {
stop(
"JSON file does not contain base64-encoded images. ",
"Drawing functions require files with image data."
)
}
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
}
# Plot bounding boxes
purrr::map2(pagewise_block_sets, seq_along(pagewise_block_sets), ~ process_image(.x, .y, imgs, type, object, prefix, dir, filename, dest, linecol, linewd, fontcol, fontsize, boxtype = "tokens"))
cli::cli_alert_success(glue::glue("Generated {length(pages_tokens)} image(s) with token bounding boxes."))
invisible(NULL)
}
#' Draw entity bounding boxes
#'
#' @description Plots the entity bounding boxes identified
#' by a Document AI form parser processor onto images of the
#' submitted document. Generates an annotated .png file for
#' each page in the original document.
#'
#' @param object either a HTTP response object from
#' \code{dai_sync()} or the path to a JSON file from
#' \code{dai_async()}.
#' @param type one of "sync" or "async", depending on
#' the function used to process the original document.
#' @param prefix string to be prepended to the output png filename.
#' @param dir path to the desired output directory.
#' @param linecol color of the bounding box line.
#' @param linewd width of the bounding box line.
#' @param fontcol color of the box numbers.
#' @param fontsize size of the box numbers.
#' @return no return value, called for side effects.
#'
#' @details Not vectorized, but documents can be multi-page.
#'
#' @export
#' @examples
#' \dontrun{
#' resp <- dai_sync("page.pdf")
#' draw_entities(resp)
#'
#' draw_tokens("page.json", type = "async")
#'
#' }
draw_entities <- function(
object,
type = "sync",
prefix = NULL,
dir = getwd(),
linecol = "red",
linewd = 3,
fontcol = "blue",
fontsize = 4
) {
# checks
if (!(length(type) == 1) || !(type %in% c("sync", "async"))) {
stop("Invalid type parameter.")
}
if (length(prefix) > 1 || is.numeric(prefix)) {
stop("Invalid prefix parameter.")
}
if (length(dir) > 1 || !(is.character(dir))) {
stop("Invalid dir parameter. Must be a valid folder path.")
}
if (!(length(linecol) == 1) || !(daiR::is_colour(linecol)) || is.na(linecol)) {
stop("Invalid linecol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(linewd) && length(linewd) == 1) || linewd <= 0) {
stop("Invalid linewd parameter. Must be a single positive number greater than 0.")
}
if (!(length(fontcol) == 1) || !(daiR::is_colour(fontcol)) || is.na(fontcol)) {
stop("Invalid fontcol parameter. Must be a single valid colour representation.")
}
if (!(is.numeric(fontsize) && length(fontsize) == 1) || fontsize <= 0) {
stop("Invalid fontsize parameter. Must be a single positive number greater than 0.")
}
dir <- normalizePath(dir, winslash = "/")
if (type == "sync") {
if (!(inherits(object, "response"))) {
stop("Object parameter not pointing to valid response object.")
}
# extract a list with pagewise sets of entity boundary boxes
parsed <- httr::content(object)
entities <- parsed$document$entities # each item is a page
pages_entities <- purrr::map(entities, ~.x$properties)
pagewise_entities_sets <- purrr::map(pages_entities, get_vertices_entities)
pagewise_entities_sets <- purrr::map(pagewise_entities_sets, transpose_page)
# decode base64 and save to temp images
pages <- parsed$document$pages
page_imgs_base64 <- purrr::map_chr(pages, ~.x$image$content)
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
} else if (type == "async") {
if (!(is_json(object))) {
stop("Object parameter not pointing to valid JSON file.")
}
# extract a list with pagewise sets of entity boundary boxes
parsed <- jsonlite::fromJSON(object)
pages_entities <- parsed$entities$properties
pagewise_entities_sets <- purrr::map(pages_entities, ~.x$pageAnchor$pageRefs)
pagewise_entities_sets <- purrr::map(pagewise_entities_sets, transpose_page_entities)
# decode base64 and save to temp images
page_imgs_base64 <- parsed$pages$image$content
if (is.null(page_imgs_base64) || any(is.na(page_imgs_base64))) {
stop(
"JSON file does not contain base64-encoded images. ",
"Drawing functions require files with image data."
)
}
imgs <- purrr::map2_chr(page_imgs_base64, seq_along(page_imgs_base64), decode_and_save)
}
# Plot bounding boxes
purrr::map2(pagewise_entities_sets, seq_along(pagewise_entities_sets), ~ process_image(.x, .y, imgs, type, object, prefix, dir, filename, dest, linecol, linewd, fontcol, fontsize, boxtype = "entities"))
cli::cli_alert_success(glue::glue("Generated {length(pages_entities)} image(s) with entity bounding boxes."))
invisible(NULL)
}
#' Decode and save base64 image
#'
#' @description Helper function for decoding base64 images and saving them to temporary files
#'
#' @param base64_string a string with a base64-encoded image.
#' @param index an integer representing the index of the element in the vector of base64-encoded images
#'
#' @noRd
decode_and_save <- function(
base64_string,
index
) {
path <- file.path(tempdir(), glue::glue("page{index}.jpg"))
outconn <- file(path, "wb")
base64enc::base64decode(base64_string, outconn)
close(outconn)
return(path)
}
#' Plot bounding box
#'
#' @description Helper function to plot bounding box on image
#'
#' @param box a dataframe of bounding box coordinates.
#' @param index an integer representing the index of the element in the vector of bounding box dataframes
#' @param info interitance from parent function
#' @param linecol interitance from parent function
#' @param linewd interitance from parent function
#' @param fontcol interitance from parent function
#' @param fontsize interitance from parent function
#'
#' @noRd
plot_box <- function(
box,
index,
info,
linecol,
linewd,
fontcol,
fontsize
) {
if (is.na(box$y[1])) box$y[1] <- 0
if (is.na(box$y[2])) box$y[2] <- 0
if (is.na(box$x[1])) box$x[1] <- 0
if (is.na(box$x[4])) box$x[4] <- 0
box$x1 <- box$x * info$width
box$y1 <- box$y * info$height
graphics::polygon(
x = box$x1,
y = box$y1,
border = linecol,
lwd = linewd
)
graphics::text(
x = box$x1[1],
y = box$y1[1],
label = index,
col = fontcol,
cex = fontsize,
#family = "Liberation Sans"
)
}
#' Process image
#'
#' @description Helper function to save image of page with drawn bounding boxes
#'
#' @param pagewise_block_set a list of dataframes of bounding box coordinates.
#' @param index an integer representing the index of the element in the list of dataframes of bounding box coordinates
#' @param imgs vector of paths to the unannotated images
#' @param type interitance from parent function
#' @param object interitance from parent function
#' @param prefix interitance from parent function
#' @param dir interitance from parent function
#' @param filename interitance from parent function
#' @param dest interitance from parent function
#' @param linecol interitance from parent function
#' @param linewd interitance from parent function
#' @param fontcol interitance from parent function
#' @param fontsize interitance from parent function
#' @param boxtype string with the type of bounding box to be drawn
#'
#' @noRd
process_image <- function(
pagewise_block_set,
index,
imgs,
type,
object,
prefix,
dir,
filename,
dest,
linecol,
linewd,
fontcol,
fontsize,
boxtype
) {
img <- magick::image_read(imgs[index])
info <- magick::image_info(img)
canvas <- magick::image_draw(img)
purrr::map2(pagewise_block_set, seq_along(pagewise_block_set), ~ plot_box(.x, .y, info, linecol, linewd, fontcol, fontsize))
if (type == "async") {
default_prefix <- substr(basename(object), 1, nchar(basename(object)) - 5)
} else {
default_prefix <- "document"
}
if (is.null(prefix)) {
filename <- glue::glue("{default_prefix}_page{index}_{boxtype}.png")
} else {
filename <- glue::glue("{prefix}_page{index}_{boxtype}.png")
}
dest <- file.path(dir, filename)
magick::image_write(canvas, format = "png", dest)
grDevices::dev.off()
}