Skip to content

Add option to generate hOCR output instead of raw text when performing OCR via tesseract #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/docsplit/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def parse_options
opts.on('--[no-]ocr', 'force OCR to be used, or disable OCR') do |o|
@options[:ocr] = o
end
opts.on('--hocr', 'force hOCR output when OCR enabled') do |h|
@options[:hocr] = h
end
opts.on('--no-clean', 'disable cleaning of OCR\'d text') do |c|
@options[:clean] = false
end
Expand Down Expand Up @@ -119,4 +122,4 @@ def parse_options

end

end
end
13 changes: 9 additions & 4 deletions lib/docsplit/text_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,25 @@ def extract_from_ocr(pdf, pages)
tempdir = Dir.mktmpdir
base_path = File.join(@output, @pdf_name)
escaped_pdf = ESCAPE[pdf]
additional_opts = ""
additional_opts += "hocr " if @use_hocr
if pages
pages.each do |page|
tiff = "#{tempdir}/#{@pdf_name}_#{page}.tif"
escaped_tiff = ESCAPE[tiff]
file = "#{base_path}_#{page}"
run "MAGICK_TMPDIR=#{tempdir} OMP_NUM_THREADS=2 gm convert -despeckle +adjoin #{MEMORY_ARGS} #{OCR_FLAGS} #{escaped_pdf}[#{page - 1}] #{escaped_tiff} 2>&1"
run "tesseract #{escaped_tiff} #{ESCAPE[file]} -l #{@language} 2>&1"
run "tesseract #{escaped_tiff} #{ESCAPE[file]} -l #{@language} #{additional_opts} 2>&1"
clean_text(file + '.txt') if @clean_ocr
run "cp #{escaped_tiff} #{base_path}_#{page}.tif" if @use_hocr
FileUtils.remove_entry_secure tiff
end
else
tiff = "#{tempdir}/#{@pdf_name}.tif"
escaped_tiff = ESCAPE[tiff]
run "MAGICK_TMPDIR=#{tempdir} OMP_NUM_THREADS=2 gm convert -despeckle #{MEMORY_ARGS} #{OCR_FLAGS} #{escaped_pdf} #{escaped_tiff} 2>&1"
run "tesseract #{escaped_tiff} #{base_path} -l #{@language} 2>&1"
run "tesseract #{escaped_tiff} #{base_path} -l #{@language} #{additional_opts} 2>&1"
run "cp #{escaped_tiff} #{base_path}.tif" if @use_hocr
clean_text(base_path + '.txt') if @clean_ocr
end
ensure
Expand Down Expand Up @@ -120,11 +124,12 @@ def extract_options(options)
@output = options[:output] || '.'
@pages = options[:pages]
@force_ocr = options[:ocr] == true
@use_hocr = options[:hocr] == true
@forbid_ocr = options[:ocr] == false
@clean_ocr = !(options[:clean] == false)
@clean_ocr = !(options[:clean] == false) && !@use_hocr
@language = options[:language] || 'eng'
end

end

end
end