cognitive_vision 0.3.1 → 0.4.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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDZjNjEzMmIyMDI0Y2M3OWY3ODAyMGZiZGFiMzVlYTdkMTk1ZGRhOQ==
4
+ ZDJhN2Y1MzljZTBjYWEwMmY0Njc5MjNhM2EwM2FlYjYxNzU1MTdhNw==
5
5
  data.tar.gz: !binary |-
6
- N2U1YTY0M2NmOTNmNTllNTM5MjBiZjA2OTljYzc3MDRhMGFhMjg0Zg==
6
+ NmEzNmNjZGYxNTQzMWRkMmE3OTgzNjc2NDFjY2VjYzRlNTFhMTA5ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NWVkZWFlN2M3YTMzMTFkMWU2ODZlMmQyMmEwMDRmNjFlOWRhNjEzMDhlMTcy
10
- NWJkNDM1MmZiMTVhZmEzODU4YTRjMzFiZTI1NmI5MWM4ODlmNmFjNDkzZTE1
11
- NmIyYWU5NTE1YmJkNmQ5NTQ4Mjc2YmQ0MDBlM2M1ODhlMjJhYmU=
9
+ MzU0MmI5NDE2MDI1Mjc1M2ZkYTcwMjYyZmUzMmE0OTcxNjAxZWI1ZTgzZjcw
10
+ YWYwNDdmNmRhNDI1MjBjN2QyMzdkMGMyMDUxNGViZThlNWVhMjVmMDgzNTcx
11
+ ZTVhZDBlNWE3YTQ5MGRhMDU0OWMxMTIzZWQ4OTI1YWI2ZmU5ZmM=
12
12
  data.tar.gz: !binary |-
13
- YTk3MGIxZThlMTIxYzBhZDVhY2E3ZDkwZjQ0ZWQ3MjU3ODE4YmFlMjQxZTg0
14
- MzI1OWE2ZGJkOWI2ZWYyMjA2MWE5OGE5YzMyN2RmMmIzOTFhNWVlZGVhOTU3
15
- ODY0ODkwMzA2MzhmMWM4MTFlOTQzODNkMjIwYjJiZTkyNmQzZmQ=
13
+ NTE0ODRkZmUyODIwMTM2ZWE5Yjc4MjY1NmZmYmQyZWJiYmQ2NDQ1ZDkwNGEx
14
+ MzhmMTgxMTRlYWRiYTlmMjM4Njg2YjRkZjI2YWFmYTE1MmU1M2ZmZWFkY2Jl
15
+ NTIxY2JkZmFlZTUzOWMxYjU4MjkzNzZjMzBlMTgwYzk4OTkwMzM=
@@ -8,13 +8,13 @@ module CognitiveVision
8
8
  class UnknownError < StandardError; end
9
9
 
10
10
  def self.analyze_image(image_url, types = [:faces])
11
- types = [types] unless types.is_a?(Array)
11
+ features = ImageFeatures.new(types)
12
12
  body = { 'url' => image_url }
13
- params = { 'visualFeatures' => types.join(',') }
13
+ params = { 'visualFeatures' => features.features_string }
14
14
  response = Connection.new.post('/analyze', params, body)
15
15
 
16
16
  treat_errors(response) if response.code != 200
17
- AnalyzeResponse.parse(response.body)
17
+ AnalyzeResponse.parse(response.body, features)
18
18
  end
19
19
 
20
20
  def self.treat_errors(response)
@@ -1,21 +1,20 @@
1
1
  module CognitiveVision
2
2
  class AnalyzeResponse
3
- attr_reader :adult, :faces
3
+ attr_reader :adult, :categories, :description, :faces, :tags
4
4
 
5
5
  def initialize(options = {})
6
- @adult = options.fetch(:adult, nil)
7
- @faces = options.fetch(:faces, [])
6
+ @adult = options.fetch(:adult, nil)
7
+ @categories = options.fetch(:categories, [])
8
+ @description = options.fetch(:description, nil)
9
+ @faces = options.fetch(:faces, [])
10
+ @tags = options.fetch(:tags, [])
8
11
  end
9
12
 
10
- def self.parse(response_hash)
11
- faces = (response_hash['faces'] || []).map { |face| Face.new(gender: face['gender'], age: face['age']) }
12
- adult = if response_hash['adult']
13
- adult_response = response_hash['adult']
14
- Adult.new(adult_content: adult_response['isAdultContent'],
15
- racy_content: adult_response['isRacyContent'], adult_score: adult_response['adultScore'],
16
- racy_score: adult_response['racyScore'])
17
- end
18
- new(faces: faces, adult: adult)
13
+ def self.parse(response_hash, features)
14
+ parsed = features.analyzers.map do |analyzer|
15
+ [analyzer.key.to_sym, analyzer.parse(response_hash)]
16
+ end
17
+ new(Hash[parsed])
19
18
  end
20
19
  end
21
20
  end
