4
4
from botmily .db import db
5
5
import pprint
6
6
7
- def Detect (imageUrl ):
8
- httpcnx = httplib .HTTPSConnection ('api.face.com' , strict = True )
9
- params = {'api_key' : '2fffccb658f4e2b092bc2360e3a6ea27' , 'api_secret' : 'ecf5262853523b43625a5c384977e259' , 'urls' : imageUrl ,'detector' : 'Aggressive' ,'attributes' : 'all' }
10
- url = "/faces/detect.json"
11
- url = url + "?" + urllib .urlencode ( params )
12
- httpcnx .request ('GET' , url )
13
- response = httpcnx .getresponse ()
14
- return json .loads (response .read ())
15
-
16
- def Recognize (imageUrl ):
17
- httpcnx = httplib .HTTPSConnection ('api.face.com' , strict = True )
18
- params = {
'api_key' :
'2fffccb658f4e2b092bc2360e3a6ea27' ,
'api_secret' :
'ecf5262853523b43625a5c384977e259' ,
'urls' :
imageUrl ,
'uids' :
'[email protected] ' ,
'detector' :
'Aggressive' }
19
- url = "/faces/recognize.json"
20
- url = url + "?" + urllib .urlencode ( params )
21
- httpcnx .request ('GET' , url )
22
- response = httpcnx .getresponse ()
23
- return json .loads (response .read ())
24
-
25
- def SaveTag (tid ,userid ):
26
- httpcnx = httplib .HTTPSConnection ('api.face.com' , strict = True )
27
- params = {'api_key' : '2fffccb658f4e2b092bc2360e3a6ea27' , 'api_secret' : 'ecf5262853523b43625a5c384977e259' , 'tids' : tid ,'uid' : userid + '@shughes.uk' }
28
- url = "/tags/save.json"
29
- url = url + "?" + urllib .urlencode ( params )
30
- httpcnx .request ('GET' , url )
31
- response = httpcnx .getresponse ()
32
- return json .loads (response .read ())
7
+ MASHAPE_AUTH = {'X-Mashape-Authorization' : 'dWNwZDBxdnF1bjhjdnlveWdtZnNtdTBpdXhodWFrOmNlOGZiMGVlM2JkZTAzMTk0YmI2ZWNhZDBjNzMwOTFhYzQ2NzUyMTI=' }
33
8
34
- def RemoveTag (tid ):
35
- httpcnx = httplib .HTTPSConnection ('api.face.com' , strict = True )
36
- params = {'api_key' : '2fffccb658f4e2b092bc2360e3a6ea27' , 'api_secret' : 'ecf5262853523b43625a5c384977e259' , 'tids' : tid }
37
- url = "/tags/remove.json"
38
- url = url + "?" + urllib .urlencode ( params )
39
- httpcnx .request ('GET' , url )
40
- response = httpcnx .getresponse ()
41
- return json .loads (response .read ())
42
-
43
- def Train (userid ):
44
- httpcnx = httplib .HTTPSConnection ('api.face.com' , strict = True )
45
- params = {'api_key' : '2fffccb658f4e2b092bc2360e3a6ea27' , 'api_secret' : 'ecf5262853523b43625a5c384977e259' , 'uids' : userid + '@shughes.uk' }
46
- url = "/faces/train.json"
9
+ def Detect (imageUrl ):
10
+ httpcnx = httplib .HTTPSConnection ('lambda-face-detection-and-recognition.p.mashape.com' , strict = True )
11
+ params = {'images' :imageUrl }
12
+ url = "/detect"
47
13
url = url + "?" + urllib .urlencode ( params )
48
- httpcnx .request ('GET' , url )
14
+ httpcnx .request ('GET' , url , None , MASHAPE_AUTH )
49
15
response = httpcnx .getresponse ()
50
16
return json .loads (response .read ())
51
17
@@ -57,13 +23,13 @@ def makeBlurb(face):
57
23
if photo .has_key ('tags' ):
58
24
facecount = 0
59
25
for tag in photo ['tags' ]:
60
- if len (tag ['attributes' ].keys ()) > 1 :
26
+ if len (tag ['attributes' ][ 0 ] .keys ()) > 1 :
61
27
facecount += 1
62
28
if facecount > 1 :
63
29
blurb = '%s faces in this photo, ' % facecount
64
30
faceNum = 1
65
31
for tag in photo ['tags' ]:
66
- if len (tag ['attributes' ].keys ()) > 1 :
32
+ if len (tag ['attributes' ][ 0 ] .keys ()) > 1 :
67
33
blurb = blurb + ' Face %i: ' % faceNum + getTagBlurb (tag )
68
34
faceNum += 1
69
35
elif facecount == 1 :
@@ -88,7 +54,7 @@ def cleanTags(tags):
88
54
for tag in photo ['tags' ]:
89
55
if tids .has_key (tag ['tid' ]):
90
56
toPurge .append (tag )
91
- elif len (tag ['attributes' ].keys ()) < 2 :
57
+ elif len (tag ['attributes' ][ 0 ] .keys ()) < 2 :
92
58
toPurge .append (tag )
93
59
else :
94
60
tids [tag ['tid' ]] = True
@@ -103,42 +69,16 @@ def cleanTags(tags):
103
69
return tags
104
70
105
71
def getTagBlurb (tag ):
106
- print 'blargh'
107
72
stringDict = {'gender' : '' , 'age' : '' , 'face' : '' ,'glasses' :'' , 'smiling' :'' , 'lips' :'' , 'mood' : '' }
108
73
attributes = tag ['attributes' ]
109
74
for attribute in attributes :
110
- if attribute == 'gender' :
111
- stringDict ['gender' ] = "\u0002 %s\u000f (%s%%) " % (attributes [attribute ]['value' ],attributes [attribute ]['confidence' ])
112
-
113
- if attribute == 'age_est' :
114
- stringDict ['age' ] = "\u0002 Age\u000f : %s(%s%%) " % (attributes [attribute ]['value' ],attributes [attribute ]['confidence' ])
75
+ if attribute .has_key ('gender' ):
76
+ stringDict ['gender' ] = "\u0002 %s\u000f (%s%%) " % (attribute ['gender' ],attribute ['confidence' ])
115
77
116
- if attribute == 'mood' :
117
- stringDict ['mood' ] = "\u0002 %s\u000f (%s%%) " % (attributes [attribute ]['value' ],attributes [attribute ]['confidence' ])
118
78
blurb = stringDict ['gender' ] + stringDict ['age' ] + stringDict ['mood' ]
119
79
return blurb
120
80
121
- def getPeopleBlurb (tags ):
122
- blurb = ''
123
- users = []
124
- try :
125
- if len (tags ['photos' ]) == 1 :
126
- photo = tags ['photos' ][0 ]
127
- if photo .has_key ('tags' ):
128
- for tag in photo ['tags' ]:
129
- if len (tag ['uids' ]) > 0 :
130
- users .append (tag ['uids' ][0 ]['uid' ].rstrip ('@shughes.uk' ))
131
- if len (users ) > 0 :
132
- for user in users :
133
- blurb = blurb + '%s,' % user
134
- return blurb + ' could be in this photo'
135
- else :
136
- return None
137
- except Exception , e :
138
- print e
139
- print '\n Face Json :'
140
- print tags
141
- return None
81
+
142
82
143
83
def getTag (face ):
144
84
if len (face ['photos' ]) == 1 :
@@ -147,58 +87,3 @@ def getTag(face):
147
87
if len (photo ['tags' ]) == 1 :
148
88
return photo ['tags' ][0 ]
149
89
return None
150
-
151
- def trainFace (imageUrl ,nick ):
152
- detectResult = Detect (imageUrl )
153
- tag = getTag (detectResult )
154
- if tag :
155
- tid = tag ['tid' ]
156
- savedTIDS = getSavedTIDS (nick )
157
- for s_tid in savedTIDS :
158
- tid = tid + ',' + s_tid
159
- x = SaveTag (tid ,nick )
160
- saved_tags = x ['saved_tags' ]
161
- for tag in saved_tags :
162
- saveTID (nick ,tag ['tid' ])
163
- result = Train (nick )
164
- if result .has_key ('updated' ):
165
- return "I now have %s picture's of %s maybe ill get good at recognizing them" % (result ['updated' ][0 ]['training_set_size' ], nick )
166
- elif result .has_key ('unchanged' ):
167
- return 'I think I already had that picture , nothing changed'
168
- else :
169
- return "Multiple faces or I coulndt find any at all"
170
-
171
- def removeMe (nick ):
172
- tids = getSavedTIDS (nick )
173
- tid = ''
174
- for result in tids :
175
- tid += result
176
- RemoveTag (tid )
177
- db .execute ("delete from face where nick='%s'" % nick )
178
- db .commit ()
179
- return 'Done'
180
-
181
-
182
-
183
- def getSavedTIDS (nick ):
184
- cur = db .execute ("select * from face where nick='%s'" % nick )
185
- result = cur .fetchall ()
186
- results = []
187
- if len (result ) > 0 :
188
- for row in result :
189
- results .append (row [1 ])
190
- return results
191
- elif len (result ) == 0 :
192
- return []
193
-
194
- def saveTID (nick , tid ):
195
- cur = db .execute ("select * from face where nick='%s' and tid='%s'" % (nick ,tid ))
196
- result = cur .fetchall ()
197
- if len (result ) == 1 :
198
- print 'TID already exists for nick'
199
- elif len (result ) > 1 :
200
- print 'porblem'
201
- elif len (result ) == 0 :
202
- print 'Saving Tag %s' % tid
203
- db .execute ("insert into face(nick, tid) values('%s', '%s')" % (nick ,tid ))
204
- db .commit ()
0 commit comments