-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_helpers.py
56 lines (44 loc) · 1.2 KB
/
mongodb_helpers.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
import json
import os
import sys
from pathlib import Path
import dotenv
import pymongo
from loguru import logger
from tqdm import tqdm
def mongodb():
dotenv.load_dotenv()
client = pymongo.MongoClient(os.environ['DB_CONNECTION_STRING'])
db = client[os.environ['DB_NAME']]
return db
def get_mongodb_data():
db = mongodb()
return list(db.bbox.find())
def main():
with open(data_file) as j:
data = json.load(j)
logger.info(f'Processing: {data_file}')
for x in tqdm(data['images']):
x.update({
'file':
Path(x['file']).name,
'_id':
int(Path(x['file']).name.split('picam1-')[1].split('.jpg')[0])
})
try:
db.bbox.insert_one(x)
except pymongo.errors.DuplicateKeyError:
logger.debug('Document with duplicate _id:', x)
with open(data_file, 'rb') as f:
db.data_files.insert_one({
'_id':
Path(data_file).name,
'detection_completion_time':
data['info']['detection_completion_time'],
'data':
f.read()
})
if __name__ == '__main__':
db = mongodb()
data_file = sys.argv[1]
main()