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 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 MapObject(object_map, obj): oid = obj['object_id'] obj['id'] = oid del obj['object_id'] if oid in object_map: object_ = object_map[oid] else: if 'attributes' in obj: attrs = obj['attributes'] del obj['attributes'] else: attrs = [] if 'w' in obj: obj['width'] = obj['w'] obj['height'] = obj['h'] del obj['w'], obj['h'] object_ = Object(**obj) object_.attributes = attrs object_map[oid] = object_ return object_map, object_
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: # the second argument should be an Object 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 MapObject(object_map, obj): """ Use object ids as hashes to `src.models.Object` instances. If item not in table, create new `Object`. Used when building scene graphs from json. """ oid = obj['object_id'] obj['id'] = oid del obj['object_id'] if oid in object_map: object_ = object_map[oid] else: if 'attributes' in obj: attrs = obj['attributes'] del obj['attributes'] else: attrs = [] if 'w' in obj: obj['width'] = obj['w'] obj['height'] = obj['h'] del obj['w'], obj['h'] object_ = Object(**obj) object_.attributes = attrs object_map[oid] = object_ return object_map, object_
def __init__(self, mongo_name=None): self.mongo_wrapper = MongoWrapper(mongo_name) self.mono_db_names = self.mongo_wrapper.get_db_names() self.scenes_handler = Scene(self.mongo_wrapper) self.hypothesis_handler = Hypothesis(self.mongo_wrapper) self.objects_handler = Object(self.mongo_wrapper) self.active_data_type = self.scenes_handler
def load_single(filepath, data): objects = data['objects'] mtllib = data['mtllib'] for mtl in mtllib: mtl.convert() model = Model(filepath) for obj in objects.itervalues(): model.objects[obj.name] = Object(obj.name, obj.vdata, obj.material) return model
def save_image_db(image_path: str): logger.info(f"Processing file {image_path}") filename = image_path.split(f"{os.sep}")[-1] mime = get_mime(filename.split(".")[1].lower()) with open(image_path, "rb") as im: bin_data = im.read() classes = detect_objects(image_path) logger.info(f"Detected classes for {image_path}: {classes}") for cl in classes: Object(cl, bin_data, mime, path=image_path).save() os.remove(image_path)
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 main(): """ Function to launch and stop the program Inside we are 5 loops. 1 = Program's loop 2 = Home sreen loop 3 = Game's loop 4 = Winner's loop 5 = Losing's loop """ pygame.init() print(pygame) backpack = bp.Backpack(datas['structure']) maze = maz.Maze('data/maze.txt', datas['structure']) # Map's iniatlization window = pygame.display.set_mode( (datas["nb_sprite"] * datas["width_sprite"], datas["nb_sprite"] * datas["width_sprite"])) icon = pygame.image.load(datas['img_icon']) pygame.display.set_icon(icon) pygame.display.set_caption(datas['title']) to_start = 1 # Launch of the program while to_start: home = pygame.image.load(datas['img_home']) window.blit(home, (0, 0)) # Home screen display pygame.display.flip() # Update the display choice = False home_loop = 1 game_loop = 1 # Launch of the Home Screen while home_loop: pygame.time.Clock().tick(30) for event in pygame.event.get(): if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_F1: home_loop = 0 choice = True # If the gamer steps on the F1 key if choice: window = pygame.display.set_mode( ((datas["nb_sprite"] * datas["width_sprite"]) + datas["width_bp"], datas["nb_sprite"] * datas["width_sprite"])) # Read and display the maze and the backpack maze.read_maze() # Objects creation obj_syringe = obj.Object('syringe', datas['img_syringe'], maze, backpack) obj_ether = obj.Object('ether', datas['img_ether'], maze, backpack) obj_needle = obj.Object('needle', datas['img_needle'], maze, backpack) obj_plastic_tube = obj.Object('plastic tube', datas['img_plastic_tube'], maze, backpack) # Main character creation mc_gyver = char.Character(datas['img_mcgyver'], maze) # Random position objects obj_ether.random_position() obj_needle.random_position() obj_plastic_tube.random_position() maze.view_maze(window) backpack.view_backpack(window) you_win = 0 you_loose = 0 # Launch of the game while game_loop: pygame.time.Clock().tick(30) # User's events for event in pygame.event.get(): if event.type == QUIT: game_loop = 0 pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: game_loop = 0 elif event.key == K_RIGHT: mc_gyver.move('right', window) elif event.key == K_LEFT: mc_gyver.move('left', window) elif event.key == K_UP: mc_gyver.move('up', window) elif event.key == K_DOWN: mc_gyver.move('down', window) else: pass window.blit(mc_gyver.name, (mc_gyver.map_x, mc_gyver.map_y)) # If the character get an object mc_gyver.get_an_object(obj_ether, window) mc_gyver.get_an_object(obj_needle, window) mc_gyver.get_an_object(obj_plastic_tube, window) # View objects obj_ether.view_object(window) obj_needle.view_object(window) obj_plastic_tube.view_object(window) # Method's Character mc_gyver.create_syringe(window, obj_syringe) if obj_syringe.syringe: obj_ether.remove_object(window) obj_needle.remove_object(window) obj_plastic_tube.remove_object(window) mc_gyver.asleep(window) pygame.display.flip() # -------------------- FINISH GAME ----------------------------- # If the character escapted or not if mc_gyver.state is False: game_loop = 0 you_win = 1 elif maze.maze_structure[mc_gyver.position_x][ mc_gyver.position_y] == 'F': if mc_gyver.state: game_loop = 0 you_loose = 1 # Launch of the win screen while you_win: window = pygame.display.set_mode( (datas["nb_sprite"] * datas["width_sprite"], datas["nb_sprite"] * datas["width_sprite"])) for event in pygame.event.get(): if event.type == QUIT: you_win = 0 pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: you_win = 0 win = pygame.image.load(datas['img_win']) window.blit(win, (0, 0)) pygame.display.flip() # Launch of the loose screen while you_loose: window = pygame.display.set_mode( (datas["nb_sprite"] * datas["width_sprite"], datas["nb_sprite"] * datas["width_sprite"])) loose = pygame.image.load(datas['img_loose']) window.blit(loose, (0, 0)) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: you_loose = 0 pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: you_loose = 0
db.session.add(dt) for consumptionType in CONSUMPTIONTYPES: ct = ConsumptionType(name=consumptionType['name'], data_type_id=consumptionType['data_type_id']) db.session.add(ct) for script in SCRIPTS: spt = Script(name=script['name']) db.session.add(spt) for obj in OBJECTS: if 'script_id' in obj: spt_id = obj['script_id'] else: spt_id = None o = Object(name=obj['name'], current_value=obj['current_value'], address=obj['address'], data_type_id=obj['data_type_id'], script_id=spt_id) db.session.add(o) db.engine.execute('CREATE TABLE `event` ( \ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \ `object_id` INTEGER NOT NULL, \ `time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\ `value` INTEGER \ );') # Needs to be manual since we don't need it in the API db.session.commit() # Lets create all devices at the endpoint item_endpoint = 'http://172.20.10.3:5000/api/v1/items'