Esempio n. 1
0
 def _parse_pairs(self, txtfile):
     pairs = []
     for x in readcsv(os.path.join(self.lfwdir, txtfile), separator='\t'):
         if len(x) == 3:
             pairs.append(
                 (ImageCategory(category=x[0],
                                filename=os.path.join(
                                    self.lfwdir, x[0],
                                    '%s_%04d.jpg' % (x[0], int(x[1])))),
                  ImageCategory(category=x[0],
                                filename=os.path.join(
                                    self.lfwdir, x[0],
                                    '%s_%04d.jpg' % (x[0], int(x[2]))))))
         elif len(x) == 4:
             pairs.append(
                 (ImageCategory(category=x[0],
                                filename=os.path.join(
                                    self.lfwdir, x[0],
                                    '%s_%04d.jpg' % (x[0], int(x[1])))),
                  ImageCategory(category=x[2],
                                filename=os.path.join(
                                    self.lfwdir, x[2],
                                    '%s_%04d.jpg' % (x[2], int(x[3]))))))
         else:
             pass
     return pairs
Esempio n. 2
0
 def _parse_cls(self, imageset='train'):
     """ImageNet Classification, imageset = {train, val}"""
     import xmltodict
     if imageset == 'train':
         imagesetfile = 'train_cls.txt'
     elif imageset == 'val':
         imagesetfile = 'val.txt'
     else:
         raise ValueError('unsupported imageset')
     csv = readcsv(os.path.join(self.datadir, 'ImageSets', 'CLS-LOC',
                                imagesetfile),
                   separator=' ')
     for (subpath, k) in csv:
         xmlfile = '%s.xml' % os.path.join(self.datadir, 'Annotations',
                                           'CLS-LOC', imageset, subpath)
         imfile = '%s.JPEG' % os.path.join(self.datadir, 'Data', 'CLS-LOC',
                                           imageset, subpath)
         if os.path.exists(xmlfile):
             d = xmltodict.parse(open(xmlfile, 'r').read())
             objlist = d['annotation']['object'] if islist(
                 d['annotation']['object']) else [
                     d['annotation']['object']
                 ]
             yield ImageCategory(filename=imfile,
                                 category=objlist[0]['name'])
         else:
             yield ImageCategory(filename=imfile,
                                 category=filepath(subpath))
Esempio n. 3
0
 def videos(self, subject):
     videos = {}
     for d in dirlist(os.path.join(self.ytfdir, 'frame_images_DB',
                                   subject)):
         k_videoindex = filetail(d)
         videos[k_videoindex] = []
         for f in imlist(d):
             videos[k_videoindex].append(
                 ImageCategory(filename=f, category=subject))
         videos[k_videoindex] = sorted(videos[k_videoindex],
                                       key=lambda im: im.filename())
     return videos
Esempio n. 4
0
    def dataset(self):
        # Return json or CSV file containing dataset description
        categorydir = os.path.join(self.datadir, '256_ObjectCategories')

        imlist = []
        for (idx_category, category) in enumerate(os.listdir(categorydir)):
            imdir = os.path.join(categorydir, category)
            for im in os.listdir(imdir):
                imlist.append(
                    ImageCategory(filename=os.path.join(
                        categorydir, category, im),
                                  category=category))

        return imlist
Esempio n. 5
0
 def dataset(self):
     return [
         ImageCategory(category=s, filename=f) for s in self.subjects()
         for f in imlist(os.path.join(self.lfwdir, s))
     ]
Esempio n. 6
0
 def subject_images(self, subject):
     """List of Images of a subject"""
     fnames = imlist(os.path.join(self.lfwdir, subject))
     return [ImageCategory(category=subject, filename=f) for f in fnames]
