summaryrefslogtreecommitdiffstats
path: root/data/face-detect.py
blob: 78591d107a64b457796552de08aba2429ff47d31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
70
#!/usr/bin/python
import os
import sys
from face_client import FaceClient, FaceError
import time
from httplib import HTTPException
from urllib2 import HTTPError

api_key = '34a84a7835bf24df2d84b4bded84e838'
api_secret = '5bc9e8c5a9e3a2d916abbe3659a1b3f8'
#url_base = 'http://74.95.195.225/static/img/jon/users/'
client = FaceClient(api_key, api_secret)

runs = []
runs += [[open(sys.argv[2]), sys.argv[1].rstrip('/') + '/' + sys.argv[2].split('/')[-2] + '/']]

def limits(client):
    response = client.account_limits()
    return response['remaining'],response['reset_time']

remaining,reset_time = limits(client)
sframe = 0
try:
    sframe = int(sys.argv[3])
except:
    sframe = 0

for lines, pic_dir in runs:
    for line in lines:
        line = line.strip().split(',')
        name,run,frame,zv = line[0:4]
        while remaining == 0:
            time.sleep(max(reset_time - time.time(),0) + 10)
            remaining,reset_time = limits(client)
        if frame != 'frame' and int(frame) > sframe:
            img = open(pic_dir + frame + '.jpg')
            response = None
            while response == None:
                try:
                    img = open(pic_dir + frame + '.jpg')
                    response = client.faces_detect(file=img)
                    img.close()
                except (HTTPException, HTTPError):
                    sys.stderr.write("HTTP Exception\n")
                    time.sleep(5)
                except FaceError:
                    sys.stderr.write("Face Exception\n")
                    sys.stderr.write(str(img))
                    time.sleep(5)
            out = line[0:4]
            tags = []
            try:
                sys.stderr.write(frame)
                sys.stderr.write(str(response['photos'][0]['tags'])+"\n")
                photo = response['photos'][0]
                out += [str(len(photo['tags']))]
                for tag in photo['tags']:
                    out += [str(tag['recognizable']),str(tag['width']),str(tag['height']),str(tag['center']['x']),str(tag['center']['y'])]
                    tags += [tag['tid']]
            except:
                sys.stderr.write("Unexpexted error:"+str(sys.exc_info()[0])+" "+frame)
                out = line[0:4] + ['0']
            print ','.join(out)
            #client.tags_remove(tids=','.join(tags))
            remaining -= 1
            time.sleep(0.3)