iiif_google_cv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 56583a675818e08d06e3cf369e3e29e83ec5c6a6
4
+ data.tar.gz: 47aa2262753fb7acf577f0bc7b0cbf919c5cd3f0
5
+ SHA512:
6
+ metadata.gz: 1f238e860c00105020019885b17802dd1d9aac36f35daa40a201d305ebb619f62487c8d4d41cf10f16a0edd075a1bf682647c24c23a5d63381df43abdd2c37b2
7
+ data.tar.gz: 671fd6463563fbb00f89fed6902e0b305aea8c61c1d565a728ed88742c6d2119c9a48e716bbabf6494da42088278a8fc90348d4c406ce50b54a834ecf29f3794
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in iiif_google_cv.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jack Reed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ # IiifGoogleCv
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/iiif_google_cv`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'iiif_google_cv'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install iiif_google_cv
22
+
23
+ ## Usage
24
+
25
+ Activate and authorize an interactive session using [Google Cloud Vision Authentication](http://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-vision/v0.27.0/guides/authentication#withgooglecloudruby) and project id
26
+
27
+ ```sh
28
+ $ VISION_PROJECT="my-project-id" VISION_KEYFILE=Project\ Key\ File.json bin/console
29
+ ```
30
+
31
+ Create a new `IiifGoogleCv::Client`
32
+
33
+ ```ruby
34
+ > c = IiifGoogleCv::Client.new(manifest_url: 'https://purl.stanford.edu/hg676jb4964/iiif/manifest')
35
+ ```
36
+
37
+ Create a manifest with annotations from the client
38
+
39
+ ```ruby
40
+ > c.iiif_manifest_with_annotations
41
+ Annotating ["https://stacks.stanford.edu/image/iiif/hg676jb4964%2F0380_796-44/full/pct:50/0/default.jpg"]
42
+ => "https://api.myjson.com/bins/12uue9"
43
+ ```
44
+
45
+ The returned url is now an annotated manifest of the images and can be viewed in a IIIF viewer like [Mirador](http://projectmirador.org/demo/).
46
+
47
+ The API calls will fail if the images are too big or do not respond in time. You can modify the responding images from the IIIF Image API by changing the `SCALE_FACTOR` environment variable. This defaults to `0.5` (50% of original size).
48
+
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mejackreed/iiif_google_cv.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "iiif_google_cv"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iiif_google_cv/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'iiif_google_cv'
8
+ spec.version = IiifGoogleCv::VERSION
9
+ spec.authors = ['Jack Reed']
10
+ spec.email = ['phillipjreed@gmail.com']
11
+
12
+ spec.summary = 'IIIF + Google Cloud Vision API'
13
+ spec.description = 'IIIF + Google Cloud Vision API'
14
+ spec.homepage = 'https://github.com/mejackreed/iiif_google_cv'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'google-cloud-vision'
25
+ spec.add_dependency 'iiif-presentation'
26
+ spec.add_dependency 'putson'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.15'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
@@ -0,0 +1,6 @@
1
+ require 'iiif_google_cv/version'
2
+ require 'iiif_google_cv/client'
3
+
4
+ module IiifGoogleCv
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,67 @@
1
+ require 'iiif_google_cv/bounding_box'
2
+
3
+ module IiifGoogleCv
4
+ class Annotations
5
+ attr_reader :client
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def url
12
+ putson.url || putson.post({})
13
+ end
14
+
15
+ def putson
16
+ @putson ||= Putson::Client.new
17
+ end
18
+
19
+ def iiif_annotations(idx)
20
+ anno_list = IIIF::Presentation::AnnotationList.new(
21
+ '@context' => 'http://iiif.io/api/presentation/2/context.json',
22
+ '@id' => url
23
+ )
24
+ anno_list.resources = (faces_to_resources(idx) << text_to_resources(idx)).flatten.compact
25
+ putson.put(anno_list.to_json)
26
+ url
27
+ end
28
+
29
+ def text_to_resources(idx)
30
+ client.annotations[idx].text.words.map do |word|
31
+ xywh = IiifGoogleCv::BoundingBox.from_gcv_a(word.bounds, client.class::SCALE_FACTOR).to_xywh
32
+ IIIF::Presentation::Resource.new(
33
+ '@id' => SecureRandom.hex,
34
+ '@type' => 'oa:Annotation',
35
+ 'motivation' => 'sc:painting',
36
+ 'resource' => {
37
+ '@id' => SecureRandom.hex,
38
+ '@type' => 'cnt:ContentAsText',
39
+ 'format' => 'text/plain',
40
+ 'chars' => word.text,
41
+ 'language' => 'eng'
42
+ },
43
+ 'on' => "#{client.canvas_id(idx)}#xywh=#{xywh}"
44
+ )
45
+ end if client.annotations[idx].text?
46
+ end
47
+
48
+ def faces_to_resources(idx)
49
+ client.annotations[idx].faces.map do |face|
50
+ xywh = IiifGoogleCv::BoundingBox.from_gcv_a(face.bounds.head, client.class::SCALE_FACTOR).to_xywh
51
+ IIIF::Presentation::Resource.new(
52
+ '@id' => SecureRandom.hex,
53
+ '@type' => 'oa:Annotation',
54
+ 'motivation' => 'sc:painting',
55
+ 'resource' => {
56
+ '@id' => SecureRandom.hex,
57
+ '@type' => 'cnt:ContentAsText',
58
+ 'format' => 'text/plain',
59
+ 'chars' => 'Face detection',
60
+ 'language' => 'eng'
61
+ },
62
+ 'on' => "#{client.canvas_id(idx)}#xywh=#{xywh}"
63
+ )
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,42 @@
1
+ module IiifGoogleCv
2
+ ##
3
+ # Basic BoundingBox operations/parsing
4
+ class BoundingBox
5
+ attr_reader :minx, :miny, :maxx, :maxy, :scale_factor
6
+
7
+ def initialize(minx, miny, maxx, maxy, scale_factor: 1.0)
8
+ @minx = minx
9
+ @miny = miny
10
+ @maxx = maxx
11
+ @maxy = maxy
12
+ @scale_factor = scale_factor
13
+ end
14
+
15
+ def self.from_gcv_a(bounds_array, scale_factor = 1.0)
16
+ i = new(999_999_999, 999_999_999, -999_999_999, -999_999_999, scale_factor: scale_factor)
17
+ bounds_array.each do |vertex|
18
+ i.check_and_set_bounds(vertex)
19
+ end
20
+ i
21
+ end
22
+
23
+ def check_and_set_bounds(vertex)
24
+ @maxx = vertex.x / scale_factor if vertex.x / scale_factor > maxx
25
+ @maxy = vertex.y / scale_factor if vertex.y / scale_factor > maxy
26
+ @minx = vertex.x / scale_factor if vertex.x / scale_factor < minx
27
+ @miny = vertex.y / scale_factor if vertex.y / scale_factor < miny
28
+ end
29
+
30
+ def width
31
+ maxx - minx
32
+ end
33
+
34
+ def height
35
+ maxy - miny
36
+ end
37
+
38
+ def to_xywh
39
+ "#{minx.to_i},#{miny.to_i},#{width.to_i},#{height.to_i}"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,99 @@
1
+ require 'faraday'
2
+ require 'iiif/presentation'
3
+ require 'google/cloud/vision'
4
+ require 'putson'
5
+
6
+ require 'iiif_google_cv/annotations'
7
+
8
+ module IiifGoogleCv
9
+ class Client
10
+ SCALE_FACTOR = (ENV['SCALE_FACTOR'] || 0.5).to_f
11
+ attr_reader :manifest, :manifest_url
12
+
13
+ def initialize(manifest_url: nil, manifest: nil)
14
+ unless manifest_url || manifest
15
+ raise(
16
+ ArgumentError,
17
+ 'manifest_url or manifest must be provided'
18
+ )
19
+ end
20
+ @manifest_url = manifest_url
21
+ @manifest = manifest
22
+ end
23
+
24
+ def putson
25
+ @putson ||= Putson::Client.new
26
+ end
27
+
28
+ def url
29
+ putson.url || putson.post({})
30
+ end
31
+
32
+ def vision
33
+ @vision ||= Google::Cloud::Vision.new
34
+ end
35
+
36
+ def annotations
37
+ puts "Annotating #{image_resources}"
38
+ @annotations ||= [*vision.annotate(
39
+ *image_resources,
40
+ faces: true,
41
+ labels: true,
42
+ landmarks: true,
43
+ logos: true,
44
+ text: true
45
+ )]
46
+ end
47
+
48
+ def process
49
+ annotations
50
+ end
51
+
52
+ def image_resources
53
+ iiif_manifest.sequences.collect do |s|
54
+ s.canvases.collect do |c|
55
+ c.images.collect do |i|
56
+ i.resource['@id'].gsub('full/0', "pct:#{(SCALE_FACTOR * 100).to_i}/0")
57
+ end
58
+ end
59
+ end.flatten.compact
60
+ end
61
+
62
+ def canvas_id(idx)
63
+ iiif_manifest.sequences.map do |s|
64
+ s.canvases[idx]['@id']
65
+ end.flatten.compact.first
66
+ end
67
+
68
+ def iiif_manifest
69
+ @iiif_manifest ||= IIIF::Service.parse(manifest_body)
70
+ end
71
+
72
+ ##
73
+ def iiif_manifest_with_annotations
74
+ @iiif_manifest_with_annotations ||= iiif_manifest.clone
75
+ @iiif_manifest_with_annotations['@id'] = url
76
+ @iiif_manifest_with_annotations.sequences.map do |s|
77
+ s.canvases.map.with_index do |c, i|
78
+ c.other_content = [
79
+ {
80
+ '@id' => iiif_annotations(i),
81
+ '@type' => 'sc:AnnotationList'
82
+ }
83
+ ]
84
+ end
85
+ end
86
+ putson.put(@iiif_manifest_with_annotations)
87
+ end
88
+
89
+ def iiif_annotations(i)
90
+ IiifGoogleCv::Annotations.new(self).iiif_annotations(i)
91
+ end
92
+
93
+ def manifest_body
94
+ return manifest if manifest
95
+ @manifest = Faraday.get(manifest_url).body
96
+ @manifest
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,3 @@
1
+ module IiifGoogleCv
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iiif_google_cv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Reed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-vision
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: iiif-presentation
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: putson
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: IIIF + Google Cloud Vision API
98
+ email:
99
+ - phillipjreed@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - iiif_google_cv.gemspec
114
+ - lib/iiif_google_cv.rb
115
+ - lib/iiif_google_cv/annotations.rb
116
+ - lib/iiif_google_cv/bounding_box.rb
117
+ - lib/iiif_google_cv/client.rb
118
+ - lib/iiif_google_cv/version.rb
119
+ homepage: https://github.com/mejackreed/iiif_google_cv
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.6.11
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: IIIF + Google Cloud Vision API
143
+ test_files: []