def index(self, input_dir): ''' ''' json_files = glob.glob(os.path.join(input_dir, '*.json')) sections = [] for json_file in json_files: basename = os.path.basename(json_file) with open(json_file) as f: new_section = Section.fromJSON(json.load(f)) new_section._id = basename new_section._data_prefix = input_dir new_section._bbox = View.calculateBB(new_section._tiles) sections.append(new_section) return sections
def process(self): ''' Starts loading the next section. ''' # do nothing while workers are not available if self._active_workers.full(): return # # here we have at least 1 worker available # # # viewing has higher priority, so check if we have anything # in the viewing queue # if len(self._viewing_queue) != 0: for view in self._viewing_queue: # check if we have the tiles required for this view allLoaded = True for tile in view._tiles: if tile._status.isVirgin(): # we need to load this tile tile._status.loading() self._loading_queue.append(tile) allLoaded = False print 'We need tile', tile elif tile._status.isLoading(): # the tile is still loading allLoaded = False if allLoaded: # # we have all the tiles and # now we can stitch the view # self._viewing_queue.remove(view) view._status.loading() print 'Stitching', view # now it is time to calculate the bounding box for this view bbox = View.calculateBB(view._tiles, view._zoomlevel) # print bbox view._bbox = bbox # re-attach the bounding box (since something could have changed) # allocate shared mem for view memory = mp.RawArray(ctypes.c_ubyte, bbox[1]*bbox[3]) view._memory = memory # we need to keep a reference view._imagedata = Stitcher.shmem_as_ndarray(memory) # start worker args = (self, view) worker = mp.Process(target=Stitcher.run, args=args) self._active_workers.put(1) # increase worker counter worker.start() # # loading has lower priority # check if we have anything in the loading queue # if len(self._loading_queue) != 0: tile = self._loading_queue.pop(0) #zoomlevels = [0, 1, 2, 3, 4, 5] # TODO dynamically # allocate shared mem for tile and for each zoom level for z in self._zoomlevels: divisor = 2**z tile_width = tile._bbox[1] / divisor tile_height = tile._bbox[3] / divisor # TODO maybe int? memory = mp.RawArray(ctypes.c_ubyte, tile_width*tile_height) imagedata = Loader.shmem_as_ndarray(memory) tile._levels.append(Level(memory, imagedata)) # start worker args = (self, tile) worker = mp.Process(target=Loader.run, args=args) self._active_workers.put(1) # increase worker counter worker.start() return # jump out