Ai
1 Star 0 Fork 0

github_repo/ocr-table

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
extract_text.sh 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
cseas 提交于 2018-11-24 20:30 +08:00 . Update requirements
#!/bin/bash
BPATH=$1 # Path to directory containing PDFs.
OPATH=$2 # Path to output directory.
LANG=$3 # See man tesseract > LANGUAGES
MIN_WORDS=5 # Number of words required to accept pdftotext result.
if [ $(echo "$LANG" | wc -c ) -lt 1 ] # Language defaults to eng.
then
LANG='eng'
fi
# If the output path does not exist, attempt to create it.
if [ ! -d "$OPATH" ]; then
mkdir -p "$OPATH"
fi
for FILEPATH in $BPATH*.pdf; do
# Extracts plain text content from a PDF.
#
# First, attempts to extract embedded text with pdftotext. If that fails,
# converts the PDF to TIFF and attempts to perform OCR with Tesseract.
#
# Path to text file to be created. E.g. ./myfile.txt
OUTFILE=$OPATH$(basename $FILEPATH).txt
touch "$OUTFILE" # The text file will be created regardless of whether
# text is successfully extracted.
# First attempt to use pdftotext to extract embedded text.
echo -n "Attempting pdftotext extraction..."
pdftotext "$FILEPATH" "$OUTFILE"
FILESIZE=$(wc -w < "$OUTFILE")
echo "extracted $FILESIZE words."
# If that fails, try Tesseract.
if [[ $FILESIZE -lt $MIN_WORDS ]]
then
echo -n "Attempting OCR extraction..."
# Use imagemagick to convert the PDF to a high-rest multi-page TIFF.
convert -density 300 "$FILEPATH" -depth 8 -strip -background white \
-alpha off ./temp.tiff > /dev/null 2>&1
# Then use Tesseract to perform OCR on the tiff.
tesseract ./temp.tiff "$OUTFILE" -l $LANG > /dev/null 2>&1
# We don't need then intermediate TIFF file, so discard it.
rm ./temp.tiff
FILESIZE=$(wc -w < "$OUTFILE")
echo "extracted $FILESIZE words."
fi
done
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/github_repo/ocr-table.git
git@gitee.com:github_repo/ocr-table.git
github_repo
ocr-table
ocr-table
master

搜索帮助