def ParseGraph(data, image): objects = [] object_map = {} relationships = [] attributes = [] # Create the Objects for obj in data['bounding_boxes']: names = [] synsets = [] for s in obj['boxed_objects']: names.append(s['name']) synsets.append(ParseSynset(s['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']], ParseSynset(rel['relationship_canon']))) # Create the Attributes for atr in data['attributes']: attributes.append(Attribute(atr['id'], object_map[atr['subject']], \ atr['attribute'], ParseSynset(atr['attribute_canon']))) return Graph(image, objects, relationships, attributes)
def ParseGraphLocal(data, image, verbose=False): global count_skips objects = [] object_map = {} relationships = [] attributes = [] for obj in data['objects']: object_map, o_ = MapObject(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 = MapObject(object_map, {'object_id': rel['subject_id']}) v = rel['predicate'] object_map, o = MapObject(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'], a['object_id'], a['names'], a['synsets'])) else: count_skips[1] += 1 if verbose: print 'Skipped {} rels, {} attrs total'.format(*count_skips) return Graph(image, objects, relationships, attributes)
def ParseGraphVRD(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 add_relation(request): feature1 = request.GET.get("feature1", None) feature2 = request.GET.get("feature2", None) relation = request.GET.get("relation", None) if feature1 == None or feature2 == None or relation == None: return render_to_json_response({'error': 'bad request'}) if not request.user.is_staff: return render_to_json_response({'error': 'insufficient permissions error. try logging in again? are you staff / admin?'}) feature1 = get_object_or_404_json(Feature, pk=feature1) feature2 = get_object_or_404_json(Feature, pk=feature2) verb = rel_obj = None try: rel_obj = Relationship.objects.get(feature1=feature1, feature2=feature2) verb = "deleted" except ObjectDoesNotExist: rel_obj = Relationship(feature1=feature1, feature2=feature2, relationship_type=relation) verb = "created" if relation == "": if verb == "deleted": rel_obj.delete() if relation == "conflates": feature2.is_primary = True feature2.save() else: return render_to_json_response({'error': 'relation is already deleted'}) else: rel_obj.save() if relation == "conflates": feature2.is_primary = False feature2.save() return render_to_json_response({'success': 'relation %s successfully.' % verb})
def compute_relationship(self, article): kw_list1 = article.keywords.split(",") for a in Article.objects.all(): kw_list2 = a.keywords.split(",") score = self.calculate_relationship(kw_list1, kw_list2) r = Relationship(id1=article.id, id2=a.id, score=score) r.save()
def ParseGraphLocal(data, image_id): global count_skips objects = [] object_map = {} relationships = [] attributes = [] for rel in data['relationships']: object_map, objects, s = MapObject(object_map, objects, rel['subject']) v = rel['predicate'] object_map, objects, o = MapObject(object_map, objects, rel['object']) rid = rel['relationship_id'] relationships.append(Relationship(rid, s, v, o, rel['synsets'])) return Graph(image_id, 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_) pass # 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']))) pass # Create the Attributes for atr in data['attributes']: attributes.append( Attribute(atr['id'], object_map[atr['subject']], atr['attribute'], parse_synset(atr['attribute_canon']))) pass return Graph(image, objects, relationships, attributes)
def ParseGraphLocal(data, image): global count_hit global count_miss objects = [] object_map = {} relationships = [] for obj in data['objects']: object_map, o_ = MapObject(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 = MapObject(object_map, {'object_id': rel['subject_id']}) v = rel['predicate'] object_map, o = MapObject(object_map, {'object_id': rel['object_id']}) rid = rel['relationship_id'] relationships.append(Relationship(rid, s, v, o, rel['synsets'])) else: count_miss += 1 if count_miss % 10000 == 1: print 'Misses: ', count_miss # print 'SKIPPING s: {}, v: {}, o: {}'.format(rel['subject_id'], rel['relationship_id'], rel['object_id']) return Graph(image, objects, relationships, [])
def UpdateRelation(id1, id2, status, action_by): try: exisit_relation =GetRelation(id1,id2) if exisit_relation: exisit_relation.status = status db.session.commit() else: relation = Relationship() if id1 < id2: relation.user1_Id = id1 relation.user2_Id = id2 else: relation.user1_Id = id2 relation.user2_Id = id1 relation.status = status relation.action_by = action_by db.session.add(relation) db.session.commit() return "relation updated successfully!!" except Exception as error: return errors.internal_error(error)
def get_relationships(self): rows = self.database.get_relationships() result = [] for row in rows: result.append(Relationship(row)) return result