forked from cvondrick/vatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
130 lines (105 loc) · 3.88 KB
/
server.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os.path, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import config
from turkic.server import handler, application
from turkic.database import session
import cStringIO
from models import *
import logging
logger = logging.getLogger("vatic.server")
@handler()
def getjob(id, verified):
job = session.query(Job).get(id)
logger.debug("Found job {0}".format(job.id))
if int(verified) and job.segment.video.trainwith:
# swap segment with the training segment
training = True
segment = job.segment.video.trainwith.segments[0]
logger.debug("Swapping actual segment with training segment")
else:
training = False
segment = job.segment
video = segment.video
labels = dict((l.id, l.text) for l in video.labels)
attributes = {}
for label in video.labels:
attributes[label.id] = dict((a.id, a.text) for a in label.attributes)
logger.debug("Giving user frames {0} to {1} of {2}".format(video.slug,
segment.start,
segment.stop))
return {"start": segment.start,
"stop": segment.stop,
"slug": video.slug,
"width": video.width,
"height": video.height,
"skip": video.skip,
"perobject": video.perobjectbonus,
"completion": video.completionbonus,
"blowradius": video.blowradius,
"jobid": job.id,
"training": int(training),
"labels": labels,
"attributes": attributes}
@handler()
def getboxesforjob(id):
job = session.query(Job).get(id)
result = []
for path in job.paths:
attrs = [(x.attributeid, x.frame, x.value) for x in path.attributes]
result.append({"label": path.labelid,
"boxes": [tuple(x) for x in path.getboxes()],
"attributes": attrs})
return result
def readpaths(tracks):
paths = []
logger.debug("Reading {0} total tracks".format(len(tracks)))
for label, track, attributes in tracks:
path = Path()
path.label = session.query(Label).get(label)
logger.debug("Received a {0} track".format(path.label.text))
for frame, userbox in track.items():
box = Box(path = path)
box.xtl = int(userbox[0])
box.ytl = int(userbox[1])
box.xbr = int(userbox[2])
box.ybr = int(userbox[3])
box.occluded = int(userbox[4])
box.outside = int(userbox[5])
box.frame = int(frame)
logger.debug("Received box {0}".format(str(box.getbox())))
for attributeid, timeline in attributes.items():
attribute = session.query(Attribute).get(attributeid)
for frame, value in timeline.items():
aa = AttributeAnnotation()
aa.attribute = attribute
aa.frame = frame
aa.value = value
path.attributes.append(aa)
paths.append(path)
return paths
@handler(post = "json")
def savejob(id, tracks):
job = session.query(Job).get(id)
for path in job.paths:
session.delete(path)
session.commit()
for path in readpaths(tracks):
job.paths.append(path)
session.add(job)
session.commit()
@handler(post = "json")
def validatejob(id, tracks):
job = session.query(Job).get(id)
paths = readpaths(tracks)
return job.trainingjob.validator(paths, job.trainingjob.paths)
@handler()
def respawnjob(id):
job = session.query(Job).get(id)
replacement = job.markastraining()
job.worker.verified = True
session.add(job)
session.add(replacement)
session.commit()
replacement.publish()
session.add(replacement)
session.commit()