Esempio n. 1
0
def priors_of_wall():
    rj = request.json
    res = {'object': [], 'mapping': {}, 'coarseSemantic': {}}
    if 'auxiliaryWallObj' in rj:
        res_prev = rj['auxiliaryWallObj']
        res['emptyChoice'] = res_prev['emptyChoice']
    else:
        res_prev = res.copy()
        res['emptyChoice'] = '781'
        res['object'].append(res['emptyChoice'])
        res['categoryCodec'] = categoryCodec
    for obj in rj['objList']:
        if 'key' not in obj:
            continue
        if obj['key'] in res_prev['mapping']:
            res['mapping'][obj['key']] = res_prev['mapping'][obj['key']]
            continue
        _mageAddW = wallRelation[getobjCat(obj['modelId'])]['_mageAddWall']
        if len(_mageAddW) == 0:
            res['mapping'][obj['key']] = 'null'
            continue
        else:
            res['mapping'][obj['key']] = random.choice(objListCat[random.choice(_mageAddW)])
    # filling objects to be loaded to the front-end; 
    for thekey in res['mapping']:
        if res['mapping'][thekey] not in res['object']:
            if res['mapping'][thekey] == 'null':
                continue
            res['object'].append(res['mapping'][thekey])
    for newobjname in res['object']:
        res['coarseSemantic'][newobjname] = getobjCat(newobjname)
    return json.dumps(res)
Esempio n. 2
0
def keyObjectKeyFunction(obj):
    if obj is None:
        return -1
    if 'modelId' not in obj:
        return -1
    cat = getobjCat(obj['modelId'])
    if cat == "Unknown Category" or cat not in res_ratio_dom:
        return -1
    return res_ratio_dom[cat][obj['roomType']]
Esempio n. 3
0
def mageAddSwapInstance():
    insname = request.json['insname']
    existList = request.json['existList']
    inscat = getobjCat(insname)
    # a special mechanism for wall objects and categories with only one object;  
    if(len(objListCat[inscat]) <= 1):
        wallalternative = ["313", "781", "124", "633"]
        newinsname = insname
        if insname in wallalternative:
            while newinsname == insname:
                newinsname = random.choice(wallalternative) 
        return newinsname
    # the following algorithm alleviate both IO and user satisfaction; 
    insindex = existList.index(insname)
    while insindex < len(existList):
        existi = existList[insindex]
        insindex += 1
        if inscat == getobjCat(existi) and insname != existi:
            return existi
    # if being executed till then, means we do not find another newinsname; 
    global SWAP_RESTART
    if SWAP_RESTART:
        for existi in existList:
            if inscat == getobjCat(existi) and insname != existi:
                SWAP_RESTART = not SWAP_RESTART
                return existi 
    else:
        newinsname = random.choice(objListCat[inscat])
        while newinsname == insname:
            newinsname = random.choice(objListCat[inscat])
        SWAP_RESTART = not SWAP_RESTART
        return newinsname
    # if being executed till then, means we still dont find another newinsname; 
    newinsname = random.choice(objListCat[inscat])
    while newinsname == insname:
        newinsname = random.choice(objListCat[inscat])
    SWAP_RESTART = not SWAP_RESTART
    return newinsname
def patternChain(pri, secs, tier={'238': 0, '153': 0, "235": 0}):
    print(f'start to generate pattern chain: {pri} & {secs}')
    for s in secs:
        if s not in tier:
            tier[s] = 1
    secs.sort()
    ps = []
    bbs = []
    lsfornextlevel = []
    for sec in secs:
        with open('./latentspace/pos-orient-4/{}.json'.format(pri)) as f:
            p = np.array(json.load(f)[getobjCat(sec)])
        ps.append(p)
        with open(f'./dataset/object/{sec}/{sec}-4p.json') as f:
            bbs.append(np.array(json.load(f)))
        lsfornextlevel.append(range(len(p)))
    res = []
    # vis_patches = []
    for i in range(100):
        # Create figure and axes
        # vis_patches = []
        r = checkbb(bbs.copy(), ps.copy(), secs.copy(), lsfornextlevel.copy(),
                    tier.copy())
        checkr = deepcopy(r)
        try:
            for item in secs:
                checkr[item].pop()
        except Exception:
            continue
        if i % 50 == 0:
            print(r)
        res.append(r)
    print(f'A new hyper-relation is generated - {pri} & {secs}')
    if len(res) == 0:
        return
    with open(
            './latentspace/pos-orient-3/hyper/{}-{}.json'.format(
                pri, '_'.join(secs)), 'w') as f:
        json.dump(res, f)
