def parse_graph_local(data, image, verbose=False): """ Modified version of `utils.ParseGraph`. """ global count_skips objects = [] object_map = {} relationships = [] attributes = [] for obj in data['objects']: object_map, o_ = map_object(object_map, obj) objects.append(o_) for rel in data['relationships']: if rel['subject_id'] in object_map and rel['object_id'] in object_map: object_map, s = map_object(object_map, {'object_id': rel['subject_id']}) v = rel['predicate'] object_map, o = map_object(object_map, {'object_id': rel['object_id']}) rid = rel['relationship_id'] relationships.append(Relationship(rid, s, v, o, rel['synsets'])) else: # Skip this relationship if we don't have the subject and object in # the object_map for this scene graph. Some data is missing in this # way. count_skips[0] += 1 if 'attributes' in data: for attr in data['attributes']: a = attr['attribute'] if a['object_id'] in object_map: if 'attributes' in a: attributes.append( Attribute( attr['attribute_id'], Object(a['object_id'], a['x'], a['y'], a['w'], a['h'], a['names'], a['synsets']), a['attributes'], a['synsets'])) else: attributes.append( Attribute( attr['attribute_id'], Object(a['object_id'], a['x'], a['y'], a['w'], a['h'], a['names'], a['synsets']), None, a['synsets'])) else: count_skips[1] += 1 if verbose: print('Skipped {} rels, {} attrs total'.format(*count_skips)) return Graph(image, objects, relationships, attributes)
def parse_graph_VRD(d): image = Image(d['photo_id'], d['filename'], d['width'], d['height'], '', '') id2obj = {} objs = [] rels = [] atrs = [] for i, o in enumerate(d['objects']): b = o['bbox'] obj = Object(i, b['x'], b['y'], b['w'], b['h'], o['names'], []) id2obj[i] = obj objs.append(obj) for j, a in enumerate(o['attributes']): atrs.append(Attribute(j, obj, a['attribute'], [])) for i, r in enumerate(d['relationships']): s = id2obj[r['objects'][0]] o = id2obj[r['objects'][1]] v = r['relationship'] rels.append(Relationship(i, s, v, o, [])) return Graph(image, objs, rels, atrs)
def parse_graph(data, image): """ Helper to parse a Graph object from API data. """ objects = [] object_map = {} relationships = [] attributes = [] # Create the Objects for obj in data['bounding_boxes']: names = [] synsets = [] for bbx_obj in obj['boxed_objects']: names.append(bbx_obj['name']) synsets.append(parse_synset(bbx_obj['object_canon'])) object_ = Object(obj['id'], obj['x'], obj['y'], obj[ 'width'], obj['height'], names, synsets) object_map[obj['id']] = object_ objects.append(object_) # Create the Relationships for rel in data['relationships']: relationships.append(Relationship(rel['id'], object_map[rel['subject']], rel['predicate'], object_map[rel['object']], parse_synset( rel['relationship_canon']))) # Create the Attributes for atr in data['attributes']: attributes.append(Attribute(atr['id'], object_map[atr['subject']], atr['attribute'], parse_synset(atr['attribute_canon']))) return Graph(image, objects, relationships, attributes)
def parse_graph_VRD(d): image = Image(d["photo_id"], d["filename"], d["width"], d["height"], "", "") id2obj = {} objs = [] rels = [] atrs = [] for i, o in enumerate(d["objects"]): b = o["bbox"] obj = Object(i, b["x"], b["y"], b["w"], b["h"], o["names"], []) id2obj[i] = obj objs.append(obj) for j, a in enumerate(o["attributes"]): atrs.append(Attribute(j, obj, a["attribute"], [])) for i, r in enumerate(d["relationships"]): s = id2obj[r["objects"][0]] o = id2obj[r["objects"][1]] v = r["relationship"] rels.append(Relationship(i, s, v, o, [])) return Graph(image, objs, rels, atrs)
def parse_graph_local(data, image, verbose=False): """ Modified version of `utils.ParseGraph`. """ global count_skips objects = [] object_map = {} relationships = [] attributes = [] for obj in data["objects"]: object_map, o_ = map_object(object_map, obj) objects.append(o_) for rel in data["relationships"]: if rel["subject_id"] in object_map and rel["object_id"] in object_map: object_map, s = map_object(object_map, {"object_id": rel["subject_id"]}) v = rel["predicate"] object_map, o = map_object(object_map, {"object_id": rel["object_id"]}) rid = rel["relationship_id"] relationships.append(Relationship(rid, s, v, o, rel["synsets"])) else: # Skip this relationship if we don't have the subject and object in # the object_map for this scene graph. Some data is missing in this # way. count_skips[0] += 1 if "attributes" in data: for attr in data["attributes"]: a = attr["attribute"] if a["object_id"] in object_map: attributes.append( Attribute( attr["attribute_id"], Object( a["object_id"], a["x"], a["y"], a["w"], a["h"], a["names"], a["synsets"], ), a["attributes"], a["synsets"], )) else: count_skips[1] += 1 if verbose: print("Skipped {} rels, {} attrs total".format(*count_skips)) return Graph(image, objects, relationships, attributes)
def parse_graph(data, image): """ Helper to parse a Graph object from API data. """ objects = [] object_map = {} relationships = [] attributes = [] # Create the Objects for obj in data["bounding_boxes"]: names = [] synsets = [] for bbx_obj in obj["boxed_objects"]: names.append(bbx_obj["name"]) synsets.append(parse_synset(bbx_obj["object_canon"])) object_ = Object( obj["id"], obj["x"], obj["y"], obj["width"], obj["height"], names, synsets, ) object_map[obj["id"]] = object_ objects.append(object_) # Create the Relationships for rel in data["relationships"]: relationships.append( Relationship( rel["id"], object_map[rel["subject"]], rel["predicate"], object_map[rel["object"]], parse_synset(rel["relationship_canon"]), )) # Create the Attributes for atr in data["attributes"]: attributes.append( Attribute( atr["id"], object_map[atr["subject"]], atr["attribute"], parse_synset(atr["attribute_canon"]), )) return Graph(image, objects, relationships, attributes)