iiif_print 3.2.2 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +4 -0
- data/lib/iiif_print/configuration.rb +9 -0
- data/lib/iiif_print/image_tool.rb +7 -4
- data/lib/iiif_print/persistence_layer/active_fedora_adapter.rb +8 -0
- data/lib/iiif_print/persistence_layer/valkyrie_adapter.rb +23 -0
- data/lib/iiif_print/persistence_layer.rb +4 -0
- data/lib/iiif_print/text_extraction/page_ocr.rb +4 -3
- data/lib/iiif_print/text_extraction_derivative_service.rb +8 -5
- data/lib/iiif_print/text_formats_from_alto_service.rb +11 -4
- data/lib/iiif_print/version.rb +1 -1
- data/lib/iiif_print.rb +1 -0
- metadata +3 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 113eb0be5d5b6783a1c81c4b5cb9ef3c1d907f20ef86cdc24e3c3d53dcc61b15
|
|
4
|
+
data.tar.gz: 40810de9ff823b85a69b849aa6876eb210b68d1a0908d122b44ef2244f45f865
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bdf04fd441bdd4fcf46380ef1f7958b28e284a826aec246c111fd6d4bba7f16db6aad80d1420a986fb8081798112f8befdad44fb4d6fc6338a458eb605d6a7a2
|
|
7
|
+
data.tar.gz: 3501966b7a60712c27995d3cf079510d5e52bbaf1733b944e855c613e506b3cf9d85bf15ffcc599a6366b86fb62ae57c7aa710064e4802be217bfe4da86ebc6c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -198,6 +198,10 @@ IiifPrint.config do |config|
|
|
|
198
198
|
# Add configurable solr field key for searching, default key is: 'human_readable_type_sim' if
|
|
199
199
|
# another key is used, make sure to adjust the config.excluded_model_name_solr_field_values to match
|
|
200
200
|
config.excluded_model_name_solr_field_key = 'some_solr_field_key'
|
|
201
|
+
|
|
202
|
+
# ImageMagick options for the image Tesseract reads, in place of the default one-bit dithered TIFF.
|
|
203
|
+
# A light blur on grayscale keeps halftone photographs from turning into specks Tesseract reads for hours.
|
|
204
|
+
config.ocr_preprocess_options = '-colorspace gray -blur 0x1'
|
|
201
205
|
end
|
|
202
206
|
```
|
|
203
207
|
|
|
@@ -188,6 +188,15 @@ module IiifPrint
|
|
|
188
188
|
@additional_tesseract_options || ""
|
|
189
189
|
end
|
|
190
190
|
|
|
191
|
+
##
|
|
192
|
+
# ImageMagick options for the image Tesseract reads, in place of the
|
|
193
|
+
# default one-bit dithered TIFF. Dithering turns halftone photographs
|
|
194
|
+
# into specks Tesseract can spend hours reading as characters; a light
|
|
195
|
+
# blur on grayscale ("-colorspace gray -blur 0x1") avoids that.
|
|
196
|
+
#
|
|
197
|
+
# @return [String, nil]
|
|
198
|
+
attr_accessor :ocr_preprocess_options
|
|
199
|
+
|
|
191
200
|
attr_writer :uv_config_path
|
|
192
201
|
##
|
|
193
202
|
# According to https://github.com/samvera/hyrax/wiki/Hyrax-Management-Guide#universal-viewer-config
|
|
@@ -29,18 +29,21 @@ module IiifPrint
|
|
|
29
29
|
# format is TIFF.
|
|
30
30
|
# @param destination [String] Path to output / destination file
|
|
31
31
|
# @param monochrome [Boolean] true if monochrome output, otherwise false
|
|
32
|
-
|
|
32
|
+
# @param options [String, nil] ImageMagick options for the output; when
|
|
33
|
+
# given, they replace the monochrome options
|
|
34
|
+
def convert(destination, monochrome = false, options: nil)
|
|
33
35
|
raise 'JP2 output not yet supported' if destination.end_with?('jp2')
|
|
34
|
-
return convert_image(jp2_to_tiff(@path), destination, monochrome) if jp2?
|
|
35
|
-
convert_image(@path, destination, monochrome)
|
|
36
|
+
return convert_image(jp2_to_tiff(@path), destination, monochrome, options) if jp2?
|
|
37
|
+
convert_image(@path, destination, monochrome, options)
|
|
36
38
|
end
|
|
37
39
|
|
|
38
40
|
private
|
|
39
41
|
|
|
40
|
-
def convert_image(source, destination, monochrome)
|
|
42
|
+
def convert_image(source, destination, monochrome, options)
|
|
41
43
|
monochrome &&= destination.slice(-4, 4).index('tif')
|
|
42
44
|
mono_opts = "-depth 1 -monochrome -compress Group4 -type bilevel "
|
|
43
45
|
opts = monochrome ? mono_opts : ''
|
|
46
|
+
opts = "#{options} " if options.present?
|
|
44
47
|
cmd = "convert #{source} #{opts}#{destination} && sync"
|
|
45
48
|
`#{cmd}`
|
|
46
49
|
end
|
|
@@ -181,6 +181,14 @@ module IiifPrint
|
|
|
181
181
|
true
|
|
182
182
|
end
|
|
183
183
|
|
|
184
|
+
##
|
|
185
|
+
# does nothing for ActiveFedora; it keeps no records for extracted text.
|
|
186
|
+
#
|
|
187
|
+
# @return [TrueClass]
|
|
188
|
+
def self.remove_extracted_text_for(*)
|
|
189
|
+
true
|
|
190
|
+
end
|
|
191
|
+
|
|
184
192
|
##
|
|
185
193
|
# Extract text from the derivatives
|
|
186
194
|
#
|
|
@@ -207,6 +207,29 @@ module IiifPrint
|
|
|
207
207
|
Hyrax::ValkyriePersistDerivatives.call(stream, directives)
|
|
208
208
|
end
|
|
209
209
|
|
|
210
|
+
##
|
|
211
|
+
# Remove the fileset's extracted text records, so that re-extracting
|
|
212
|
+
# text replaces them rather than adding to them. The stored files are
|
|
213
|
+
# kept: the new records are written to the same derivative paths.
|
|
214
|
+
#
|
|
215
|
+
# @param [Hyrax::FileSet] a Valkyrie fileset
|
|
216
|
+
# @return [TrueClass, nil] nil when there is nothing to remove
|
|
217
|
+
def self.remove_extracted_text_for(file_set:)
|
|
218
|
+
# re-read: earlier derivative plugins in the same job (e.g. the thumbnail)
|
|
219
|
+
# have saved the file set since the caller loaded it
|
|
220
|
+
file_set = Hyrax.query_service.find_by(id: file_set.id)
|
|
221
|
+
extracted = Hyrax.custom_queries.find_many_file_metadata_by_use(resource: file_set,
|
|
222
|
+
use: Hyrax::FileMetadata::Use.uri_for(use: :extracted_file)).to_a
|
|
223
|
+
return if extracted.empty?
|
|
224
|
+
|
|
225
|
+
# file_ids can hold Strings as well as Valkyrie::IDs, so compare as strings
|
|
226
|
+
extracted_ids = extracted.map { |file_metadata| file_metadata.id.to_s }
|
|
227
|
+
file_set.file_ids = file_set.file_ids.reject { |id| extracted_ids.include?(id.to_s) }
|
|
228
|
+
Hyrax.persister.save(resource: file_set)
|
|
229
|
+
extracted.each { |file_metadata| Hyrax.persister.delete(resource: file_metadata) }
|
|
230
|
+
true
|
|
231
|
+
end
|
|
232
|
+
|
|
210
233
|
##
|
|
211
234
|
# Extract text from the derivatives
|
|
212
235
|
#
|
|
@@ -117,6 +117,10 @@ module IiifPrint
|
|
|
117
117
|
def self.pdf_path_for(file_set:)
|
|
118
118
|
raise NotImplementedError, "#{self}.{__method__}"
|
|
119
119
|
end
|
|
120
|
+
|
|
121
|
+
def self.remove_extracted_text_for(file_set:)
|
|
122
|
+
raise NotImplementedError, "#{self}.{__method__}"
|
|
123
|
+
end
|
|
120
124
|
end
|
|
121
125
|
end
|
|
122
126
|
end
|
|
@@ -75,12 +75,13 @@ module IiifPrint
|
|
|
75
75
|
|
|
76
76
|
private
|
|
77
77
|
|
|
78
|
-
# transform the image into a one-bit TIFF for OCR
|
|
78
|
+
# transform the image into a one-bit TIFF for OCR; a one-bit JP2 is
|
|
79
|
+
# converted too, since Leptonica cannot read JP2 at one bit per sample
|
|
79
80
|
def preprocess_image
|
|
80
81
|
tool = IiifPrint::ImageTool.new(@path)
|
|
81
|
-
return if tool.metadata[:color] == 'monochrome'
|
|
82
|
+
return if tool.metadata[:color] == 'monochrome' && tool.metadata[:content_type] != 'image/jp2'
|
|
82
83
|
intermediate_path = File.join(Dir.mktmpdir, 'monochrome-interim.tif')
|
|
83
|
-
tool.convert(intermediate_path, true)
|
|
84
|
+
tool.convert(intermediate_path, true, options: IiifPrint.config.ocr_preprocess_options)
|
|
84
85
|
@path = intermediate_path
|
|
85
86
|
end
|
|
86
87
|
end
|
|
@@ -28,13 +28,16 @@ module IiifPrint
|
|
|
28
28
|
# TODO: Do we need this source_path instance variable?
|
|
29
29
|
@source_path = filename
|
|
30
30
|
ocr = page_ocr_service_class.new(filename)
|
|
31
|
+
# run all OCR first, so a failed or empty run leaves existing text in place
|
|
32
|
+
contents = ocr_derivatives.transform_values { |method_name| ocr.public_send(method_name) }
|
|
33
|
+
return if contents[:txt].blank?
|
|
31
34
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
+
IiifPrint.remove_extracted_text_for(file_set: file_set)
|
|
36
|
+
|
|
37
|
+
contents.each do |extension, content|
|
|
35
38
|
next if content.blank?
|
|
36
39
|
|
|
37
|
-
write(content: content, path:
|
|
40
|
+
write(content: content, path: prepare_path(extension.to_s), extension: extension)
|
|
38
41
|
end
|
|
39
42
|
end
|
|
40
43
|
|
|
@@ -47,7 +50,7 @@ module IiifPrint
|
|
|
47
50
|
end
|
|
48
51
|
|
|
49
52
|
def cleanup_derivatives(*)
|
|
50
|
-
ocr_derivatives.
|
|
53
|
+
ocr_derivatives.each_key do |extension|
|
|
51
54
|
super(extension.to_s)
|
|
52
55
|
end
|
|
53
56
|
end
|
|
@@ -18,7 +18,7 @@ module IiifPrint
|
|
|
18
18
|
# Write data as UTF-8 encoded text
|
|
19
19
|
File.open(save_path, "w:UTF-8") do |f|
|
|
20
20
|
f.write(data)
|
|
21
|
-
IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url:
|
|
21
|
+
IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: save_path, container: 'extracted_text', mime_type: mime_type })
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -28,6 +28,10 @@ module IiifPrint
|
|
|
28
28
|
!File.size(path).zero?
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
def alto_with_words?(path)
|
|
32
|
+
nonempty_file?(path) && IiifPrint::TextExtraction::AltoReader.new(path).text.present?
|
|
33
|
+
end
|
|
34
|
+
|
|
31
35
|
# if there was no derivative yet, there might be one in-transit from
|
|
32
36
|
# an ingest, so check for that, and use its source if applicable:
|
|
33
37
|
def incoming_alto_path
|
|
@@ -35,16 +39,17 @@ module IiifPrint
|
|
|
35
39
|
fileset_id: @file_set.id,
|
|
36
40
|
destination_name: 'xml'
|
|
37
41
|
).pluck(:path).uniq.first
|
|
38
|
-
path if
|
|
42
|
+
path if alto_with_words?(path)
|
|
39
43
|
end
|
|
40
44
|
|
|
41
45
|
def alto_path
|
|
42
|
-
# check first for existing
|
|
46
|
+
# check first for existing derivative data with words; an ALTO without
|
|
47
|
+
# words (e.g. from a failed OCR run) should not prevent OCR:
|
|
43
48
|
path = derivative_path_factory.derivative_path_for_reference(
|
|
44
49
|
@file_set,
|
|
45
50
|
'xml'
|
|
46
51
|
)
|
|
47
|
-
return path if
|
|
52
|
+
return path if alto_with_words?(path)
|
|
48
53
|
incoming_alto_path
|
|
49
54
|
end
|
|
50
55
|
|
|
@@ -67,6 +72,8 @@ module IiifPrint
|
|
|
67
72
|
width,
|
|
68
73
|
height
|
|
69
74
|
)
|
|
75
|
+
IiifPrint.remove_extracted_text_for(file_set: @file_set)
|
|
76
|
+
save_derivative('xml', source_file)
|
|
70
77
|
save_derivative('json', reader.json)
|
|
71
78
|
save_derivative('txt', reader.text)
|
|
72
79
|
end
|
data/lib/iiif_print/version.rb
CHANGED
data/lib/iiif_print.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: iiif_print
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sean Upton
|
|
@@ -11,10 +11,9 @@ authors:
|
|
|
11
11
|
- Kirk Wang
|
|
12
12
|
- LaRita Robinson
|
|
13
13
|
- Jeremy Friesen
|
|
14
|
-
autorequire:
|
|
15
14
|
bindir: bin
|
|
16
15
|
cert_chain: []
|
|
17
|
-
date:
|
|
16
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
18
17
|
dependencies:
|
|
19
18
|
- !ruby/object:Gem::Dependency
|
|
20
19
|
name: blacklight_iiif_search
|
|
@@ -502,7 +501,6 @@ homepage: https://github.com/notch8/iiif_print/
|
|
|
502
501
|
licenses:
|
|
503
502
|
- Apache-2.0
|
|
504
503
|
metadata: {}
|
|
505
|
-
post_install_message:
|
|
506
504
|
rdoc_options: []
|
|
507
505
|
require_paths:
|
|
508
506
|
- lib
|
|
@@ -517,8 +515,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
517
515
|
- !ruby/object:Gem::Version
|
|
518
516
|
version: '0'
|
|
519
517
|
requirements: []
|
|
520
|
-
rubygems_version: 3.
|
|
521
|
-
signing_key:
|
|
518
|
+
rubygems_version: 3.6.7
|
|
522
519
|
specification_version: 4
|
|
523
520
|
summary: IiifPrint is a gem (Rails "engine") for Hyrax-based digital repository applications
|
|
524
521
|
to support displaying parent/child works in the same viewer (Universal Viewer) and
|