File size: 1,479 Bytes
db24a4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from nltk.corpus import wordnet as wn
from fastapi.responses import JSONResponse
from google.cloud import storage

OBJECT_MAP = {
    "stop_sign": "traffic_control",
    "sports_ball": "ball",
    "wine_glass": "glass",
    "potted_plant": "plant",   
}

MULTI_OBJECT_BONUS = 1.0

def get_hypernym_path(object):
    object = object.lower().replace(' ', '_')
    if object in OBJECT_MAP:
        object = OBJECT_MAP[object]
    synset = wn.synsets(object, 'n')
    if len(synset) == 0:
        hypernym_path = [object]
    else:
        hypernym_path = [s.name()[:-5] for s in synset[0].hypernym_paths()[0]]
    return hypernym_path

def parse_query(query: str) -> list[dict]:
    result = []
    query = query.split(',')
    for q in query:
        q = q.split(':')
        if len(q) == 2:
            result.append({'object': q[0].strip(), 'amount': int(q[1].strip())})
        else:
            result.append({'object': q[0].strip(), 'amount': 'any'})
    return result

def make_response(status: int, message: str, data: dict|None = None) -> dict:
    return JSONResponse(content={
        'status': status,
        'message': message,
        'data': data,    
    })
    
def download_from_bucket(dir: str) -> None:
    client = storage.Client()
    bucket = client.get_bucket('thangtd1')
    blobs = bucket.list_blobs(prefix='object-detection')
    for blob in blobs:
        if blob.name.endswith('.json'):
            blob.download_to_filename(dir + '/' + blob.name[16:])