Class: CloudVision::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_vision/parser.rb

Constant Summary collapse

RESPONSE_LABELS =
{
	SAFETY_TEST => 'safeSearchAnnotation',
	FACIAL_TEST => 'faceAnnotations',
	LABEL_TEST => 'labelAnnotations',
	TEXT_TEST => 'textAnnotations',
	LOGO_TEST => 'logoAnnotations',
	LANDMARK_TEST => 'landmarkAnnotations',
	PROPERTIES_TEST => 'imagePropertiesAnnotation'
}.freeze
RESPONSE_SCALE =
{
	'VERY_UNLIKELY' => -2,
	'UNLIKELY' => -1,
	'UNKNOWN' => 0,
	'POSSIBLE' => 1,
	'LIKELY' => 2,
	'VERY_LIKELY' => 3
}.freeze

Class Method Summary collapse

Class Method Details

.parse_analysis(analysis) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cloud_vision/parser.rb', line 24

def self.parse_analysis( analysis )
	parsed_data = {}

	if analysis && analysis.is_a?( String )
		analysis = JSON.parse( analysis )
	elsif !analysis || !analysis.is_a?( Hash )
		raise( 'Parsing Failed. Analysis must be valid JSON' )
	end

	if analysis[ RESPONSE_LABELS[SAFETY_TEST] ]
		new_data = parse_safety( analysis )
		parsed_data[ SAFETY_TEST ] = new_data
	end

	if analysis[ RESPONSE_LABELS[FACIAL_TEST] ]
		new_data = parse_faces( analysis )
		parsed_data[ FACIAL_TEST ] = new_data
	end

	if analysis[ RESPONSE_LABELS[LABEL_TEST] ]
		new_data = parse_entity( analysis, LABEL_TEST )
		parsed_data[ LABEL_TEST ] = new_data
	end

	if analysis[ RESPONSE_LABELS[TEXT_TEST] ]
		new_data = parse_entity( analysis, TEXT_TEST )
		parsed_data[ TEXT_TEST ] = new_data
	end

	if analysis[ RESPONSE_LABELS[LOGO_TEST] ]
		new_data = parse_entity( analysis, LOGO_TEST )
		parsed_data[ LOGO_TEST ] = new_data
	end

	if analysis[ RESPONSE_LABELS[LANDMARK_TEST] ]
		new_data = parse_entity( analysis, LANDMARK_TEST )
		parsed_data[ LANDMARK_TEST ] = new_data
	end

	if analysis[ RESPONSE_LABELS[PROPERTIES_TEST] ]
		new_data = parse_properties( analysis )
		parsed_data[ PROPERTIES_TEST ] = new_data
	end

	parsed_data
end

.parse_entity(analysis, target) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cloud_vision/parser.rb', line 128

def self.parse_entity( analysis, target )
	processed_data = []

	if analysis[ RESPONSE_LABELS[target] ]
		analysis[ RESPONSE_LABELS[target] ].each do |label_data|
			entity_data = {
				description: label_data[ 'description' ],
				locale: label_data[ 'locale' ],
				score: label_data[ 'score' ].to_f * 100,
				confidence: label_data[ 'confidence' ].to_f * 100,
				relevance: label_data[ 'topicality' ].to_f * 100
			}

			# Some entity responses do not report confidence or relevance data
			if entity_data[ :confidence ] == 0 && entity_data[ :relevance ] == 0
				entity_data.delete( :relevance )
				entity_data.delete( :confidence )
			end

			# Text filters include no score either.
			if entity_data[ :score ] == 0
				entity_data.delete( :score )
			end

			# Landmarks include no locale either.
			if !entity_data[ :locale ]
				entity_data.delete( :locale )
			end

			processed_data << entity_data
		end
	end

	processed_data
end

.parse_faces(analysis) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cloud_vision/parser.rb', line 83

def self.parse_faces( analysis )
	processed_data = []

   if analysis[ RESPONSE_LABELS[FACIAL_TEST] ]
     analysis[ RESPONSE_LABELS[FACIAL_TEST] ].each do |face_data|
       annotations = {}

       # Only include face data for users with 4 bounding points
       if face_data['boundingPoly']['vertices'].length == 4
         top_left = face_data['boundingPoly']['vertices'][ 0 ]
         bottom_right = face_data['boundingPoly']['vertices'][ 2 ]

         annotations[ :face ] = {
           x: top_left['x'],
           y: top_left['y'],
           width: bottom_right['x'].to_i - top_left['x'].to_i,
           height: bottom_right['y'].to_i - top_left['y'].to_i
         }
       end

       # Find the confidence in the image parse
       annotations[ :confidence ] = face_data[ 'detectionConfidence' ]

       # Store the image quality metrics
       annotations[ :quality ] = {
         under_exposed: RESPONSE_SCALE[ face_data['underExposedLikelihood'] ],
         blurred: RESPONSE_SCALE[ face_data['blurredLikelihood'] ],
         headwear: RESPONSE_SCALE[ face_data['headwearLikelihood'] ]
       }

       # Parse the sentiment analysis data
       annotations[ :sentiment ] = {
         joy: RESPONSE_SCALE[ face_data['joyLikelihood'] ],
         sorrow: RESPONSE_SCALE[ face_data['sorrowLikelihood'] ],
         anger: RESPONSE_SCALE[ face_data['angerLikelihood'] ],
         surprise: RESPONSE_SCALE[ face_data['surpriseLikelihood'] ]
       }

       processed_data << annotations
     end
   end

	processed_data
end

.parse_properties(analysis) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cloud_vision/parser.rb', line 164

def self.parse_properties( analysis )
	processed_data = {}

	properties_data = analysis[ RESPONSE_LABELS[PROPERTIES_TEST] ]
	if properties_data && properties_data[ 'dominantColors' ]
		properties_data = properties_data[ 'dominantColors' ][ 'colors' ]
		processed_data[ :colors ] = []

		properties_data.each do |color_data|
			red = color_data[ 'color' ][ 'red' ].to_i.to_s( 16 )
			green = color_data[ 'color' ][ 'green' ].to_i.to_s( 16 )
			blue = color_data[ 'color' ][ 'blue' ].to_i.to_s( 16 )

			processed_data[ :colors ] << {
				hex: "##{red}#{green}#{blue}".upcase,
				score: color_data[ 'score' ].to_f * 100,
				percentage: color_data[ 'pixelFraction' ].to_f * 100
			}
		end
	end

	processed_data
end

.parse_safety(analysis) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cloud_vision/parser.rb', line 71

def self.parse_safety( analysis )
	processed_data = {}

	if analysis[ RESPONSE_LABELS[SAFETY_TEST] ]
     analysis[ RESPONSE_LABELS[SAFETY_TEST] ].each do |risk_test, value|
       processed_data[ risk_test.to_sym ] = RESPONSE_SCALE[ value ]
     end
	end

	processed_data
end