@@ -7,61 +7,27 @@ class << self
77 REKOGNITION_SECRET = ENV [ 'MUSTACHIO_REKOGNITION_SECRET' ] || raise ( 'please set MUSTACHIO_REKOGNITION_SECRET' )
88
99
10- def get_response ( file , jobs )
11- conn = Faraday . new :url => 'https://rekognition.com' do |faraday |
12- faraday . request :multipart
13- faraday . request :url_encoded
14- faraday . adapter :excon
15- end
16-
17- payload = {
18- :api_key => REKOGNITION_KEY ,
19- :api_secret => REKOGNITION_SECRET ,
20- :uploaded_file => Faraday ::UploadIO . new ( file , content_type ( file ) ) ,
21- :jobs => jobs ,
22- :name_space => '' ,
23- :user_id => ''
24- }
25-
26- conn . post ( '/func/api/' , payload )
27- end
28-
29- def json file , jobs = 'face'
30- response = self . get_response ( file , jobs )
31- JSON . parse response . body
32- end
33-
34- def dims file
35- `identify -format "%wx%h" #{ file . path } ` . strip . split ( 'x' ) . map ( &:to_f )
10+ def get_response ( file )
11+ client = Face . get_client ( api_key : REKOGNITION_KEY , api_secret : REKOGNITION_SECRET )
12+ client . faces_detect ( file : Faraday ::UploadIO . new ( file , content_type ( file ) ) )
3613 end
3714
3815 def content_type file
3916 `file -b --mime #{ file . path } ` . strip . split ( /[:;]\s +/ ) [ 0 ]
4017 end
4118
4219 def validate_response ( json )
43- unless json [ 'face_detection' ]
44- usage = json [ 'usage' ] || { }
45- msg = usage [ 'status' ] || 'failure.'
46- raise Error . new ( "Rekognition API: #{ msg } " )
20+ unless json [ 'status' ] == 'success'
21+ msg = json [ 'status' ]
22+ raise Error . new ( "SkyBiometry API: #{ msg } " )
4723 end
4824 end
4925
5026 def face_detection file
51- json = self . json file , 'face_part'
27+ json = self . get_response file
5228 self . validate_response ( json )
53- width , height = self . dims file
5429
55- json [ 'face_detection' ] . map do |entry |
56- mouth_left , mouth_right , nose = entry . values_at ( 'mouth_l' , 'mouth_r' , 'nose' ) . map do |dims |
57- {
58- 'x' => ( ( dims [ 'x' ] . to_f / width ) * 100.0 ) ,
59- 'y' => ( ( dims [ 'y' ] . to_f / height ) * 100.0 )
60- }
61- end
62-
63- { 'mouth_left' => mouth_left , 'mouth_right' => mouth_right , 'nose' => nose }
64- end
30+ json [ 'photos' ] . first [ 'tags' ] . map { |entry | entry . slice ( 'mouth_center' , 'nose' ) }
6531 end
6632 end
6733 end
0 commit comments