multitagger 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86e88fd337cbe596e5ca06bb9647668379fa2a09
4
+ data.tar.gz: bd4da909ba31732c3ea47eb975cae94376740495
5
+ SHA512:
6
+ metadata.gz: 21ebe5d9bc3cd80a83e7fcd22fc0a1daee6ee773b20a0108f3496e4454f1539abdeeb24226891fd83d05cb72a18fa8e1149244e1aa669a34d05f02fefaf57715
7
+ data.tar.gz: 35d25c29bffb4b6c85a86297961733e610044a809ffebd34b6f80b457596fb1e1275f253d8cbb30347e1aa9fb630aa31d00165e830dedf7e25df44a28fce2e89
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /config/multitagger.yml
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at dj@danieljanus.pl. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in multitagger.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rebased
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,87 @@
1
+ # Multitagger
2
+
3
+ Multitagger offers a unified API for automatic image tagging services. With Multitagger, you can start developing your image-recognizing app and have
4
+ the comfort of changing your provider without rewriting the code, or even
5
+ use multiple providers at once. See the [blog post] for background.
6
+
7
+ Multitagger currently supports five providers:
8
+
9
+ - [Algorithmia Illustration Tagger]
10
+ - [Clarifai]
11
+ - [Google Cloud Vision]
12
+ - [Imagga]
13
+ - [Microsoft Cognitive Services]
14
+
15
+ [Algorithmia Illustration Tagger]: https://algorithmia.com/algorithms/deeplearning/IllustrationTagger
16
+ [Clarifai]: https://www.clarifai.com/
17
+ [Google Cloud Vision]: https://cloud.google.com/vision/
18
+ [Microsoft Cognitive Services]: https://www.microsoft.com/cognitive-services
19
+ [Imagga]: https://imagga.com
20
+ [blog post]: https://blog.rebased.pl/2016/10/04/computer-vision-1.html
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application’s Gemfile:
25
+
26
+ ```ruby
27
+ gem 'multitagger'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install multitagger
37
+
38
+ ## Usage
39
+
40
+ The simplest way to use Multitagger is to just do:
41
+
42
+ ```ruby
43
+ tags = Multitagger.tag(:google, "/path/to/your/image.jpg")
44
+ ```
45
+
46
+ Where the first argument tells Multitagger to use Google Cloud Vision as the tagging service, and the second designates a local path to your image.
47
+
48
+ This will return an array of `Multitagger::Tag` structures, each of which has `name` (a string) and `prob` (a number from 0.0 to 1.0 representing the probability that this tag fits the image). See the file `multitagger/tag.rb`.
49
+
50
+ To use another provider, just replace the first argument. `Multitagger.providers` will tell you what providers are available.
51
+
52
+ You can also instantiate the tagging service and then call the `tag` method on that:
53
+
54
+ ```ruby
55
+ tagger = Multitagger.tagger(:google)
56
+ tags = tagger.tag("/path/to/your/image.jpg")
57
+ ```
58
+
59
+ ## Configuration
60
+
61
+ Before you can use Multitagger, you must configure it. This is commonly done by putting your service’s credentials in `config/multitagger.yml`.
62
+
63
+ You need obtain the credentials (typically an API key, an app ID, a secret, or a combination of these, depending on the service) separately for each service you plan on using, by registering with the service.
64
+
65
+ You’ll find a sample `multitagger.yml` in the `config/` directory in this repo; substitute the keys as needed.
66
+
67
+ If you need to put this file elsewhere, call this before invoking tagging:
68
+
69
+ ```ruby
70
+ Multitagger::Config.read("/path/to/your/multitagger.yml")
71
+ ```
72
+
73
+ Alternatively, you can pass the configuration hash as a second argument to `Multitagger.tagger`.
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ 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).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rebased/multitagger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "multitagger"
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
@@ -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,13 @@
1
+ google:
2
+ project: my_google_project
3
+ keyfile: my_google_project.json
4
+ microsoft:
5
+ key: my_microsoft_api_key
6
+ clarifai:
7
+ id: my_clarifai_app_id
8
+ secret: my_clarifai_app_secret
9
+ algorithmia:
10
+ key: my_algorithmia_api_key
11
+ imagga:
12
+ key: my_imagga_api_key
13
+ secret: my_imagga_api_secret
@@ -0,0 +1,21 @@
1
+ require "multitagger/version"
2
+ require "multitagger/tag"
3
+ require "multitagger/config"
4
+
5
+ Dir[File.dirname(__FILE__) + '/multitagger/provider/*.rb'].each {|file| require file }
6
+
7
+ module Multitagger
8
+ def self.tagger(provider, config = nil)
9
+ clazz = Provider.const_get(provider.to_s.capitalize)
10
+ config ||= Config.read[provider.to_s]
11
+ clazz.new(config)
12
+ end
13
+
14
+ def self.providers
15
+ Provider.constants.select { |c| Provider.const_get(c).is_a? Class }.map(&:downcase)
16
+ end
17
+
18
+ def self.tag(provider, image)
19
+ self.tagger(provider).tag(image)
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ require 'yaml'
2
+
3
+ module Multitagger
4
+ class Config
5
+ def self.read(path = "config/multitagger.yml")
6
+ @@config ||= YAML.load_file(path)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require 'algorithmia'
2
+
3
+ module Multitagger
4
+ module Provider
5
+ class Algorithmia
6
+ def initialize(config)
7
+ @client = ::Algorithmia.client(config["key"])
8
+ end
9
+
10
+ def tag(image)
11
+ algo = @client.algo("deeplearning/IllustrationTagger/0.2.3")
12
+ image_data = Base64.encode64(File.open(image, "rb").read)
13
+ input = { "image" => "data:image/jpeg;base64,#{image_data}" }
14
+ result = algo.pipe(input).result
15
+ result["general"].map { |x| Tag.new(x.keys.first, x.values.first) }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ require 'clarifai_ruby'
2
+ require 'base64'
3
+
4
+ module ClarifaiRuby
5
+ class MyTagRequest < TagRequest
6
+ def get(image_url_or_path, opts = {})
7
+ if image_url_or_path =~ URI::regexp
8
+ super
9
+ else
10
+ body = {
11
+ encoded_data: File.new(image_url_or_path),
12
+ model: opts[:model]
13
+ }
14
+ build_request!(body, opts)
15
+ @raw_response = @client.post(TAG_PATH, body).parsed_response
16
+ raise RequestError.new @raw_response["status_msg"] if @raw_response["status_code"] != "OK"
17
+ TagResponse.new(raw_response)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ module Multitagger
24
+ module Provider
25
+ class Clarifai
26
+ def initialize(our_config)
27
+ ClarifaiRuby.configure do |config|
28
+ config.client_id = our_config["id"]
29
+ config.client_secret = our_config["secret"]
30
+ end
31
+ end
32
+
33
+ def tag(image)
34
+ response = ClarifaiRuby::MyTagRequest.new.get(image)
35
+ response.tag_images.first.tags.map { |x| Tag.new(x.word, x.prob) }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ require 'google/cloud'
2
+
3
+ module Multitagger
4
+ module Provider
5
+ class Google
6
+ def initialize(config)
7
+ @vision = ::Google::Cloud.vision(config["project"], config["keyfile"])
8
+ end
9
+
10
+ def tag(image)
11
+ img = @vision.image(image)
12
+ img.labels.map { |x| Tag.new(x.description, x.score) }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ require 'rest_client'
2
+
3
+ module Multitagger
4
+ module Provider
5
+ class Imagga
6
+ def initialize(config)
7
+ @key = config["key"]
8
+ @secret = config["secret"]
9
+ end
10
+
11
+ def tag(image)
12
+ auth = 'Basic ' + Base64.strict_encode64("#{@key}:#{@secret}").chomp
13
+ image_response_raw = RestClient.post "https://api.imagga.com/v1/content",
14
+ { image: File.new(image, 'rb') },
15
+ { Authorization: auth }
16
+ image_response = JSON.parse image_response_raw
17
+ image_id = image_response["uploaded"].first["id"]
18
+ tag_response_raw = RestClient.get "https://api.imagga.com/v1/tagging?content=#{image_id}",
19
+ { Authorization: auth }
20
+ tag_response = JSON.parse tag_response_raw
21
+ tags = tag_response["results"].first["tags"].map { |x| [ x["tag"], x["confidence"] ] }
22
+ tags.map { |x| Tag.new(x.first, x.last / tags.first.last) }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'microsoft_computer_vision'
2
+ require 'json'
3
+
4
+ module Multitagger
5
+ module Provider
6
+ class Microsoft
7
+ def initialize(config)
8
+ @client = MicrosoftComputerVision::Client.new(config["key"])
9
+ end
10
+
11
+ def tag(image)
12
+ result = @client.analyze image, {visual_features: "Tags"}
13
+ data = JSON.parse result.body
14
+ data["tags"].map { |x| Tag.new(x["name"], x["confidence"]) }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Multitagger
2
+ Tag = Struct.new(:name, :prob)
3
+ end
@@ -0,0 +1,3 @@
1
+ module Multitagger
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'multitagger'
2
+
3
+ puts Multitagger.tagger(:clarifai).tag("/tmp/cat.jpg")
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multitagger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "multitagger"
8
+ spec.version = Multitagger::VERSION
9
+ spec.authors = ["Daniel Janus"]
10
+ spec.email = ["daniel.janus@rebased.com"]
11
+
12
+ spec.summary = %q{Tag images using multiple computer vision API providers}
13
+ spec.homepage = "https://github.com/rebased/multitagger"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_runtime_dependency "clarifai_ruby", "~> 0.1.2"
24
+ spec.add_runtime_dependency "algorithmia", "~> 0.9.4"
25
+ spec.add_runtime_dependency "rest-client", "~> 2.0.0"
26
+ spec.add_runtime_dependency "google-cloud", "~> 0.20.0"
27
+ spec.add_runtime_dependency "microsoft_computer_vision", "~> 0.2.0"
28
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multitagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Janus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: clarifai_ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: algorithmia
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.4
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.4
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: google-cloud
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.20.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.20.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: microsoft_computer_vision
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.2.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.2.0
111
+ description:
112
+ email:
113
+ - daniel.janus@rebased.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - CODE_OF_CONDUCT.md
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - bin/console
125
+ - bin/setup
126
+ - config/multitagger.yml.example
127
+ - lib/multitagger.rb
128
+ - lib/multitagger/config.rb
129
+ - lib/multitagger/provider/algorithmia.rb
130
+ - lib/multitagger/provider/clarifai.rb
131
+ - lib/multitagger/provider/google.rb
132
+ - lib/multitagger/provider/imagga.rb
133
+ - lib/multitagger/provider/microsoft.rb
134
+ - lib/multitagger/tag.rb
135
+ - lib/multitagger/version.rb
136
+ - lib/test.rb
137
+ - multitagger.gemspec
138
+ homepage: https://github.com/rebased/multitagger
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.5.1
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Tag images using multiple computer vision API providers
162
+ test_files: []