def subjectset(self, wordnetid): """Iterator for single subject""" assert wordnetid in self.wordnetid_to_name().keys( ), 'Invalid wordnetid "%s"' % wordnetid d = os.path.join(self.datadir, wordnetid) for f in imlist(d): yield ImageDetection(filename=f, category=filebase(d))
def dataset(self): categorydir = LABELS imlist = [] for (idx_category, category) in enumerate(categorydir): imdir = os.path.join(self.datadir, SUBDIR, category) for filename in os.listdir(imdir): if isjpg(filename) and not filename.startswith('.'): # Write image im = os.path.join(self.datadir, SUBDIR, category, filename) # Write detections gtfile = os.path.join( self.datadir, SUBDIR, category, os.path.splitext(os.path.basename(filename))[0] + '_' + category.lower() + '.groundtruth') if not os.path.isfile(gtfile): gtfile = os.path.join( self.datadir, SUBDIR, category, os.path.splitext(os.path.basename(filename))[0] + '_' + category.lower() + 's.groundtruth') # plural hack for line in open(gtfile, 'r'): if line.strip() == '': continue (xmin, ymin, xmax, ymax) = line.strip().split() imlist.append( ImageDetection(filename=im, category=category, xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax)) return imlist
def _parse_loc(self, imageset='train'): """ImageNet localization, imageset = {train, val}""" import xmltodict if imageset == 'train': imagesetfile = 'train_loc.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 (path, k) in csv: xmlfile = '%s.xml' % os.path.join(self.datadir, 'Annotations', 'CLS-LOC', imageset, path) d = xmltodict.parse(open(xmlfile, 'r').read()) imfile = '%s.JPEG' % os.path.join(self.datadir, 'Data', 'CLS-LOC', imageset, path) objlist = d['annotation']['object'] if islist( d['annotation']['object']) else [d['annotation']['object']] for obj in objlist: yield ImageDetection(filename=imfile, category=obj['name'], xmin=int(obj['bndbox']['xmin']), ymin=int(obj['bndbox']['ymin']), xmax=int(obj['bndbox']['xmax']), ymax=int(obj['bndbox']['ymax']))
def dataset(self): """Return a generator to iterate over dataset""" for d in dirlist(os.path.join(self.datadir, 'images')): for f in imlist(d): im = ImageDetection(filename=f, category=filebase(d)) im = im.boundingbox(xmin=float(im.width() - 256) / 2.0, ymin=float(im.height() - 256.0) / 2.0, xmax=256.0 + ((im.width() - 256.0) / 2.0), ymax=256.0 + ((im.height() - 256.0) / 2.0)) im = im.boundingbox(dilate=0.875) # central 224x224 yield im
def parse(self): """ Return a list of ImageDetections for all URLs in facescrub """ imset = [] imdir = remkdir(os.path.join(self._datadir, 'images')) csv_actors = readcsv(os.path.join(self._datadir, 'facescrub_actors.txt'), separator='\t') for (subjectname, imageid, faceid, url, bbox, sha256) in csv_actors[1:]: categoryname = subjectname.replace(' ', '_') (xmin, ymin, xmax, ymax) = bbox.split(',') imset.append( ImageDetection(url=url, filename=os.path.join( imdir, '%s_%s.jpg' % (categoryname, imageid)), category=categoryname, xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, attributes={'GENDER': 'male'})) csv_actresses = readcsv(os.path.join(self._datadir, 'facescrub_actresses.txt'), separator='\t') for (subjectname, imageid, faceid, url, bbox, sha256) in csv_actresses[1:]: categoryname = subjectname.replace(' ', '_') (xmin, ymin, xmax, ymax) = bbox.split(',') imset.append( ImageDetection(url=url, filename=os.path.join( imdir, '%s_%s.jpg' % (categoryname, imageid)), category=categoryname, xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, attributes={'GENDER': 'female'})) return imset
def take(self, n, wordnetid=None): """Randomly select n images from the dataset, or n images of a given subjectid""" subjectid = np.random.choice( self.subjects(), n) if wordnetid is None else [wordnetid] * n takelist = [] for s in subjectid: d = os.path.join(self.datadir, s) f = np.random.choice(imlist(d), 1)[0] im = ImageDetection(filename=f).category(filebase(d)) takelist.append(im) return takelist
def take_per_subject(self, n): """Randomly select n images per subject from the dataset""" subjectid = self.subjects() takelist = [] for s in subjectid: d = os.path.join(self.datadir, s) for k in range(0, n): f = np.random.choice(imlist(d), 1)[0] im = ImageDetection(filename=f).category(filebase(d)) takelist.append(im) return takelist
def take(self, n): S = np.random.choice(self.subjects(), n) takelist = [] for d in dirlist(os.path.join(self.datadir, 'images')): if filebase(d) in S: f = np.random.choice(imlist(d), 1)[0] im = ImageDetection(filename=f, category=filebase(d)) im = im.boundingbox(xmin=float(im.width() - 256) / 2.0, ymin=float(im.height() - 256.0) / 2.0, xmax=256.0 + ((im.width() - 256.0) / 2.0), ymax=256.0 + ((im.height() - 256.0) / 2.0)) im = im.boundingbox(dilate=0.875) # central 224x224 takelist.append(im) return takelist
def dataset(self): """Return a generator to iterate over dataset""" SCHEMA = [ 'id', 'url', 'left', 'top', 'right', 'bottom', 'pose', 'detection_score', 'curation' ] for f in txtlist(os.path.join(self.datadir, 'files')): for r in readcsv(f, separator=' '): im = ImageDetection(url=r[2], category=filebase(f), xmin=float(r[3]), ymin=float(r[4]), xmax=float(r[5]), ymax=float(r[6]), attributes=dict(zip(SCHEMA, r))) yield im
def frontalset(self, n_frontal=1): # http://www.robots.ox.ac.uk/~vgg/data/vgg_face2/meta/test_posetemp_imglist.txt assert (n_frontal >= 1 and n_frontal <= 10) assert os.path.exists( os.path.join(self.datadir, 'test_posetemp_imglist.txt') ), 'Download "test_posetemp_imglist.txt" from (http://www.robots.ox.ac.uk/~vgg/data/vgg_face2/meta/test_posetemp_imglist.txt) to "%s"' % self.datadir d = groupbyasdict([ x.strip().split('/') for x in readlist( os.path.join(self.datadir, 'test_posetemp_imglist.txt')) ], lambda v: v[0]) d_subjectid_to_frontallist = { k: [os.path.join(self.datadir, k, y[1]) for y in v[0:n_frontal]] for (k, v) in d.items() } # first and second set of five are frontal for (k, v) in d_subjectid_to_frontallist.items(): for f in v: yield ImageDetection(filename=f).category(k)
def parse(self, subject): """Parse youtubefaces into a list of ImageDetections""" # Write images and annotations # The data in this file is in the following format: # filename,[ignore],x,y,width,height,[ignore],[ignore] # where: # x,y are the center of the face and the width and height are of the rectangle that the face is in. # For example: # $ head -3 Richard_Gere.labeled_faces.txt # Richard_Gere\3\3.618.jpg,0,262,165,132,132,0.0,1 # Richard_Gere\3\3.619.jpg,0,260,164,131,131,0.0,1 # Richard_Gere\3\3.620.jpg,0,261,165,129,129,0.0,1 imlist = [] categorydir = os.path.join(self.ytfdir, 'frame_images_DB') for i, infilename in enumerate( glob('%s/*labeled_faces.txt' % categorydir)): print('[vipy.dataset.youtubefaces:] parsing "%s" ' % infilename) with open(infilename, 'r') as infile: for line in infile: (imname, ignore, x_ctr, y_ctr, w, h, ignore, ignore) = line.split(',') imname = imname.replace('\\', '/') category = imname.split('/')[0] xmin = int(x_ctr) - int(float(w) / 2.0) ymin = int(y_ctr) - int(float(h) / 2.0) xmax = int(xmin) + int(w) ymax = int(ymin) + int(h) p = os.path.join(categorydir, imname) (category, videoid, filename) = imname.split('/') mediaID = '%s_%s' % (category, videoid) imlist.append( ImageDetection(filename=p, category=category, xmin=int(xmin), ymin=int(ymin), xmax=int(xmax), ymax=int(ymax)).setattribute( 'MEDIA_ID', str(mediaID))) # Done! self._parsed = imlist return imlist
def tinyset(self, size=1000): """Return the first (size) image objects in the dataset""" outlist = [] imglist = np.random.permutation( [f[0] for f in readcsv(self._imagelist())]) for (k, f) in enumerate(imglist): print('[megaface.dataset][%d/%d]: importing "%s"' % (k, size, f)) A = self._attributes(os.path.join(self.datadir, f)) outlist = outlist + [ ImageDetection(filename=os.path.join(self.datadir, f), category=filebase(f)).boundingbox( xmin=A['bounding_box']['x'], ymin=A['bounding_box']['y'], width=A['bounding_box']['width'], height=A['bounding_box']['height']) ] if k > size: break return outlist
def dataset(self): csvfile = os.path.join(self.datadir, 'aflw.csv') with open(csvfile, 'r') as f: for x in f.readline().split(','): if x[0][0] != '#': im = ImageDetection( filename=os.path.join(self.datadir, x[0]), category='face', xmin=float(x[1]) if len(x[1]) > 0 else float('nan'), ymin=float(x[2]) if len(x[2]) > 0 else float('nan'), xmax=float(x[1]) + float(x[3]) if ((len(x[1]) > 0) and (len(x[3]) > 0)) else float('nan'), ymax=float(x[2]) + float(x[4]) if ((len(x[2]) > 0) and (len(x[4]) > 0)) else float('nan'), attributes={k: v for (k, v) in zip(SCHEMA, x)}) # Parse row yield im
def take(self, n): """Randomly select n frames from dataset""" takelist = [] SCHEMA = [ 'id', 'url', 'left', 'top', 'right', 'bottom', 'pose', 'detection_score', 'curation' ] for csvfile in np.random.choice( txtlist(os.path.join(self.datadir, 'data')), n): csv = readcsv(csvfile, separator=' ') r = csv[np.random.randint(1, len(csv))] # not including header im = ImageDetection(url=r[2], category=filebase(csvfile), xmin=float(r[3]), ymin=float(r[4]), xmax=float(r[5]), ymax=float(r[6]), attributes=dict(zip(SCHEMA, r))) takelist.append(im) return takelist
def _triplet_montage(wb, matelist, nonmatelist, probelist, outfile, f_saliency=None): X_mate = [wb.net.encode(wb.net.preprocess(im.pil())) for im in matelist] X_nonmate = [wb.net.encode(wb.net.preprocess(im.pil())) for im in nonmatelist] # Create saliency for each matrix entry, overwrite probelist for (i, (x_mate, im_mate)) in enumerate(zip(X_mate, matelist)): for (j, (x_nonmate, im_nonmate)) in enumerate(zip(X_nonmate, nonmatelist)): wb.net.set_triplet_classifier(x_mate, x_nonmate) if f_saliency is not None: img_saliency = f_saliency(probelist[i][j]) probelist[i][j].buffer(img_saliency) # Montage imlist = [ImageDetection(xmin=0, ymin=0, xmax=256, ymax=256).buffer(np.uint8(np.zeros( (256,256,3) )))] imlist = imlist + nonmatelist for (im_mate, im_matedprobes) in zip(matelist, probelist): imlist.append(im_mate) imlist = imlist + im_matedprobes img_montage = vipy.visualize.montage(imlist, 112, 112, rows=len(matelist)+1, cols=len(nonmatelist)+1, grayscale=False, skip=False, border=1, crop=False) return vipy.util.imwrite(img_montage, outfile)
def test_batch(): imb = vipy.batch.Batch([ ImageDetection(filename=rgbfile, category='face', bbox=vipy.geometry.BoundingBox(0, 0, 100, 100)) for k in range(0, 100) ]) imb.crop() assert len(imb) == 100 assert np.array(imb.torch()).shape == (100, 3, 100, 100) imb.grey() for im in imb: assert im.colorspace() == 'grey' res = imb.map(lambda im: np.sum(im.array()) * 0) assert np.sum(res) == 0 v = vipy.video.RandomScene() b = vipy.batch.Batch(v, n_processes=2) res = b.map(lambda v, k: v[k], args=[(k, ) for k in range(0, len(v))]) assert isinstance(res[0], vipy.image.Scene) print('[test_image.batch]: batch PASSED')
def tinyset(self, size=1000): """Return the first (size) image objects in the trainset""" outlist = [] if not os.path.exists( os.path.join(self.datadir, 'Megaface_Challenge_1M_disjoint_LOOSE.csv')): print('[MF2.tinyset]: generating csv file for MF2') self._trainset() imglist = np.random.permutation([ f[0] for f in readcsv( os.path.join(self.datadir, 'Megaface_Challenge_1M_disjoint_LOOSE.csv')) ]) for (k, f) in enumerate(imglist): print('[MF2.tinyset][%d/%d]: importing "%s"' % (k, size, f)) outlist = outlist + [ ImageDetection(filename=os.path.join(self.datadir, f), category=filebase(f)) ] if k > size: break return outlist
def test_imagedetection(): # Constructors im = ImageDetection() im.__repr__() assert im.category() is None and im.bbox.xywh() == (0,0,0,0) print('[test_image.imagedetection]: Empty ImageDetection constructor PASSED') im = ImageDetection(category='test', xmin=0, ymin=0, width=200, height=200) print('[test_image.imagedetection]: Empty filename ImageDetection constructor PASSED') print('[test_image.imagedetection]: Empty bounding box ImageDetection constructor PASSED') im = ImageDetection(filename=rgbfile, category='face', bbox=BoundingBox(0,0,100,100)) try: im = ImageDetection(filename=rgbfile, category='face', bbox='a_bad_type') raise Failed() except Failed: raise except: print('[test_image.imagedetection]: bounding box constructor PASSED') im = ImageDetection(filename=rgbfile, category='face', xmin=-1, ymin=-2, ymax=10, xmax=20) try: im = ImageDetection(filename=rgbfile, category='face', xmin='a_bad_type', ymin=-2, ymax=10, xmax=20) raise Failed() except Failed: raise except: print('[test_image.imagedetection]: (xmin,ymin,xmax,ymax) bounding box constructor PASSED') im = ImageDetection(filename=rgbfile, xmin=100, ymin=100, bbwidth=200, bbheight=-200, category='face') assert im.invalid() print('[test_image.imagedetection]: invalid box: PASSED') try: im.crop() raise Failed() except Failed: raise except: print('[test_image.imagedetection]: invalid box crop: PASSED') im = ImageDetection(filename=rgbfile, xmin=100000, ymin=100000, bbwidth=200, bbheight=200, category='face') print('[test_image.imagedetection]: invalid imagebox: PASSED') # boundingbox() methods im = ImageDetection(filename=rgbfile, category='face', xmin=-1, ymin=-2, ymax=10, xmax=20) assert im.boundingbox() == BoundingBox(-1, -2, 20, 10) assert not im.boundingbox(xmin=0, ymin=0, width=10, height=20) == BoundingBox(0,0,width=10, height=20) assert im.boundingbox(xmin=0, ymin=0, width=10, height=20).bbox == BoundingBox(0,0,width=10, height=20) assert im.boundingbox(bbox=BoundingBox(1,2,3,4)).bbox == BoundingBox(1,2,3,4) assert im.boundingbox(bbox=BoundingBox(xcentroid=1,ycentroid=2,width=3,height=4)).bbox == BoundingBox(xcentroid=1,ycentroid=2,width=3,height=4) assert im.boundingbox(xmin=-1, ymin=-2, ymax=10, xmax=20).boundingbox(dilate=1.5).bbox == BoundingBox(xmin=-1, ymin=-2, ymax=10, xmax=20).dilate(1.5) assert im.boundingbox(xmin=-1, ymin=-2, ymax=10, xmax=20).boundingbox(dilate=1.5).bbox == BoundingBox(xmin=-1, ymin=-2, ymax=10, xmax=20).dilate(1.5) try: im.boundingbox(xmin=1) raise Failed() except Failed: raise except: print('[test_image.imagedetection]: boundingbox() methods PASSED') # Crop im = ImageDetection(filename=rgbfile, xmin=100, ymin=100, bbwidth=200, bbheight=200, category='face').crop() assert(im.shape() == (200,200)) assert(im.bbox.width() == im.width() and im.bbox.height() == im.height() and im.bbox.xmin() == 0 and im.bbox.ymin() == 0) im = ImageDetection(filename=rgbfile) (H,W) = im.shape() im = im.boundingbox(xmin=0, ymin=0, width=W, height=H).crop() assert(im.shape() == (H,W)) print('[test_image.imagedetection]: crop PASSED') # Rescale im = ImageDetection(filename=rgbfile, xmin=100, ymin=100, bbwidth=200, bbheight=200, category='face') im = im.rescale(0.5) assert(im.bbox.width() == 100 and im.bbox.height() == 100) print('[test_image.imagedetection]: rescale PASSED') # Resize imorig = ImageDetection(filename=rgbfile, xmin=100, ymin=100, width=200, height=300, category='face') im = imorig.clone().resize(cols=int(imorig.width() // 2.0), rows=int(imorig.height() // 2.0)) assert(im.bbox.width() == 100 and im.bbox.height() == 150) (H,W) = imorig.shape() im = imorig.clone().resize(cols=100) assert(im.crop().width() == int(np.round(200*(100.0/W)))) print('[test_image.imagedetection]: resize PASSED') # Isinterior im = ImageDetection(array=np.zeros((10,20), dtype=np.float32), xmin=100, ymin=100, xmax=200, ymax=200, category='face') assert not im.isinterior() assert not im.isinterior(20,10) im = ImageDetection(array=np.zeros((10,20), dtype=np.float32), xmin=0, ymin=0, width=20, height=10) assert im.isinterior() print('[test_image.imagedetection]: interior PASSED') # Fliplr img = np.random.rand(10,20).astype(np.float32) im = ImageDetection(array=img, xmin=0, ymin=0, xmax=5, ymax=10) assert im.isinterior() assert np.allclose(im.clone().fliplr().crop().array(), np.fliplr(im.crop().array())) print('[test_image.imagedetection]: fliplr PASSED') # Square crops img = np.random.rand(10,20).astype(np.float32) im = ImageDetection(array=img, xmin=0, ymin=0, xmax=5, ymax=10) imorig = im.clone() (x,y,w,h) = im.bbox.xywh() (xc,yc) = im.boundingbox().centroid() # box centroid assert im.clone().minsquare().bbox.shape() == (10, 5) assert im.clone().minsquare().shape() == (10, 10) assert im.clone().minsquare().bbox.centroid() == (xc, yc) assert im.clone().maxsquare().bbox.shape() == (10, 5) assert im.clone().maxsquare().shape() == (20,20) assert im.clone().maxsquare().bbox.centroid() == (xc,yc) assert im.clone().centersquare().shape() == (10,10) assert im.clone().centersquare().boundingbox().xywh() == (-5,0,5,10) img = np.random.rand(20,10,3).astype(np.float32) assert ImageDetection(array=img, xmin=0, ymin=0, width=10, height=10).minsquare().crop().shape() == (10,10) img = np.random.rand(21,9,3).astype(np.float32) assert ImageDetection(array=img, xmin=0, ymin=0, width=9, height=21).centersquare().crop().shape() == (9,9) img = np.random.rand(10,11,3).astype(np.float32) assert ImageDetection(array=img, xmin=0, ymin=0, width=11, height=10).centersquare().crop().shape() == (10,10) print('[test_image.imagedetection]: minsquare PASSED') print('[test_image.imagedetection]: maxsquare PASSED') print('[test_image.imagedetection]: centersquare PASSED') # Dilate im = ImageDetection(array=img, xmin=1, ymin=0, width=3, height=4) assert im.clone().dilate(2).bbox == BoundingBox(centroid=im.bbox.centroid(), width=6, height=8) and img.shape[0] == im.height() print('[test_image.imagedetection]: dilate PASSED') # Pad img = np.random.rand(20,40).astype(np.float32) im = ImageDetection(array=img, xmin=0, ymin=0, width=40, height=20) assert np.allclose(im.clone().zeropad(10,10).crop().array(), img) and (im.clone().zeropad(10,20).shape() == (20 + 20 * 2, 40 + 10 * 2)) img = np.random.rand(20,40).astype(np.float32) im = ImageDetection(array=img, xmin=0, ymin=0, width=40, height=20) assert np.allclose(im.clone().meanpad(10,10).crop().array(), img) and (im.clone().meanpad(10,20).shape() == (20 + 20 * 2, 40 + 10 * 2)) imorig = ImageDetection(array=img, xmin=0, ymin=0, width=40, height=20) im = imorig.clone().meanpad( (0,10), (0,20) ) assert np.allclose(imorig.clone().meanpad( (0,10), (0,20) ).crop().array(), img) and (imorig.clone().meanpad( (0,10), (0,20) ).shape() == (20 + 20, 10 + 40)) and img[0,0] == im.array()[0,0] and im.array()[-1,-1] != 0 im = imorig.clone().zeropad( (0,10), (0,20) ) assert np.allclose(imorig.clone().zeropad( (0,10), (0,20) ).crop().array(), img) and (imorig.clone().zeropad( (0,10), (0,20) ).shape() == (20 + 20, 10 + 40)) and img[0,0] == im.array()[0,0] and im.array()[-1,-1] == 0 im = imorig.clone().zeropadlike(100, 110) assert im.width() == 100 and im.height() == 110 print('[test_image.imagedetection]: pad PASSED') # imclip img = np.random.rand(20,10,3).astype(np.float32) im = ImageDetection(array=img, xmin=0, ymin=0, width=10, height=20) assert im.clone().imclip().bbox.xywh() == (0,0,10,20) im = ImageDetection(array=img, xmin=0, ymin=0, width=10, height=200) assert im.clone().imclip().bbox.xywh() == (0,0,10,20) im = ImageDetection(array=img, xmin=100, ymin=200, width=10, height=200) im = ImageDetection(array=img, xmin=-1, ymin=-2, width=100, height=200) assert im.clone().imclip().bbox.xywh() == (0,0,10,20) im = ImageDetection(array=img, xmin=1, ymin=1, width=9, height=9) assert im.clone().imclip().bbox.xywh() == (1,1,9,9) print('[test_image.imagedetection]: imclip PASSED') # Setzero img = np.random.rand(20,10,3).astype(np.float32) assert ImageDetection(array=img, xmin=0, ymin=0, width=2, height=3).setzero().crop().sum() == 0 print('[test_image.imagedetection]: setzero PASSED') # Mask assert np.sum(ImageDetection(array=img, xmin=-1, ymin=-2, width=2, height=3).rectangular_mask(10,10)) == 1 assert np.sum(ImageDetection(array=img, xmin=0, ymin=0, width=2, height=3).rectangular_mask(10,10)) == 6 print('[test_image.imagedetection]: mask PASSED') # Dict assert isinstance(ImageDetection(array=img, xmin=0, ymin=0, width=2, height=3).dict(), dict) print('[test_image.imagedetection]: dict PASSED')
def dataset(self): """Return a generator to iterate over dataset""" for d in dirlist(os.path.join(self.datadir)): for f in imlist(d): yield ImageDetection(filename=f).category(filebase(d))
def _parse(self): outlist = [] id2name = {k:v for (k,v) in readcsv(os.path.join(self.datadir, 'names.txt'), separator=' ')} for d in dirlist(self.datadir): outlist = outlist + [ImageDetection(filename=imfile, category=id2name[str(filebase(d))], xmin=13, ymin=13, xmax=250 - 13, ymax=250 - 13) for imfile in imlist(d)] return outlist
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)
def by_subject(self, wordnetid): d = os.path.join(self.datadir, 'images', wordnetid) for f in imlist(d): yield ImageDetection(filename=f, category=filebase(d))
def test_boundingbox(): # Constructors try: bb = BoundingBox() raise Failed() except Failed: raise except: pass try: bb = BoundingBox(xmin=0) raise Failed() except Failed: raise except: pass try: bb = BoundingBox(xmin=0, ymin=0, xcentroid=0, ycentroid=0) raise Failed() except Failed: raise except: pass try: bb = BoundingBox(xmin=0, width=0, xcentroid=0, ycentroid=0) raise Failed() except Failed: raise except: pass print('[test_geometry.boundingbox]: Degenerate constructors: PASSED') str(BoundingBox(xmin=0, ymin=0, width=10, height=10.2)) print('[test_geometry.boundingbox]: __str__ PASSED') BoundingBox(xmin=0, ymin=0, width=10, height=10.2).__repr__() print('[test_geometry.boundingbox]: __repr__ PASSED') bb = BoundingBox(xmin=0, ymin=0, width=10, height=10.2) print('[test_geometry.boundingbox]: (x,y,w,h) constructor: PASSED') bb = BoundingBox(xmin='0', ymin='0.5', width='1E2', height='10.2') print('[test_geometry.boundingbox]: (x,y,w,h) string constructor: PASSED') bb = BoundingBox(xmin='0', ymin='0.5', xmax='1E2', ymax='1000.2') print( '[test_geometry.boundingbox]: (xmin,ymin,xmax,ymax) constructor: PASSED' ) bb = BoundingBox(centroid=('0', 0), width=10, height=10) print( '[test_geometry.boundingbox]: (centroid, width, height) constructor: PASSED' ) assert BoundingBox(centroid=(0, 0), width=10, height=20) == BoundingBox(xmin=-5, ymin=-10, width=10, height=20) assert BoundingBox(xmin=10, ymin=20, xmax=30, ymax=40) != BoundingBox( xmin=-5, ymin=-10, width=10, height=20) print('[test_geometry.boundingbox]: equivalence PASSED') try: bb = BoundingBox(mask=np.zeros((10, 10))) raise Failed() except Failed: raise except: print( '[test_geometry.boundingbox]: Degenerate mask constructor: PASSED') bb = BoundingBox(xmin=0, ymin=0, width=-100, height=0) assert bb.isdegenerate() print('[test_geometry.boundingbox]: Invalid box: PASSED') # Corners bb = BoundingBox(xmin=10, ymin=20, width=100, height=200) assert bb.xmin() == 10 assert bb.ymin() == 20 assert bb.width() == 100 assert bb.height() == 200 assert bb.xmax() == 10 + 100 assert bb.ymax() == 20 + 200 assert bb.upperleft() == (10, 20) assert bb.upperright() == (10 + 100, 20) assert bb.bottomleft() == (10, 20 + 200) assert bb.bottomright() == (10 + 100, 20 + 200) print('[test_geometry.boundingbox.corners]: PASSED') bb = BoundingBox(xmin=0, ymin=0, width=100, height=100) bb.setheight(20) bb.setwidth(40) assert bb.to_xywh() == (30.0, 40.0, 40.0, 20) print('[test_geometry.boundingbox.setheight]: PASSED') print('[test_geometry.boundingbox.setwidth]: PASSED') bb = BoundingBox(centroid=(10, 10), width=1, height=1) assert bb.centroid() == (10, 10) print('[test_geometry.boundingbox.centroid]: PASSED') # Intersection bb1 = BoundingBox(xmin=0, ymin=0, width=100, height=100) bb2 = BoundingBox(xmin=50, ymin=50, width=100, height=100) bb1.intersection(bb2) assert bb1.to_xywh() == (50.0, 50.0, 50.0, 50.0) assert bb1.xywh() == (50.0, 50.0, 50.0, 50.0) print('[test_geometry.boundingbox.intersection]: PASSED') print('[test_geometry.boundingbox.xwyh]: PASSED') bb2 = BoundingBox(xmin=200, ymin=200, width=100, height=100) try: bb1.intersection(bb2) raise Failed() except Failed: raise except: bb1.intersection(bb2, strict=False) print('[test_geometry.boundingbox]: intersection degeneracy: PASSED') # Translation bb = BoundingBox(xmin=0, ymin=0, width=100, height=100) bb.translate(10, 20) assert bb.to_xywh() == (10.0, 20.0, 100.0, 100.0) print('[test_geometry.boundingbox]: translate: PASSED') bb1 = BoundingBox(xmin=0, ymin=0, width=100, height=100) bb2 = BoundingBox(xmin=50, ymin=60, width=100, height=100) bb1.dx(bb2) bb1.dy(bb2) assert bb1.dx(bb2) == 50 and bb1.dy(bb2) == 60 print('[test_geometry.boundingbox.dx]: PASSED') print('[test_geometry.boundingbox.dy]: PASSED') # Dist assert (bb1.dist(bb2) == np.sqrt(50 * 50 + 60 * 60)) print('[test_geometry.boundingbox.dist]: PASSED') # IoU bb1 = BoundingBox(xmin=0, ymin=0, width=100, height=100) bb2 = BoundingBox(xmin=50, ymin=50, width=50, height=50) assert bb1.iou(bb2) == ((50.0 * 50.0) / (100.0 * 100.0)) assert bb1.intersection_over_union(bb2) == ((50.0 * 50.0) / (100.0 * 100.0)) print('[test_geometry.boundingbox.iou]: PASSED') # Union bb1 = BoundingBox(xmin=1, ymin=2, width=3, height=1) bb2 = BoundingBox(xmin=0, ymin=0, width=2, height=5) assert bb1.clone().union(bb2).xywh() == (0, 0, 4, 5) bb3 = BoundingBox(xmin=0, ymin=0, width=2, height=6) assert bb1.clone().union([bb2, bb3]).xywh() == (0, 0, 4, 6) assert bb1.clone().union([]).xywh() == bb1.xywh() print('[test_geometry.boundingbox.union]: PASSED') # Inside bb1 = BoundingBox(xmin=0, ymin=0, width=100, height=100) assert bb1.inside((0, 25)) and not bb1.inside((-10, 0)) and bb1.inside( (99, 99)) print('[test_geometry.boundingbox.inside]: PASSED') # Typecast assert isinstance(bb1.int().xmin(), int) print('[test_geometry.boundingbox.int]: PASSED') # Dilation bb = BoundingBox(xmin=0, ymin=0, width=100, height=100).dilate(2.0) assert bb.width() == 200 and bb.height() == 200 and bb.centroid() == (50, 50) print('[test_geometry.boundingbox.dilate]: PASSED') bb = BoundingBox(xmin=0, ymin=0, width=100, height=100).dilate_height(3.0) assert bb.width() == 100 and bb.height() == 300 and bb.centroid() == (50, 50) print('[test_geometry.boundingbox.dilate_height]: PASSED') bb = BoundingBox(xmin=0, ymin=0, width=100, height=100).dilate_width(1.1) assert bb.width() == 110 and bb.height() == 100 and list( np.round(bb.centroid())) == [50, 50] print('[test_geometry.boundingbox.dilate_width]: PASSED') bb = BoundingBox(xmin=1, ymin=1, width=1, height=1).top(1.0).bottom(1.0).left(1.0).right(1.0) assert bb.to_xywh() == (0, 0, 3, 3) print('[test_geometry.boundingbox.top]: PASSED') print('[test_geometry.boundingbox.bottom]: PASSED') print('[test_geometry.boundingbox.left]: PASSED') print('[test_geometry.boundingbox.right]: PASSED') # Transformations bb = BoundingBox(xmin=1, ymin=1, width=1, height=1).rescale(2.5) assert bb.to_xywh() == (2.5, 2.5, 2.5, 2.5) print('[test_geometry.boundingbox.rescale]: PASSED') bb = BoundingBox(xmin=10, ymin=20, width=30, height=40).maxsquare() assert bb.to_xywh() == (5, 20, 40, 40) print('[test_geometry.boundingbox.maxsquare]: PASSED') bb = BoundingBox(xmin=0, ymin=0, width=0, height=0).convexhull( np.array([[0, 0], [100, 100], [50, 50]])) assert bb.to_xywh() == (0, 0, 100, 100) print('[test_geometry.boundingbox.convexhull]: PASSED') img = ImageDetection( array=np.zeros((128, 256), dtype=np.float32)).boundingbox( xmin=10, ymin=20, width=30, height=40).rectangular_mask() im = ImageDetection(array=np.float32(img), colorspace='float').boundingbox(xmin=10, ymin=20, width=30, height=40) assert np.sum(img) == np.sum(im.crop().array()) bb = BoundingBox(xmin=10, ymin=20, width=30, height=40).rot90cw(128, 256) im = ImageDetection(array=np.float32(np.rot90(img, 3)), bbox=bb, colorspace='float') assert np.sum(img) == np.sum(im.crop().array()) print('[test_geometry.boundingbox.rot90cw]: PASSED') bb = BoundingBox(xmin=10, ymin=20, width=30, height=40).rot90ccw(128, 256) im = ImageDetection(array=np.rot90(img, 1), bbox=bb) assert np.sum(img) == np.sum(im.crop().array()) print('[test_geometry.boundingbox.rot90ccw]: PASSED') bb = BoundingBox(xmin=10, ymin=20, width=30, height=40) img = ImageDetection( array=np.zeros((128, 256), dtype=np.float32)).boundingbox( bbox=bb).rectangular_mask() bb = bb.fliplr(img) im = ImageDetection(array=np.fliplr(img), bbox=bb) assert np.sum(img) == np.sum(im.crop().array()) print('[test_geometry.boundingbox.fliplr]: PASSED') assert BoundingBox(xmin=-10, ymin=-10, width=30, height=40).hasoverlap( np.zeros((128, 256), dtype=np.float32)) assert not BoundingBox(xmin=1000, ymin=1000, width=30, height=40).hasoverlap( np.zeros((128, 256), dtype=np.float32)) print('[test_geometry.boundingbox.hasoverlap]: PASSED') assert BoundingBox(xmin=-10, ymin=-10, width=30, height=40).imclip(np.zeros( (128, 256), dtype=np.float32)) == BoundingBox(xmin=0, ymin=0, width=20, height=30) assert BoundingBox(xmin=-10, ymin=-10, width=30, height=40).imclipshape(128, 256) == BoundingBox(xmin=0, ymin=0, width=20, height=30) try: BoundingBox(xmin=-100, ymin=-100, width=30, height=40).imclip(np.zeros((128, 256), dtype=np.float32)) Failed() except Failed: raise except: print('[test_geometry.boundingbox.imclip]: PASSED') assert BoundingBox(xmin=-20, ymin=-10, width=30, height=40).aspectratio() == 30.0 / 40.0 print('[test_geometry.boundingbox.aspectratio]: PASSED') assert BoundingBox(xmin=-20, ymin=-10, width=30, height=40).shape() == (40, 30) print('[test_geometry.boundingbox.shape]: PASSED') assert BoundingBox(xmin=-20, ymin=-10, width=30, height=40).mindimension() == (30) assert BoundingBox(xmin=-20, ymin=-10, width=30, height=40).mindim() == (30) print('[test_geometry.boundingbox.mindimension]: PASSED') print('[test_geometry.boundingbox.mindim]: PASSED') BoundingBox(xmin=-20, ymin=-10, width=30, height=40).dict() print('[test_geometry.boundingbox]: dict PASSED')
def fastset(self): """Return a generator to iterate over dataset""" for d in dirlist(os.path.join(self.datadir, 'images')): for f in imlist(d): im = ImageDetection(filename=f, category=filebase(d)) yield im