@@ -64,11 +64,12 @@ make_hocr <- function(
6464 parsed_sync <- httr :: content(output )
6565
6666 # add metadata
67- langs <- paste(parsed_sync [[" document" ]][[" pages" ]][[1 ]][[" detectedLanguages" ]][[1 ]][[" languageCode" ]])
67+ lang <- parsed_sync [[" document" ]][[" pages" ]][[1 ]][[" detectedLanguages" ]][[1 ]][[" languageCode" ]]
68+ if (is.null(lang )) lang <- " unknown"
6869 n_pages <- length(parsed_sync [[" document" ]][[" pages" ]])
6970 head <- xml2 :: xml_children(doc )[[1 ]]
7071 lang_node <- xml2 :: xml_children(head )[[4 ]]
71- xml2 :: xml_attrs(lang_node )[[2 ]] <- langs
72+ xml2 :: xml_attrs(lang_node )[[2 ]] <- lang
7273 pages_node <- xml2 :: xml_children(head )[[5 ]]
7374 xml2 :: xml_attrs(pages_node )[[2 ]] <- n_pages
7475
@@ -85,11 +86,12 @@ make_hocr <- function(
8586 parsed <- jsonlite :: fromJSON(output )
8687
8788 # add metadata
88- langs <- paste(parsed [[" pages" ]][[" detectedLanguages" ]][[1 ]][[" languageCode" ]], collapse = " " )
89+ lang <- parsed [[" pages" ]][[" detectedLanguages" ]][[1 ]][[" languageCode" ]]
90+ if (is.null(lang )) lang <- " unknown"
8991 n_pages <- nrow(parsed $ pages )
9092 head <- xml2 :: xml_children(doc )[[1 ]]
9193 lang_node <- xml2 :: xml_children(head )[[4 ]]
92- xml2 :: xml_attrs(lang_node )[[2 ]] <- langs
94+ xml2 :: xml_attrs(lang_node )[[2 ]] <- lang
9395 pages_node <- xml2 :: xml_children(head )[[5 ]]
9496 xml2 :: xml_attrs(pages_node )[[2 ]] <- n_pages
9597
@@ -116,21 +118,26 @@ process_page_sync <- function(
116118 page_index ,
117119 pages ,
118120 text
119- ) {
120-
121+ ) {
121122 page <- pages [[page_index ]]
122123 id <- paste0(" page_" , page_index )
123124 x2_page <- page [[" dimension" ]][[" width" ]]
124125 y2_page <- page [[" dimension" ]][[" height" ]]
125126 bbox <- paste0(" bbox 0 0 " , x2_page , " " , y2_page )
126- xml2 :: xml_add_child(body , " div" , class = ' ocr_page' , id = id , title = bbox )
127+ xml2 :: xml_add_child(body , " div" , class = " ocr_page" , id = id , title = bbox )
127128 div1 <- xml2 :: xml_children(body )[[page_index ]]
128-
129+
129130 blocks <- page [[" blocks" ]]
131+
132+ # handle blank pages
133+ if (is.null(blocks ) || length(blocks ) == 0 ) {
134+ return (invisible (NULL ))
135+ }
136+
130137 block_coords <- get_vertices(blocks )
131138 block_coords <- purrr :: map(block_coords , transpose_block )
132139 block_coords <- purrr :: map(block_coords , ~ process_coord(.x , x2_page , y2_page ))
133- block_segments <- purrr :: map(blocks , ~ .x [[" layout" ]][[" textAnchor" ]][[" textSegments" ]][[1 ]])
140+ block_segments <- purrr :: map(blocks , ~ .x [[" layout" ]][[" textAnchor" ]][[" textSegments" ]][[1 ]])
134141 if (is.null(block_segments [[1 ]][[" startIndex" ]])) block_segments [[1 ]][[" startIndex" ]] <- 0
135142
136143 for (j in seq_along(block_coords )) {
@@ -314,18 +321,25 @@ process_page_async <- function(
314321 page_index ,
315322 parsed ,
316323 text
317- ) {
324+ ) {
318325 id <- paste0(" page_" , page_index )
319326 page_coords <- parsed [[" pages" ]][[" layout" ]][[" boundingPoly" ]][[" vertices" ]]
320327 x2_page <- page_coords [[page_index ]][[" x" ]][[3 ]]
321328 y2_page <- page_coords [[page_index ]][[" y" ]][[3 ]]
322329 bbox <- paste0(" bbox 0 0 " , x2_page , " " , y2_page )
323- xml2 :: xml_add_child(body , " div" , class = ' ocr_page' , id = id , title = bbox )
330+ xml2 :: xml_add_child(body , " div" , class = " ocr_page" , id = id , title = bbox )
324331 div1 <- xml2 :: xml_children(body )[[page_index ]]
325-
326- block_coords <- parsed [[" pages" ]][[" blocks" ]][[page_index ]][[" layout" ]][[" boundingPoly" ]][[" normalizedVertices" ]]
327- block_coords <- purrr :: map(block_coords , ~ process_coord(.x , x2_page , y2_page ))
328- block_segments <- parsed [[" pages" ]][[" blocks" ]][[page_index ]][[" layout" ]][[" textAnchor" ]][[" textSegments" ]]
332+
333+ blocks <- parsed [[" pages" ]][[" blocks" ]][[page_index ]]
334+
335+ # handle blank pages
336+ if (is.null(blocks ) || length(blocks ) == 0 ) {
337+ return (invisible (NULL ))
338+ }
339+
340+ block_coords <- blocks [[" layout" ]][[" boundingPoly" ]][[" normalizedVertices" ]]
341+ block_coords <- purrr :: map(block_coords , ~ process_coord(.x , x2_page , y2_page ))
342+ block_segments <- blocks [[" layout" ]][[" textAnchor" ]][[" textSegments" ]]
329343 if (is.null(block_segments [[1 ]][[" startIndex" ]])) block_segments [[1 ]][[" startIndex" ]] <- 0
330344
331345 for (j in seq_along(block_coords )) {
@@ -590,14 +604,12 @@ process_coord <- function(
590604 x2_page ,
591605 y2_page
592606 ) {
593- # handle async format (data frame with x, y columns)
594607 if (is.data.frame(coord )) {
595608 coord [coord < 0 ] <- 0
596- xs <- round(coord [[" x " ]] * x2_page )
597- ys <- round(coord [[" y " ]] * y2_page )
609+ xs <- round(coord [[" xs " ]] * x2_page )
610+ ys <- round(coord [[" ys " ]] * y2_page )
598611 return (list (xs = xs , ys = ys ))
599612 }
600- # handle sync format (list with xs, ys fields)
601613 else {
602614 coord [coord < 0 ] <- 0
603615 coord [[" xs" ]] <- round(coord [[" xs" ]] * x2_page )
0 commit comments