summaryrefslogtreecommitdiffstats
path: root/data/face-tag.py
blob: 68311fa98404acfc596022589ee93c7e3010eca2 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/python
from subprocess import Popen
import os
import sys
from face_client import FaceClient, FaceError
from httplib import HTTPException
from urllib2 import HTTPError
from httplib import HTTPException
import time

api_key = '34a84a7835bf24df2d84b4bded84e838'
api_secret = '5bc9e8c5a9e3a2d916abbe3659a1b3f8'
client = FaceClient(api_key, api_secret)

def limits(response):
    return response['remaining'],response['reset_time']

remaining,reset_time = limits(client.account_limits())
sframe = 0
try:
    sframe = int(sys.argv[4])
except:
    sframe = 0
try:
    eframe = int(sys.argv[5])
except:
    eframe = sys.maxint

ns = sys.argv[1]
dataset = sys.argv[2]
pic_dir = sys.argv[3]
f = open('train.log', 'w')
for line in open(dataset):
    line = line.strip().split(',')
    sub_dir,user,run,frame,zv,num,rec = line[0:7]
    user = '_'.join(user.split())
    while remaining == 0:
        time.sleep(max(reset_time - time.time(),0) + 10)
        remaining,reset_time = limits(client)
    if frame != 'frame' and int(frame) > sframe and int(frame) < eframe and int(num) == 1 and rec == 'True':
        response = None
        while response == None:
            try:
                img = open(pic_dir + sub_dir + '/' + frame + '.jpg')
                response = client.faces_detect(file=img)
                img.close()
                remaining,reset_time = limits(response['usage'])
            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]
        tids = []
        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'])]
                tids += [tag['tid']]
        except:
            sys.stderr.write("Unexpexted error:"+str(sys.exc_info()[0])+" "+frame)
            out = line[0:4] + ['0']
        #client.tags_remove(tids=','.join(tags))
        if len(tids) > 0:
            saved = None
            while saved == None:
                try:
                    saved = client.tags_save(tids = tids[0], uid = user+'@'+ns)
                except HTTPException as (errno, strerror):
                    sys.stderr.write("Save HTTP Exception " + strerror + "\n")
                    time.sleep(5)
                except HTTPError as (errno):
                    sys.stderr.write("Save HTTP Error " + str(errno) + "\n")
                    time.sleep(5)
                except FaceError:
                    sys.stderr.write("Save Face Exception\n")
                    sys.stderr.write(str(img))
                    time.sleep(5)
            sys.stderr.write(str(saved)+"\n")
            print ','.join(out)
        time.sleep(0.3)
        #trained = client.faces_train(user+'@'+ns)
        #sys.stderr.write(str(trained)+"\n")
        #f.write("\t".join([user, str(len(saved['saved_tags'])), str(trained)])+"/n")
        #k = trained.keys()
        #k.remove(u'status')
        #ntrain = trained[k[0]][0][u'training_set_size']

        #print ",".join([user,str(count),str(len(saved['saved_tags'])),str(ntrain)] + out)