【发布时间】:2020-04-06 07:13:46
【问题描述】:
我必须从通过 URL 上传的 pdf 提取数据。 pdf 是 image/.png 格式,因此在使用 tesseract 包时,有几行无法识别。
代码:
library(rvest)
library(dplyr)
library(pdftools)
library(tesseract)
url="https://www.hindustancopper.com/Page/PriceCircular"
links=url %>%
#reading the html of the url
read_html()%>%
#fetching out the nodes and the attributes
html_nodes("#viewTable li:nth-child(1) a") %>% html_attr("href")%>%
#replacing few strings
str_replace("../..",'')
str(links)
#using pdftools to read the pdf
base_url <- 'https://www.hindustancopper.com'
# combine the base url with the event url
event_url <- paste0(base_url, links)
event_url
#since the link has a scan copy and not the pdf itself hence using tesseract package
pdf_convert(event_url,
pages = 1,
dpi = 850,
filenames = "page1.png")
# what does the data look like
text <- ocr("page1.png")
cat(text)
实际输出读取产品列表及其价格:
CONTINUOUS CAST COPPER WIRE ROD 11 MM 44567
CONTINUOUS CAST COPPER WIRE ROD NS 439678
CONTINUOUS CAST COPPER WIRE ROD 16 MM 443056...etc.
预期的输出应该是:
CONTINUOUS CAST COPPER WIRE ROD 11 MM 441567
CATHODE FULL 434122
CONTINUOUS CAST COPPER WIRE ROD NS 439678
CONTINUOUS CAST COPPER WIRE ROD 16 MM 443056...etc
我已经尝试过多次更改 dpi 参数的值,但这并没有太大帮助。 提前致谢!
【问题讨论】:
-
你试过不同的 PSM 吗?
-
PSM 已经内置在这个函数中。我认为所使用的任何函数都没有提供任何声明 psm 的选项。参考以下网址:rdrr.io/github/hansthompson/pdfHarvester/src/R/Tesseract.R
-
您需要能够尝试另一种页面分割模式,因为它可以捕获当前 PSM 遗漏的区域。我不明白为什么它被固定为 -psm 7,它将图像视为单个文本行,这对于多行文本图像来说效果不佳。 github.com/tesseract-ocr/tesseract/blob/master/doc/…
标签: image-processing ocr tesseract pdftools propensity-score-matching