@@ -0,0 +1,10 @@
1
+ module CognitiveVision
2
+ class Category
3
+ attr_reader :name, :score
4
+
5
+ def initialize(options = {})
6
+ @name = options.fetch(:name)
7
+ @score = options.fetch(:score)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module CognitiveVision
2
+ class Description
3
+ attr_reader :captions, :tags
4
+
5
+ def initialize(options = {})
6
+ @captions = options.fetch(:captions, [])
7
+ @tags = options.fetch(:tags, [])
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module CognitiveVision
2
+ class DescriptionCaption
3
+ attr_reader :confidence, :text
4
+
5
+ def initialize(options = {})
6
+ @confidence = options.fetch(:confidence)
7
+ @text = options.fetch(:text)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module CognitiveVision
2
+ module ImageFeature
3
+ class Adult
4
+ def key
5
+ 'adult'
6
+ end
7
+
8
+ def parse(response)
9
+ adult_response = response[key]
10
+ CognitiveVision::Adult.new(adult_content: adult_response['isAdultContent'],
11
+ adult_score: adult_response['adultScore'], racy_score: adult_response['racyScore'],
12
+ racy_content: adult_response['isRacyContent'])
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module CognitiveVision
2
+ module ImageFeature
3
+ class Category
4
+ def key
5
+ 'categories'
6
+ end
7
+
8
+ def parse(response)
9
+ response[key].map { |category| CognitiveVision::Category.new(name: category['name'], score: category['score']) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module CognitiveVision
2
+ module ImageFeature
3
+ class Description
4
+ def key
5
+ 'description'
6
+ end
7
+
8
+ def parse(response)
9
+ description_response = response[key]
10
+ captions = description_response['captions'].map do |caption|
11
+ CognitiveVision::DescriptionCaption.new(text: caption['text'], confidence: caption['confidence'])
12
+ end
13
+ CognitiveVision::Description.new(tags: description_response['tags'], captions: captions)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module CognitiveVision
2
+ module ImageFeature
3
+ class Face
4
+ def key
5
+ 'faces'
6
+ end
7
+
8
+ def parse(response)
9
+ response[key].map { |face| CognitiveVision::Face.new(gender: face['gender'], age: face['age']) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module CognitiveVision
2
+ module ImageFeature
3
+ class Tag
4
+ def key
5
+ 'tags'
6
+ end
7
+
8
+ def parse(response)
9
+ response[key].map { |tag| CognitiveVision::Tag.new(name: tag['name'], confidence: tag['confidence']) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module CognitiveVision
2
+ class ImageFeatures
3
+ ANALYZERS = {
4
+ adult: ImageFeature::Adult.new,
5
+ categories: ImageFeature::Category.new,
6
+ description: ImageFeature::Description.new,
7
+ faces: ImageFeature::Face.new,
8
+ tags: ImageFeature::Tag.new
9
+ }
10
+
11
+ def initialize(features)
12
+ @features = [features].flatten
13
+ end
14
+
15
+ def features_string
16
+ @features.join(',')
17
+ end
18
+
19
+ def analyzers
20
+ ANALYZERS.map{ |feature, analyzer| analyzer if @features.include?(feature) }.reject(&:nil?)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ module CognitiveVision
2
+ class Tag
3
+ attr_reader :confidence, :name
4
+
5
+ def initialize(options = {})
6
+ @confidence = options.fetch(:confidence)
7
+ @name = options.fetch(:name)
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module CognitiveVision
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
@@ -1,8 +1,18 @@
1
1
  require 'cognitive_vision/version'
2
2
  require 'cognitive_vision/configuration'
3
3
  require 'cognitive_vision/connection'
4
+ require 'cognitive_vision/image_feature/adult'
5
+ require 'cognitive_vision/image_feature/category'
6
+ require 'cognitive_vision/image_feature/description'
7
+ require 'cognitive_vision/image_feature/face'
8
+ require 'cognitive_vision/image_feature/tag'
9
+ require 'cognitive_vision/image_features'
4
10
  require 'cognitive_vision/adult'
11
+ require 'cognitive_vision/category'
12
+ require 'cognitive_vision/description'
13
+ require 'cognitive_vision/description_caption'
5
14
  require 'cognitive_vision/face'
15
+ require 'cognitive_vision/tag'
6
16
  require 'cognitive_vision/analyze_response'
7
17
  require 'cognitive_vision/analyze_image'
8
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cognitive_vision
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Ribeiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-09 00:00:00.000000000 Z
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -173,9 +173,19 @@ files:
173
173
  - lib/cognitive_vision/adult.rb
174
174
  - lib/cognitive_vision/analyze_image.rb
175
175
  - lib/cognitive_vision/analyze_response.rb
176
+ - lib/cognitive_vision/category.rb
176
177
  - lib/cognitive_vision/configuration.rb
177
178
  - lib/cognitive_vision/connection.rb
179
+ - lib/cognitive_vision/description.rb
180
+ - lib/cognitive_vision/description_caption.rb
178
181
  - lib/cognitive_vision/face.rb
182
+ - lib/cognitive_vision/image_feature/adult.rb
183
+ - lib/cognitive_vision/image_feature/category.rb
184
+ - lib/cognitive_vision/image_feature/description.rb
185
+ - lib/cognitive_vision/image_feature/face.rb
186
+ - lib/cognitive_vision/image_feature/tag.rb
187
+ - lib/cognitive_vision/image_features.rb
188
+ - lib/cognitive_vision/tag.rb
179
189
  - lib/cognitive_vision/version.rb
180
190
  homepage: https://github.com/mattr/cognitive_vision
181
191
  licenses: