-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.py
115 lines (76 loc) · 2.55 KB
/
database.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
import datetime
import yaml
from enum import Enum
from pymongo import MongoClient
on = False
db = None
def init(conf="config/config.yaml"):
global db, on
on = True
with open(conf, 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.BaseLoader)['mongo']
client = MongoClient(cfg['server'], int(cfg['port']))
db = client.facerec
clean_invalid_states() # do it at startup
def is_on():
return on
def now():
return datetime.datetime.now().isoformat()
class Status(Enum):
UNKNOWN = 0
RUNNING = 1
COMPLETE = 2
ERROR = 3
def save_metadata(metadata):
locator = metadata['locator']
return db.metadata.replace_one({'locator': locator}, metadata, upsert=True)
def get_metadata(uri):
return db.metadata.find_one({
'$or': [
{'media': uri},
{'programme': uri},
{'locator': uri},
{'notice': uri},
]
})
# invalidate RUNNING statuses at startup
def clean_invalid_states():
db.status.remove({'status': Status.RUNNING.value})
def save_status(uri, project, status):
update = {
'locator': uri,
'project': project,
'status': Status[status].value,
'timestamp': now()
}
return db.status.replace_one({'locator': uri, 'project': project}, update, upsert=True)
def get_status(uri, project):
s = db.status.find_one({'locator': uri, 'project': project})
if s is None:
return None
return Status(s.get('status', 0))
def clean_analysis(uri, project):
db.feat_cluster.remove({'video': uri, 'project': project})
return db.track.remove({'locator': uri, 'project': project})
def insert_partial_analysis(track):
return db.track.insert_one(track)
def insert_feat_cluster(clusters):
for x in clusters:
db.feat_cluster.insert_one(x)
def get_feat_cluster(uri, project):
return list(db.feat_cluster.find({'video': uri, 'project': project}))
def get_analysis(uri, project):
return list(db.track.find({'locator': uri, 'project': project}))
def get_all_about(uri, project):
v = get_metadata(uri)
if v:
locator = v['locator']
status = get_status(locator, project)
if status and status != Status.ERROR:
v['status'] = status.name
v['project'] = project
v['tracks'] = get_analysis(locator, project)
v['feat_clusters'] = get_feat_cluster(locator, project)
return v
def get_video_with(person, project):
return list(db.track.find({'name': person, 'project': project, 'confidence': {'$gte': 0.6}}))