def gen_imgs_classifier(samples, patches_dir):
    num_samples = len(samples)
    print("gen_imgs_classifier ", num_samples)

    for counter, batch_sample in samples.iterrows():

        with openslide.open_slide(batch_sample.slide_path) as slide:
            tiles = DeepZoomGenerator(slide, tile_size=256, overlap=0, limit_bounds=False)

            img = tiles.get_tile(tiles.level_count - 1, batch_sample.tile_loc[::-1])

        # only load truth mask for tumor slides
        if batch_sample.is_tumor:
            truth_slide_path = MASK_TRUTH_DIR / osp.basename(batch_sample.slide_path).replace('.tif',
                                                                                              '_Mask.tif')
            with openslide.open_slide(str(truth_slide_path)) as truth:
                truth_tiles = DeepZoomGenerator(truth, tile_size=256, overlap=0, limit_bounds=False)
                mask = truth_tiles.get_tile(truth_tiles.level_count - 1, batch_sample.tile_loc[::-1])
                # check center patch (128,128) if WHITE then mark as tumor
                mask_n = np.array(mask)
                mask_center = mask_n[128, 128]

                if mask_center[0] == 255:
                    # print("ITS A TUMOR !!!!!!")
                    cv2.imwrite(str(patches_dir) + 'mask/' + str(batch_sample.tile_loc[::-1]) + '.png',
                                cv2.cvtColor(np.array(mask_n), cv2.COLOR_RGB2BGR))
                    cv2.imwrite(str(patches_dir) + 'tumor/' + str(batch_sample.tile_loc[::-1]) + '.png',
                                cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))

        else:
            # normal
            cv2.imwrite(str(patches_dir) + 'normal/' + str(batch_sample.tile_loc[::-1]) + '.png',
                        cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
Esempio n. 2
0
def load_slide():
    slidefile = app.config['DEEPZOOM_SLIDE']
    if slidefile is None:
        raise ValueError('No slide file specified')
    config_map = {
        'DEEPZOOM_TILE_SIZE': 'tile_size',
        'DEEPZOOM_OVERLAP': 'overlap',
        'DEEPZOOM_LIMIT_BOUNDS': 'limit_bounds',
    }
    opts = dict((v, app.config[k]) for k, v in config_map.items())
    slide = open_slide(slidefile)
    app.slides = {
        SLIDE_NAME: DeepZoomGenerator(slide, **opts)
    }
    app.associated_images = []
    app.slide_properties = slide.properties
    for name, image in slide.associated_images.items():
        app.associated_images.append(name)
        slug = slugify(name)
        app.slides[slug] = DeepZoomGenerator(ImageSlide(image), **opts)
    try:
        mpp_x = slide.properties[openslide.PROPERTY_NAME_MPP_X]
        mpp_y = slide.properties[openslide.PROPERTY_NAME_MPP_Y]
        app.slide_mpp = (float(mpp_x) + float(mpp_y)) / 2
    except (KeyError, ValueError):
        app.slide_mpp = 0
Esempio n. 3
0
def load_slide(slidefile, tile_size):
    print("there is an image")
    slide = open_slide(slidefile)
    slides = {
        "slide": DeepZoomGenerator(slide,
                                   tile_size=tile_size,
                                   overlap=1,
                                   limit_bounds=True)
    }
    associated_images = []
    slide_properties = slide.properties
    for name, image in slide.associated_images.items():
        associated_images.append(name)
        slug = slugify(name)
        slides[slug] = DeepZoomGenerator(ImageSlide(image),
                                         tile_size=tile_size,
                                         overlap=1,
                                         limit_bounds=True)
    try:
        mpp_x = slide.properties[openslide.PROPERTY_NAME_MPP_X]
        mpp_y = slide.properties[openslide.PROPERTY_NAME_MPP_Y]
        slide_mpp = (float(mpp_x) + float(mpp_y)) / 2
    except (KeyError, ValueError):
        slide_mpp = 0

    return slides, associated_images, slide_properties, slide_mpp
def index_wsi(file_path):
    config_map = {
        'DEEPZOOM_TILE_SIZE': 'tile_size',
        'DEEPZOOM_OVERLAP': 'overlap',
        'DEEPZOOM_LIMIT_BOUNDS': 'limit_bounds',
    }
    opts = dict((v, app.config[k]) for k, v in config_map.items())
    slide = open_slide('static/wsi/' + file_path)
    app.slides = {
        SLIDE_NAME: DeepZoomGenerator(slide, **opts)
    }
    app.associated_images = []
    app.slide_properties = slide.properties
    for name, image in slide.associated_images.items():
        app.associated_images.append(name)
        slug = slugify(name)
        app.slides[slug] = DeepZoomGenerator(ImageSlide(image), **opts)
    try:
        mpp_x = slide.properties[openslide.PROPERTY_NAME_MPP_X]
        mpp_y = slide.properties[openslide.PROPERTY_NAME_MPP_Y]
        slide_mpp = (float(mpp_x) + float(mpp_y)) / 2
    except (KeyError, ValueError):
        slide_mpp = 0
    slide_url = url_for('dzi', slug=SLIDE_NAME)
    return render_template('as_viewer.html', slide_url=slide_url, slide_mpp=slide_mpp, file_name=file_path, dictionary=getDictionary(file_path))
Esempio n. 5
0
    def _serve_tiles(self, request):
        tile_config = request.path[len("/data/plugin/WSI/tiles/"):].split("/")

        id = int(tile_config[0])

        if id == self._cache_slide_id:
            slide = self._cache_slide
        else:
            slide_path = self._cache_slide_prop[id]["slide_path"]
            slide = DeepZoomGenerator(open_slide(slide_path))
            self._cache_slide = slide
            self._cache_slide_id = id

        if len(tile_config) <= 2:
            response = werkzeug.Response(slide.get_dzi("jpeg"))
            response.headers.set('Content-Type', 'application/xml')
            return response
        else:
            level, colrow = tile_config[1:]
            level = int(level)
            col, row = map(int, colrow[:-1 * len(".jpeg"):].split("_"))

            tile = slide.get_tile(level, (col, row))

            buf = PILBytesIO()
            tile.save(buf, "jpeg", quality=75)
            response = werkzeug.Response(buf.getvalue())
            response.headers.set('Content-Type', 'image/jpeg')
            return response
Esempio n. 6
0
 def _get_dz(self, associated=None):
     if associated is not None:
         image = ImageSlide(self._slide.associated_images[associated])
     else:
         image = self._slide
     return DeepZoomGenerator(image, self._tile_size, self._overlap,
                              limit_bounds=self._limit_bounds)
Esempio n. 7
0
    def print_tile_dimensions(self, patch_size=0):
        patch_dim = patch_size
        level_count = 0
        level_tiles = []
        level_dimensions = []

        # Check that either tile or patch size is set correctly.
        if patch_size == 0:
            print("[error]: set patch size.")
        else:
            print("Setting patch size ", patch_dim)

            # Open image and return variables.
            slide = open_slide(self.file_path)
            tiles = DeepZoomGenerator(slide, tile_size=patch_size, overlap=0)

            level_count = tiles.level_count
            level_tiles = tiles.level_tiles
            level_dimensions = tiles.level_dimensions

        print("============Tile Dimensions==========")
        print("Level count:         " + str(level_count))
        print("Level tiles:         " + str(level_tiles))
        print("Level dimensions:    " + str(level_dimensions))
        print("=====================================")
Esempio n. 8
0
def ndpi_cut(ndpi_way, x, k, outway, pe):  # x = 分割后图片边长  k = ndpi图片的第几个level
    # time_start = time.time()
    if not os.path.isdir(outway):
        os.makedirs(outway)
    for files in os.listdir(ndpi_way):
        # 当前文件夹所有文件
        if files.endswith('.tiff'):  # 判断是否以.ndpi结尾 也可以为tiff
            slide = op.OpenSlide(ndpi_way + '\\' + files)
            # print(slide.level_dimensions)
            bigImg = DeepZoomGenerator(slide,
                                       tile_size=x - 2,
                                       overlap=1,
                                       limit_bounds=False)
            count = bigImg.level_count - 1
            z = count - k  # 倒着读取
            raw, col = bigImg.level_tiles[z]
            # print(raw, col)
            (filename, extension) = os.path.splitext(files)
            for i in range(raw - 1):
                for j in range(col - 1):
                    try:
                        img = bigImg.get_tile(z, (j, i))
                        Img = np.asarray(img)
                        if Img.shape == (a, a, Img.shape[2]):
                            p = judge_white.judge_white(Img)
                            print(p, Img.shape, (a, a, Img.shape[2]))
                            if p < pe:
                                outputway = outway + '\\' + filename + r'_{a}_{b}.png'.format(
                                    a=i, b=j)
                                img.save(outputway)
                            else:
                                pass
                    except ValueError:
                        break
            print(files + ' done')
Esempio n. 9
0
def getTile(file, level, col, row):
    format = 'jpeg'
    slide = DeepZoomGenerator(open_slide(file))
    tile = slide.get_tile(level, (col, row))
    buf = PILBytesIO()
    tile.save(buf, format, quality=app.config['DEEPZOOM_TILE_QUALITY'])
    return base64.b64encode(buf.getvalue())
Esempio n. 10
0
def load_slide(file):
    slidefile = file
    if slidefile is None:
        raise ValueError('No slide file specified')
    config_map = {
        'DEEPZOOM_TILE_SIZE': 'tile_size',
        'DEEPZOOM_OVERLAP': 'overlap',
        'DEEPZOOM_LIMIT_BOUNDS': 'limit_bounds',
    }
    opts = dict((v, app.config[k]) for k, v in config_map.items())
    slide = open_slide(slidefile)
    left = slide.properties[
        "mirax.NONHIERLAYER_3_LEVEL_0_SECTION.COMPRESSED_STITCHING_ORIG_SLIDE_SCANNED_AREA_IN_PIXELS__LEFT"]
    top = slide.properties[
        "mirax.NONHIERLAYER_3_LEVEL_0_SECTION.COMPRESSED_STITCHING_ORIG_SLIDE_SCANNED_AREA_IN_PIXELS__TOP"]

    app.slides = {
        SLIDE_NAME: DeepZoomGenerator(slide, **opts),
        'left': left,
        'top': top
    }
    try:
        mpp_x = slide.properties[openslide.PROPERTY_NAME_MPP_X]
        mpp_y = slide.properties[openslide.PROPERTY_NAME_MPP_Y]
        app.slide_mpp = (float(mpp_x) + float(mpp_y)) / 2
        return app.slide_mpp
    except (KeyError, ValueError):
        app.slide_mpp = 0
        return 0
Esempio n. 11
0
    def get(self, path):
        with self._lock:
            if path in self._cache:
                # Move to end of LRU
                slide = self._cache.pop(path)
                self._cache[path] = slide
                return slide

        if path.endswith('.ndpi') and self.use_ndp_read and os.path.getsize(
                path) >= self.ndpi_size_limit:
            slide = LargeNDPiDeepZoomGenerator(path, self.dz_opts['tile_size'],
                                               self.dz_opts['overlap'])
        else:
            osr = OpenSlide(path)
            slide = DeepZoomGenerator(osr, **self.dz_opts)
            try:
                mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
                mpp_y = osr.properties[openslide.PROPERTY_NAME_MPP_Y]
                slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
            except (KeyError, ValueError):
                slide.mpp = 0

        with self._lock:
            if path not in self._cache:
                if len(self._cache) == self.cache_size:
                    self._cache.popitem(last=False)
                self._cache[path] = slide
        return slide
 def run_image(self, associated=None):
     """
     Run a single image from self.slide.
     """
     if associated is None:
         image = self.slide
         if self.with_viewer:
             basename = os.path.join(self.basename, VIEWER_SLIDE_NAME)
         else:
             basename = self.basename
     else:
         image = ImageSlide(self.slide.associated_images[associated])
         basename = os.path.join(self.basename, self.slugify(associated))
     dz = DeepZoomGenerator(image,
                            self.tile_size,
                            self.overlap,
                            limit_bounds=self.limit_bounds)
     tiler = DeepZoomImageTiler(dz, basename, self.format, associated,
                                self.queue, self.slide, self.basename_jpg,
                                self.xml_file, self.mask_type,
                                self.xml_label, self.roi_percentage,
                                self.img_extension, self.save_masks,
                                self.magnification)
     tiler.run()
     self.dzi_data[self.url_for(associated)] = tiler.get_dzi()
Esempio n. 13
0
 def _run_image(self, associated=None):
     """Run a single image from self._slide."""
     if associated is None:
         image = self._slide
         if self._with_viewer:
             basename = os.path.join(self._basename, VIEWER_SLIDE_NAME)
         else:
             basename = self._basename
     else:
         image = ImageSlide(self._slide.associated_images[associated])
         basename = os.path.join(self._basename, self._slugify(associated))
     print("enter DeepZoomGenerator")
     dz = DeepZoomGenerator(image,
                            self._tile_size,
                            self._overlap,
                            limit_bounds=self._limit_bounds)
     print("enter DeepZoomImageTiler")
     tiler = DeepZoomImageTiler(dz, basename, self._format, associated,
                                self._queue, self._slide, self._basenameJPG,
                                self._xmlfile, self._mask_type,
                                self._xmlLabel, self._ROIpc,
                                self._ImgExtension, self._SaveMasks,
                                self._Mag)
     tiler.run()
     self._dzi_data[self._url_for(associated)] = tiler.get_dzi()
Esempio n. 14
0
def tile(mode, project, specimen, block, resource, slide_name, slide_ext,
         level, col, row, format):
    format = format.lower()
    if format != 'jpeg' and format != 'png':
        # Not supported by Deep Zoom
        return 'bad format'
        abort(404)

    # Get a project reference, using either local database or remotely supplied dict
    pr = dzi_get_project_ref(project)

    # Get the raw SVS/tiff file for the slide (the resource should exist,
    # or else we will spend a minute here waiting with no response to user)
    sr = SlideRef(pr, specimen, block, slide_name, slide_ext)
    tiff_file = sr.get_local_copy(resource)

    os = None
    if mode == 'affine':
        os = AffineTransformedOpenSlide(
            tiff_file, get_affine_matrix(sr, 'affine', resource, 'image'))
    else:
        os = OpenSlide(tiff_file)

    dz = DeepZoomGenerator(os)
    tile = dz.get_tile(level, (col, row))

    buf = PILBytesIO()
    tile.save(buf, format, quality=75)
    resp = make_response(buf.getvalue())
    resp.mimetype = 'image/%s' % format
    print('PNG size: %d' % (len(buf.getvalue(), )))
    return resp
Esempio n. 15
0
    def get(self, path):
        with self._lock:
            if path in self._cache:
                # Move to end of LRU
                slide = self._cache.pop(path)
                self._cache[path] = slide
                return slide

        if is_image(path):
            #osr = ImageSlide(path)
            osr = get_osr(path)
        else:
            osr = OpenSlide(path)
        slide = DeepZoomGenerator(osr, **self.dz_opts)
        try:
            mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
            mpp_y = osr.properties[openslide.PROPERTY_NAME_MPP_Y]
            slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
        except (KeyError, ValueError):
            slide.mpp = 0

        with self._lock:
            if path not in self._cache:
                if len(self._cache) == self.cache_size:
                    self._cache.popitem(last=False)
                self._cache[path] = slide
        return slide
Esempio n. 16
0
def save_valid_imgs(samples, patch_size=PATCH_SIZE):
    num_samples = len(samples)
    for idx in range(num_samples):
        for _, batch_sample in samples.iloc[idx:idx + 1].iterrows():
            slide_path = batch_sample.slide_path
        print(slide_path)
        slide_name = 's'
        slide_contains_tumor = 'pos' in slide_path
        with openslide.open_slide(slide_path) as slide:
            tiles = DeepZoomGenerator(slide,
                                      tile_size=patch_size,
                                      overlap=0,
                                      limit_bounds=False)
            x, y = batch_sample.tile_loc[::-1]
            if slide_contains_tumor:
                start_x = int(slide.properties.get('openslide.bounds-x', 0))
                start_y = int(slide.properties.get('openslide.bounds-y', 0))
                start_x = start_x / patch_size
                start_y = start_y / patch_size
                x += start_x
                y += start_y

                img = tiles.get_tile(tiles.level_count - 1, (x, y))
                img.save('/data/model/patches/val/pos/{}_pos_{}.png'.format(
                    slide_name, idx))

            else:
                img = tiles.get_tile(tiles.level_count - 1, (x, y))
                img.save('/data/model/patches/val/neg/{}_neg_{}.png'.format(
                    slide_name, idx))

    print('num_samples_val : ', num_samples)
    print('val img save completed')
Esempio n. 17
0
    def get(self, path):
        """
        Method for getting slide cache. Has self and path arguments.
        """
        with self._lock:
            if path in self._cache:
                # Move to end of LRU
                slide = self._cache.pop(path)
                self._cache[path] = slide
                return slide

        osr = OpenSlide(path)
        slide = DeepZoomGenerator(osr, **self.dz_opts)
        try:
            mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
            mpp_y = osr.properties[openslide.PROPERTY_NAME_MPP_Y]
            slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
        except (KeyError, ValueError):
            slide.mpp = 0

        with self._lock:
            if path not in self._cache:
                if len(self._cache) == self.cache_size:
                    self._cache.popitem(last=False)
                self._cache[path] = slide
        return slide
Esempio n. 18
0
def image(filename):
    user = current_user
    engine = db.get_engine(bind=user.dbname)
    DBSession = sessionmaker(bind=engine)
    db_session = DBSession()

    annotationsInfo = db_session.query(Annotation).filter_by(pathimage_imageid=filename).all()

    slug = slugify(filename)
    slidempp = 'slide_mpp'
    imagesize = 'imagesize'
    if not hasattr(app,'slides'):
        app.slides = {}
        print('app.slides is null')


    if not app.slides.has_key(slug):
        print('image not exsits ' + slug)
        image = db_session.query(PathImage).filter_by(filename=filename).first()
        slidefile = image.filepath
        config_map = {
            'DEEPZOOM_TILE_SIZE': 'tile_size',
            'DEEPZOOM_OVERLAP': 'overlap',
            'DEEPZOOM_LIMIT_BOUNDS': 'limit_bounds',
        }
        opts = dict((v, app.config[k]) for k, v in config_map.items())

        if 'kfb' in slidefile:
            slide = kfbslide.KfbSlide(slidefile)
            deepzoom = kfb_deepzoom.KfbDeepZoomGenerator(slide, **opts)
        else:
            slide = open_slide(slidefile)
            deepzoom = DeepZoomGenerator(slide, **opts)

        slideinfo = {SLIDE_NAME: deepzoom}

        try:
            mpp_x = slide.properties[openslide.PROPERTY_NAME_MPP_X]
            mpp_y = slide.properties[openslide.PROPERTY_NAME_MPP_Y]
            slideinfo[slidempp] = (float(mpp_x) + float(mpp_y)) / 2
        except (KeyError, ValueError):
            slideinfo[slidempp] = 0
        slideinfo[imagesize] = [int(deepzoom.level_dimensions[-1][0]), int(deepzoom.level_dimensions[-1][1])]
        slideinfo['active'] = True
        app.slides[slug] = slideinfo

    annotaions = getannotations(annotationsInfo, app.slides[slug][imagesize], db_session,user)
    db_session.close()
    slidename = SLIDE_NAME + slug
    slide_url = url_for('dzi', slug=slidename)
    return render_template("display.html",
                           user=user,
                           slide_url=slide_url,
                           slide_mpp=app.slides[slug][slidempp],
                           canlogout=True,
                           image_name=filename,
                           annotations=annotaions,
                           imagesize=app.slides[slug][imagesize]
                           )
Esempio n. 19
0
def dzi_asso(path, associated_name):
    slide = _get_slide(path)
    associated_image = slide.osr.associated_images[associated_name]
    dzg = DeepZoomGenerator(ImageSlide(associated_image))
    format = app.config['DEEPZOOM_FORMAT']
    resp = make_response(dzg.get_dzi(format))
    resp.mimetype = 'application/xml'
    return resp
Esempio n. 20
0
def gen_imgs(samples, batch_size, base_truth_dir=BASE_TRUTH_DIR, shuffle=True):
   
    
    num_samples = len(samples)
    while 1: # Loop forever so the generator never terminates
        if shuffle:
            samples = samples.sample(frac=1) # shuffle samples
        
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples.iloc[offset:offset+batch_size]
        
            #images = []
            #masks = []
            for _, batch_sample in batch_samples.iterrows():
                slide_contains_tumor = osp.basename(batch_sample.slide_path).startswith('tumor_')
                 
                with openslide.open_slide(batch_sample.slide_path) as slide:
                    tiles = DeepZoomGenerator(slide, tile_size=224, overlap=0, limit_bounds=False)
                    img = tiles.get_tile(tiles.level_count-1, batch_sample.tile_loc[::-1])
                    im = np.array(img)
                    int1, int2= batch_sample.tile_loc[::-1]
                    if  batch_sample.is_tumor==True:
                        imsave('/home/wli/Documents/patches/tumor/%s_%d_%d.png' % (os.path.splitext(osp.basename(batch_sample.slide_path))[0], int1, int2), im)
                    else:
                        imsave('/home/wli/Documents/patches/normal/%s_%d_%d.png' % (os.path.splitext(osp.basename(batch_sample.slide_path))[0], int1, int2), im)

                # only load truth mask for tumor slides
                if slide_contains_tumor:
                    truth_slide_path = osp.join(base_truth_dir, osp.basename(batch_sample.slide_path).replace('.tif', '_mask.tif'))
                    with openslide.open_slide(str(truth_slide_path)) as truth:
                        truth_tiles = DeepZoomGenerator(truth, tile_size=224, overlap=0, limit_bounds=False)
                        mask = truth_tiles.get_tile(truth_tiles.level_count-1, batch_sample.tile_loc[::-1])
                        mask = (cv2.cvtColor(np.array(mask), cv2.COLOR_RGB2GRAY) > 0).astype(int)
                        mk = np.array(mask)
                        int1, int2= batch_sample.tile_loc[::-1]
                    if  batch_sample.is_tumor==True:
                        imsave('/home/wli/Documents/patches_mask/tumor/%s_%d_%d.png' % (os.path.splitext(osp.basename(batch_sample.slide_path))[0], int1, int2), mk)
                    else:
                        imsave('/home/wli/Documents/patches_mask/normal/%s_%d_%d.png' % (os.path.splitext(osp.basename(batch_sample.slide_path))[0], int1, int2), mk) 
                else:
                    mask = np.zeros((224, 224))
                    mk = np.array(mask)
                    int1, int2= batch_sample.tile_loc[::-1]
                    imsave('/home/wli/Documents/patches_mask/normal/%s_%d_%d.png' % (os.path.splitext(osp.basename(batch_sample.slide_path))[0], int1, int2), mk)

            yield
Esempio n. 21
0
 def _get_dz(self):
     '''
     Create and return an openslide.DeepZoomGenerator object
     '''
     return DeepZoomGenerator(self._slide,
                              self._tile_size,
                              self._overlap,
                              limit_bounds=self._limit_bounds)
Esempio n. 22
0
 def do_tile(associated, image):
     dz = DeepZoomGenerator(image,
                            TILE_SIZE,
                            OVERLAP,
                            limit_bounds=LIMIT_BOUNDS)
     return sync_image(pool, slide_relpath, slide_path, associated,
                       dz, key_basepath, key_md5sums,
                       mpp if associated is None else None)
Esempio n. 23
0
 def insertslide(cls, key, sl):
     opts = {
         'tile_size': settings.DEEPZOOM_TILE_SIZE,
         'overlap': settings.DEEPZOOM_OVERLAP
     }
     with cls._dict_lock:
         cls._slides[key] = sl
         cls._deepzooms[key] = DeepZoomGenerator(sl, **opts)
Esempio n. 24
0
 def get_dz(self, slide_path, associated=None):
     if slide_path != self._slide_path:
         generator = lambda slide: DeepZoomGenerator(
             slide, TILE_SIZE, OVERLAP, limit_bounds=LIMIT_BOUNDS)
         slide = OpenSlide(slide_path)
         self._slide_path = slide_path
         self._generators = {None: generator(slide)}
         for name, image in slide.associated_images.iteritems():
             self._generators[name] = generator(ImageSlide(image))
     return self._generators[associated]
Esempio n. 25
0
def load_slide():
    slidefile = app.config['DEEPZOOM_SLIDE']
    if slidefile is None:
        raise ValueError('No slide file specified')
    config_map = {
        'DEEPZOOM_TILE_SIZE': 'tile_size',
        'DEEPZOOM_OVERLAP': 'overlap',
    }
    opts = dict((v, app.config[k]) for k, v in config_map.items())
    slide = open_slide(slidefile)
    app.slides = {
        SLIDE_NAME: DeepZoomGenerator(slide, **opts)
    }
    app.associated_images = []
    app.slide_properties = slide.properties
    for name, image in slide.associated_images.items():
        app.associated_images.append(name)
        slug = slugify(name)
        app.slides[slug] = DeepZoomGenerator(ImageSlide(image), **opts)
def generate_prob_map(wsi_img):#,net,transformer,batch_size):
  f = openslide.OpenSlide(wsi_img)
  dz = DeepZoomGenerator(f,256,0,True)
  tile_index = dz.level_count-1
  tile_count = dz.level_tiles[tile_index]
  
  potential_list = get_all_possible_patches(f,dz)
  potential_list = np.array(potential_list)

  #processes = []
  #final_result = mp.Queue()
  #manager = Manager()
  #return_dict = manager.dict()
  #fold_num = 7
  #num_patches = potential_list.shape[0]
  #fold_count = num_patches/fold_num
  
  #for gpu_id in range(7):
  #  start_pos = gpu_id*fold_count
  #  end_pos = (gpu_id+1)*fold_count
  #  if gpu_id == fold_num-1:
  #    end_pos = num_patches
  #  one_fold = potential_list[start_pos:end_pos]
  #  processes.append(Process(target=classify_with_1_gpu,args=\
  #    (dz,tile_index,one_fold,gpu_id+2,return_dict)))
    #process.start()
  #[process.start() for process in processes]
    
  #for process in processes:
  #  process.join()
    
  #results = [final_result.get() for p in processes]
  #results.sort()
  #results = return_dict.values()
  #result = np.zeros((num_patches),np.float32)
  #start_pos = 0
  #for gpu_id in range(7):
  #  one_result = return_dict[gpu_id+2]
  #  end_pos = start_pos + one_result.shape[0]
  #  result[start_pos:end_pos] = one_result
  #  start_pos += one_result.shape[0]
  #print(result)
  #print (len(result))
  
  model_def = 'model_files/deploy.prototxt'
  model_weights = 'model_files/bvlc_googlenet_iter_40000.caffemodel'
  
  result = batch_predict(potential_list,dz,tile_index,model_def,model_weights)
  
  final_map = np.zeros((tile_count[1],tile_count[0]),np.float32)
  num_patches = result.shape[0]
  for i in range(num_patches):
    final_map[potential_list[i,1],potential_list[i,0]]=result[i]
      
  return final_map,num_patches
Esempio n. 27
0
    def get(self, path):
        with self._lock:
            if path in self._cache:
                # Move to end of LRU
                slide = self._cache.pop(path)
                self._cache[path] = slide
                return slide
        try:
            osr = OpenSlide(path)
        except:
            osr = ImageSlide(path)
            #Fix for 16 bits tiff files
            if osr._image.getextrema()[1] > 256:
                osr._image = osr._image.point(lambda i: i *
                                              (1. / 256)).convert('L')

        slide = DeepZoomGenerator(osr, **self.dz_opts)
        slide.osr = osr

        slide.associated_images = {}
        for name, image in slide.osr.associated_images.items():
            slide.associated_images[name] = DeepZoomGenerator(
                ImageSlide(image))

        try:
            mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
            mpp_y = osr.properties[openslide.PROPERTY_NAME_MPP_Y]
            slide.properties = osr.properties
            slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
        except (KeyError, ValueError):
            slide.mpp = 0
        try:
            slide.properties = slide.properties
        except:
            slide.properties = osr.properties
        slide.tileLock = Lock()
        with self._lock:
            if path not in self._cache:
                while len(self._cache) >= self.cache_size:
                    self._cache.popitem(last=False)
                self._cache[path] = slide
        return slide
Esempio n. 28
0
def slide_dzi(request, pk):
    scan = get_object_or_404(Scan, pk=pk)
    raw_slide = open_slide(scan.file)
    slide = DeepZoomGenerator(
        raw_slide,
        tile_size=DEEPZOOM_TILE_SIZE,
        overlap=DEEPZOOM_OVERLAP,
        limit_bounds=DEEPZOOM_LIMIT_BOUNDS,
    )
    response = slide.get_dzi(DEEPZOOM_FORMAT)
    return HttpResponse(response, content_type="application/xml")
Esempio n. 29
0
    def get(self, path, originalPath=None):
        with self._lock:
            if path in self._cache:
                # Move to end of LRU
                slide = self._cache.pop(path)
                self._cache[path] = slide
                return slide

        osr = OpenSlide(path)
        # try:
        #    osr = OpenSlide(path)
        # except:
        #    osr = ImageSlide(path)
        # Fix for 16 bits tiff files
        # if osr._image.getextrema()[1] > 256:
        #     osr._image = osr._image.point(lambda i:i*(1./256)).convert('L')

        slide = DeepZoomGenerator(osr, **self.dz_opts)
        slide.osr = osr

        slide.associated_images = {}
        for name, image in slide.osr.associated_images.items():
            slide.associated_images[name] = image

        try:
            mpp_x = osr.properties[openslide.PROPERTY_NAME_MPP_X]
            mpp_y = osr.properties[openslide.PROPERTY_NAME_MPP_Y]
            slide.properties = osr.properties
            slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
        except (KeyError, ValueError):
            try:
                if osr.properties["tiff.ResolutionUnit"] == "centimetre":
                    numerator = 10000  # microns in CM
                else:
                    numerator = 25400  # Microns in Inch
                mpp_x = numerator / float(osr.properties["tiff.XResolution"])
                mpp_y = numerator / float(osr.properties["tiff.YResolution"])
                slide.properties = osr.properties
                slide.mpp = (float(mpp_x) + float(mpp_y)) / 2
            except:
                slide.mpp = 0
        try:
            slide.properties = slide.properties
        except:
            slide.properties = osr.properties
        slide.tileLock = Lock()
        if originalPath:
            slide.properties = {"Path": originalPath}
        with self._lock:
            if path not in self._cache:
                while len(self._cache) >= self.cache_size:
                    self._cache.popitem(last=False)
                self._cache[path] = slide
        return slide
 def _get_deepzoom_wrapper(self,
                           original_file_source,
                           file_mimetype,
                           tile_size=None):
     os_wrapper = self._get_openslide_wrapper(original_file_source,
                                              file_mimetype)
     if os_wrapper:
         return DeepZoomGenerator(os_wrapper,
                                  **self._get_deepzoom_config(tile_size))
     else:
         return None