Optimized

This commit is contained in:
Kevin Veen-Birkenbach 2020-12-10 21:48:34 +01:00
parent 1266e2f24a
commit c91929f28e

View File

@ -1,26 +1,30 @@
#!/bin/bash #!/bin/bash
# @see https://faceted.wordpress.com/2010/07/11/how-to-extract-text-from-pdf-files-using-poppler-and-gocr-on-ubuntu/ # @see https://faceted.wordpress.com/2010/07/11/how-to-extract-text-from-pdf-files-using-poppler-and-gocr-on-ubuntu/
# @param $1 Working directory
# @param $2 language
# sudo pacman -Syyu tesseract-data-deu tesseract-data-en tesseract # sudo pacman -Syyu tesseract-data-deu tesseract-data-en tesseract
if [ -z "$1" ] if [ -z "$2" ]
then then
echo "You need to define an working directory" && exit 1; echo "You need to define an working directory" && exit 1;
fi fi
TMP_FOLDER="$(mktemp -d)/" TMP_FOLDER="$(mktemp -d)/" &&
ORIGIN_FOLDER="$1/origin/"; ORIGIN_FOLDER="$1/origin/" &&
OUTPUT_FOLDER="$1/readable/"; OUTPUT_FOLDER="$1/generated/" &&
echo "Cleaning up $OUTPUT_FOLDER..." &&
rm -v "$OUTPUT_FOLDER"* || exit 1;
for origin_file in "$ORIGIN_FOLDER"*.*; do for origin_file in "$ORIGIN_FOLDER"*.*; do
if [ "$(head -c 4 "$origin_file")" = "%PDF" ]; then if [ "$(head -c 4 "$origin_file")" = "%PDF" ]; then
tmp_file="$TMP_FOLDER$(basename "$origin_file")" tmp_file="$TMP_FOLDER$(basename "$origin_file")"
echo "Generating $tmp_file..." echo "Generating $tmp_file..."
pdfimages "$origin_file" "$tmp_file" pdfimages "$origin_file" "$tmp_file"
else else
cp "$origin_file" "$TMP_FOLDER" cp -v "$origin_file" "$TMP_FOLDER"
fi fi
done done
for tesseract_input_file in "$TMP_FOLDER"*.*; do for tesseract_input_file in "$TMP_FOLDER"*.*; do
txt_output_file_without_suffix="$OUTPUT_FOLDER$(basename "$origin_file")"; txt_output_file_without_suffix="$OUTPUT_FOLDER$(basename "$tesseract_input_file")";
echo "Generating $txt_output_file_without_suffix.txt..." echo "Generating $txt_output_file_without_suffix.txt..."
tesseract -l deu "$tesseract_input_file" "$txt_output_file_without_suffix"; tesseract -l "$2" "$tesseract_input_file" "$txt_output_file_without_suffix";
echo "file content:" echo "file content:"
cat "$txt_output_file_without_suffix.txt" cat "$txt_output_file_without_suffix.txt"
done done