Esempio n. 5
0
def priors_of_objlist():
    if request.method == 'GET':
        return "Please refer to POST method for acquiring priors. "
    # indexIndicator = 0
    # 'existPair': ['i_dom-c_sec': 'i_sec']
    res = {'prior': [], 'index': [], 'object': [], 'existPair': {}, 'belonging': [], 'coarseSemantic': {}, 'catMask': []}
    room_json = request.json
    if 'auxiliarySecObj' in room_json:
        aso = room_json['auxiliarySecObj']
    else:
        aso = res.copy()
    # note that we currently do not consider a dominant object with two copies; 
    instancePairCount = {}
    for obj in room_json['objList']:
        if 'mageAddDerive' not in obj:
            continue
        if obj['mageAddDerive'] == "":
            continue
        if obj['mageAddDerive'] not in instancePairCount:
            instancePairCount[obj['mageAddDerive']] = 1
        else:
            instancePairCount[obj['mageAddDerive']] += 1
    for obj in room_json['objList']: # for each existing object: 
        if obj is None:
            continue
        if 'modelId' not in obj:
            continue
        ppri = f'./latentspace/pos-orient-4/{obj["modelId"]}.json'
        if os.path.exists(ppri):
            with open(ppri) as f:
                pri = json.load(f)
        else:
            continue
        for c_sec in pri:
            # e.g., Loveseat Sofa; 
            if c_sec not in categoryRelation[getobjCat(obj['modelId'])]:
                continue
            pairid = f'{obj["modelId"]}-{c_sec}'
            if pairid in aso['existPair']:
                objname = aso['existPair'][pairid]
            else:
                if 'share' in categoryRelation[getobjCat(obj['modelId'])][c_sec]: 
                    objname = random.choice(objListCat[
                        random.choice(categoryRelation[getobjCat(obj['modelId'])][c_sec]['share'])
                    ])
                    # print(f'selected: {objname} of {getobjCat(objname)}')
                else:
                    objname = random.choice(objListCat[c_sec])
            pairInsid = f'{obj["modelId"]}-{objname}'
            res['existPair'][pairid] = objname
            if objname not in res['object']:
                res['object'].append(objname)
            if pairInsid in instancePairCount and 'max' in categoryRelation[getobjCat(obj['modelId'])][c_sec]:
                if instancePairCount[pairInsid] >= categoryRelation[getobjCat(obj['modelId'])][c_sec]['max']:
                    continue
            # the following priors are involved in real-time calculation; 
            res['prior'] += priorTransform(pri[c_sec], obj['translate'], obj['orient'], obj['scale'])
            res['index'] += np.full(len(pri[c_sec]), objname).tolist()
            res['catMask'] += np.full(len(pri[c_sec]), categoryCodec[getobjCat(objname)]).tolist()
            res['belonging'] += np.full(len(pri[c_sec]), obj["modelId"]).tolist()
        # for objname in pri:
        #     if objname not in res['object']:
        #         res['object'].append(objname)
        #     res['prior'] += priorTransform(pri[objname], obj['translate'], obj['orient'], obj['scale'])
        #     res['index'] += np.full(len(pri[objname]), objname).tolist()
            # np.arange(indexIndicator, indexIndicator + len(pri[objname])).tolist()
            # indexIndicator = indexIndicator + len(pri[objname])
    # print(instancePairCount)
    for newobjname in res['object']:
        res['coarseSemantic'][newobjname] = getobjCat(newobjname)
    return json.dumps(res)
Esempio n. 6
0
def priors_of_roomShape():
    rj = request.json
    existingCatList = []
    for obj in rj['objList']:
        if obj is None:
            continue
        if 'modelId' not in obj:
            continue
        if obj['modelId'] not in objCatList:
            continue
        if len(objCatList[obj['modelId']]) == 0:
            continue
        if objCatList[obj['modelId']][0] not in existingCatList:
            existingCatList.append(objCatList[obj['modelId']][0])
    existingPendingCatList = existingCatList.copy()
    res = {'object': [], 'prior': [], 'index': [], 'coarseSemantic': {}, 'catMask': []}
    if 'auxiliaryDomObj' in rj:
        if 'heyuindex' in rj['auxiliaryDomObj']:
            res['heyuindex'] = rj['auxiliaryDomObj']['heyuindex']
        for objname in rj['auxiliaryDomObj']['object']:
            if objCatList[objname][0] not in existingPendingCatList:
                existingPendingCatList.append(objCatList[objname][0])
    # print(existingCatList)
    # print(existingPendingCatList)
    # load and process room shapes; 
    room_meta = p2d('.', f'/dataset/room/{rj["origin"]}/{rj["modelId"]}f.obj')
    room_meta = room_meta[:, 0:2]
    wallSecIndices = np.arange(1, len(room_meta)).tolist() + [0]
    res['room_meta'] = room_meta.tolist()
    rv = room_meta[:] - room_meta[wallSecIndices]
    normals = rv[:, [1,0]]
    normals[:, 1] = -normals[:, 1]
    res['room_orient'] = np.arctan2(normals[:, 0], normals[:, 1]).tolist()
    res['room_oriNormal'] = normals.tolist()
    # room_polygon = Polygon(room_meta[:, 0:2]) # requires python library 'shapely'
    # currently, we hack few available coherent groups...
    roomTypeSuggestedList = []
    categoryList = []
    for rt in rj['roomTypes']:
        if 'heyuindex' not in res:
            res['heyuindex'] = np.random.randint(len(roomTypeDemo[rt]))
        categoryList += roomTypeDemo[rt][res['heyuindex']]
        break
    for cat in categoryList:
        if cat in existingCatList:
            continue
        roomTypeSuggestedList.append(random.choice(objListCat[cat]))
    if 'auxiliaryDomObj' not in rj: # if this is the first time calling this function for the pending room...
        res['object'] = roomTypeSuggestedList
    else:
        # re-fuel the pending object list; 
        for newobjname in roomTypeSuggestedList:
            if objCatList[newobjname][0] in existingPendingCatList:
                continue
            rj['auxiliaryDomObj']['object'].append(newobjname)
        res['object'] = rj['auxiliaryDomObj']['object'].copy()
        for objname in rj['auxiliaryDomObj']['object']:
            # if the specific instance is already in the room;
            if objCatList[objname][0] in existingCatList: 
                res['object'].remove(objname)

    if len(res['object']) == 0: # if a recommendation list is full: 
        pass

    # load wall priors; 
    for obj in res['object']:
        with open(f'./latentspace/wdot-4/{obj}.json') as f:
            wallpri = json.load(f)
            res['prior'] += wallpri
            res['index'] += np.full(len(wallpri), obj).tolist()
            res['catMask'] += np.full(len(wallpri), categoryCodec[getobjCat(obj)]).tolist()
    for newobjname in res['object']:
        res['coarseSemantic'][newobjname] = getobjCat(newobjname)
    return json.dumps(res)