Esempio n. 7
0
def _test_image_fileformat(imgfile):
    # Filename object
    im = ImageDetection(filename=imgfile, xmin=100, ymin=100, bbwidth=700, height=1000, category='face')
    print('[test_image.image]["%s"]:  Image __desc__: %s' % (im, imgfile))
    im.crop()
    print('[test_image.image]["%s"]:  Image __desc__: %s' % (im, imgfile))
    print('[test_image.image]["%s"]:  Filename: PASSED' % imgfile)

    # Clone
    im = Image(filename=imgfile).load()
    imb = im
    im._array = im._array + 1  # modify array
    np.testing.assert_array_equal(imb.numpy(), im.numpy())  # share buffer
    imc = im.clone()
    np.testing.assert_array_equal(imc.numpy(), imb.numpy())  # share buffer
    imc._array = imc._array + 2  # modify array
    assert np.any(imc.numpy() != imb.numpy())  
    imc = im.clone(flushforward=True)
    assert(imc._array is None and im._array is not None)  
    imc = im.clone(flushbackward=True)
    assert(im._array is None and imc._array is not None)  
    imc = im.clone(flush=True)
    assert(im._array is None and imc._array is None)
    print('[test_image.image]["%s"]:  Image.clone: PASSED' % imgfile)

    # Downgrade
    im = ImageDetection(filename=imgfile, xmin=100, ymin=100, bbwidth=700, height=1000, category='face')    
    imd = im.detection()
    assert imd.xywh() == im.boundingbox().xywh()
    imd = im.image()
    assert imd.shape() == im.shape()
    print('[test_image.image]["%s"]:  ImageDetection downgrade  PASSED' % imgfile)    
    
    # Saveas
    im = Image(filename=imgfile).load()
    f = temppng()
    assert im.saveas(f) == f and os.path.exists(f)
    print('[test_image.image]["%s"]:  Image.saveas: PASSED' % imgfile)

    # Stats
    im = Image(filename=imgfile).load().stats()
    print('[test_image.image]["%s"]:  Image.stats: PASSED' % imgfile)

    # Resize
    f = temppng()
    im = Image(filename=imgfile).load().resize(cols=16,rows=8).saveas(f)
    assert Image(filename=f).shape() == (8,16)
    assert Image(filename=f).width() == 16
    assert Image(filename=f).height() == 8
    im = Image(filename=imgfile).load().resize(16,8).saveas(f)
    assert Image(filename=f).shape() == (8,16)
    assert Image(filename=f).width() == 16
    assert Image(filename=f).height() == 8
    im = Image(filename=imgfile).load()
    (h,w) = im.shape()
    im = im.resize(rows=16)
    assert im.shape() == (16,int((w / float(h)) * 16.0))
    print('[test_image.image]["%s"]:  Image.resize: PASSED' % imgfile)

    # Rescale
    f = temppng()
    im = Image(filename=imgfile).load().resize(rows=8).saveas(f)
    assert Image(filename=f).height() == 8
    im = Image(filename=imgfile).load().resize(cols=8).saveas(f)
    assert Image(filename=f).width() == 8
    im = Image(filename=imgfile).load().maxdim(256).saveas(f)
    assert np.max(Image(filename=f).shape()) == 256
    print('[test_image.image]["%s"]:  Image.rescale: PASSED' % imgfile)

    # GIF
    im = Image(url=gifurl)
    im.download(verbose=True)
    assert im.shape() == (200,200)
    print('[test_image.image]["%s"]:  GIF: PASSED' % imgfile)

    # Transparent PNG
    im = Image(url=pngurl)
    im.load(verbose=True)
    assert im.colorspace() == 'rgba'
    print('[test_image.image]["%s"]:  PNG: PASSED' % imgfile)

    # Image colorspace conversion
    im = Image(filename=imgfile).resize(200,200)
    print(im.rgb()) 
    assert im.colorspace() == 'rgb'
    assert(im.shape() == (200,200) and im.channels() == 3)
    assert im.array().dtype == np.uint8
    
    print(im.luminance()) 
    assert im.colorspace() == 'lum'   
    assert(im.shape() == (200,200) and im.channels() == 1)
    assert im.array().dtype == np.uint8
    
    print(im.bgr())
    assert im.colorspace() == 'bgr'    
    assert(im.shape() == (200,200) and im.channels() == 3)
    assert im.array().dtype == np.uint8
    
    print(im.rgba())
    assert im.colorspace() == 'rgba'    
    assert(im.shape() == (200,200) and im.channels() == 4)
    assert im.array().dtype == np.uint8
    
    print(im.hsv())
    assert im.colorspace() == 'hsv'    
    assert(im.shape() == (200,200) and im.channels() == 3)
    assert im.array().dtype == np.uint8
    
    print(im.bgra()) 
    assert im.colorspace() == 'bgra'   
    assert(im.shape() == (200,200) and im.channels() == 4)
    assert im.array().dtype == np.uint8
    
    print(im.gray()) 
    assert im.colorspace() in ['grey', 'gray']
    assert(im.shape() == (200,200) and im.channels() == 1)
    assert im.array().dtype == np.float32
    
    print(im.float())
    assert im.colorspace() == 'float'       
    assert(im.shape() == (200,200) and im.channels() == 1)
    assert im.array().dtype == np.float32
    
    print('[test_image.image]["%s"]:  Image conversion: PASSED' % imgfile)
    im = Image(filename=imgfile).load().grey()
    assert im.colorspace() == 'grey'
    assert im.max() == 1.0
    print('[test_image.image]["%s"]:  Greyscale image conversion: PASSED' % imgfile)

    # Crops
    imorig = Image(filename=imgfile).load().lum()
    (H,W) = imorig.shape()
    im = imorig.clone().maxsquare()
    assert im.shape() == (np.maximum(W,H), np.maximum(W,H)) and imorig.array()[0,0] == im.array()[0,0]
    im = imorig.clone().minsquare()    
    assert im.shape() == (np.minimum(W,H), np.minimum(W,H)) and imorig.array()[0,0] == im.array()[0,0]
    im = imorig.clone().centersquare()
    (xo,yo) = imorig.centerpixel() 
    (x,y) = im.centerpixel()
    assert im.shape() == (np.minimum(W,H), np.minimum(W,H)) and imorig.array()[yo,xo] == im.array()[y,x]
    print('[test_image.image]["%s"]:  crops PASSED' % imgfile)

    # Pixel operations
    im = Image(filename=imgfile).load()
    im.min()
    im.max()
    im.mean()
    im.intensity()
    im.saturate(0, 0.5)
    im.mat2gray()
    im.gain(1)
    im.bias(2)
    print('[test_image.image]["%s"]:  greylevel transformations  PASSED' % imgfile)

    # Image conversion
    im = Image(filename=imgfile).load()
    im.pil()
    im.numpy()
    im.html()
    im.torch()
    print('[test_image.image]["%s"]:  image conversions  PASSED' % imgfile)

    # Image colormaps
    im = ImageDetection(filename=imgfile, xmin=100, ymin=100, bbwidth=200, bbheight=200, category='face').crop()
    im.rgb().jet().bone().hot().rainbow()
    print('[test_image.image]["%s"]:  Image colormaps: PASSED' % imgfile)

    # Image exporter
    im.dict()
    print('[test_image.image]["%s"]:  dictionary: PASSED' % imgfile)
    
    # Image category
    im = ImageCategory(filename=imgfile, category='face')
    assert im.load().category() == 'face'
    print('[test_image.image]["%s"]:  ImageCategory constructor PASSED' % imgfile)
    assert ImageCategory(category='1') == ImageCategory(category='1')
    assert ImageCategory(category='1') != ImageCategory(category='2')
    print('[test_image.image]["%s"]:  ImageCategory equivalence PASSED' % imgfile)
    assert ImageCategory(category='1').category() == '1'
    assert ImageCategory(label='1').label() == '1'    
    assert not ImageCategory(category='1').category == '2'
    assert ImageCategory(category='1').category('2').label() == '2' 
    im.score(1.0)
    im.probability(1.0)
    try:
        ImageCategory(category='1', label='2')
        Failed()
    except Failed:
        raise
    except:
        pass
    print('[test_image.image]["%s"]:  ImageCategory category conversion PASSED' % imgfile)    
    
    # Random images
    im = vipy.image.RandomImage(128,256)
    assert im.shape() == (128, 256)
    print('[test_image.image]["%s"]:  RandomImage PASSED' % imgfile)
    im = vipy.image.RandomImageDetection(128,256)
    assert im.clone().crop().width() == im.bbox.imclipshape(W=256,H=128).width()
    print('[test_image.image]["%s"]:  RandomImageDetection PASSED' % imgfile)

    d = vipy.image.RandomImageDetection(128,256).dict()
    assert isinstance(d, dict)
    print('[test_image.image]["%s"]:  dict PASSED' % imgfile)

    # Map
    im = vipy.image.RandomImage(128,256)
    im2 = im.clone().map(lambda img: np.float32(img)+1.0)
    assert np.allclose(np.float32(im.array())+1.0, im2.array())
    print('[test_image.image]["%s"]:  map PASSED' % imgfile)

    # interpolation 
    im = vipy.image.RandomImage(128,256)
    im.resize(256,256, interp='bilinear')
    im.resize(256,256, interp='bicubic')
    im.resize(256,256, interp='nearest')
    try:
        im.resize(256,256, interp='somethingelse')        
        Failed()
    except Failed:
        raise
    except:
        pass
    print('[test_image.image]["%s"]:  interpolation PASSED' % imgfile)