Class: Towise::API

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ API



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/towise.rb', line 8

def initialize(config)
    config['Content-Type'] = 'application/x-www-form-urlencoded'
    @headers = config
    @BASE_URL = "https://api.towise.io"
    @DETECT = {
        "face" => "/detect/face",
        "body" => "/detect/person",
        "emotion" => "/detect/emotion"
    }
    @RECOGNIZE = {
        "face" => "/recognize/face" 
    }
    @PERSONS = "/persons/"
    @FACES = "/faces/"  

end

Instance Attribute Details

#BASE_URLObject

Returns the value of attribute BASE_URL.



7
8
9
# File 'lib/towise.rb', line 7

def BASE_URL
  @BASE_URL
end

#DETECTObject

Returns the value of attribute DETECT.



7
8
9
# File 'lib/towise.rb', line 7

def DETECT
  @DETECT
end

#FACESObject

Returns the value of attribute FACES.



7
8
9
# File 'lib/towise.rb', line 7

def FACES
  @FACES
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/towise.rb', line 7

def headers
  @headers
end

#PERSONSObject

Returns the value of attribute PERSONS.



7
8
9
# File 'lib/towise.rb', line 7

def PERSONS
  @PERSONS
end

#RECOGNIZEObject

Returns the value of attribute RECOGNIZE.



7
8
9
# File 'lib/towise.rb', line 7

def RECOGNIZE
  @RECOGNIZE
end

Instance Method Details

#addFace(image, personId, imageSave = "no") ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/towise.rb', line 140

def addFace(image, personId,imageSave="no")
    data = checkImage?(image)
    data['person_id'] = personId
    data['save_image'] = imageSave
    
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.post(@FACES,data)
    return JSON.parse(response.body)
end

#addPerson(name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/towise.rb', line 95

def addPerson(name)
    data = {
        "name" => name
    }
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.post(@PERSONS,data)
    return JSON.parse(response.body)
end

#bodyDetect(image = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/towise.rb', line 45

def bodyDetect(image=nil)
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end

    response = conn.post(@DETECT['body'],checkImage?(image))
    return JSON.parse(response.body)
end

#checkImage?(image) ⇒ Boolean



25
26
27
28
29
30
31
32
# File 'lib/towise.rb', line 25

def checkImage?(image)
    isBase64 = !!image.match(/(data:image\/jpeg;base63)/)
    if(isBase64)
        return { "image_base64" => image}
    else
        return {"image_url" => image}
    end
end

#emotionDetect(image = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/towise.rb', line 56

def emotionDetect(image=nil)
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.post(@DETECT['emotion'],checkImage?(image))
    return JSON.parse(response.body)
end

#faceComparing(image = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/towise.rb', line 66

def faceComparing(image=nil)
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.post(@RECOGNIZE['face'],checkImage?(image))
    return JSON.parse(response.body)
end

#faceDetect(image = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/towise.rb', line 34

def faceDetect(image=nil)
    
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.post(@DETECT['face'],checkImage?(image))
    return JSON.parse(response.body)
end

#getAllFace(personId) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/towise.rb', line 120

def getAllFace(personId)
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.params['person_id'] = personId
        f.adapter Faraday.default_adapter
    end
    response = conn.get(@FACES)
    return JSON.parse(response.body)
end

#getAllPersonObject



76
77
78
79
80
81
82
83
# File 'lib/towise.rb', line 76

def getAllPerson()
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.adapter Faraday.default_adapter
    end
    response = conn.get(@PERSONS)
    return JSON.parse(response.body)
end

#getFace(faceId) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/towise.rb', line 130

def getFace(faceId)
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.params['face_id'] = faceId
        f.adapter Faraday.default_adapter
    end
    response = conn.get(@FACES)
    return JSON.parse(response.body)
end

#getPerson(personId) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/towise.rb', line 85

def getPerson(personId)
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.headers = @headers
        f.params['person_id'] = personId
        f.adapter Faraday.default_adapter
    end
    response = conn.get(@PERSONS)
    return JSON.parse(response.body)
end

#removeFace(faceId) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/towise.rb', line 154

def removeFace(faceId)
    data = {
        "face_id" => faceId
    }
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.run_request(:delete,@FACES,data,@headers)
    return JSON.parse(response.body)
end

#removePerson(personId) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/towise.rb', line 108

def removePerson(personId)
    data =  {
        "person_id":personId
    }
    conn = Faraday.new(:url => @BASE_URL) do |f|
        f.request :url_encoded
        f.adapter Faraday.default_adapter
    end
    response = conn.run_request(:delete,@PERSONS,data,@headers)
    return JSON.parse(response.body)
end