Ejemplo n.º 1
0
 def key(self, obj):        
     """Return a cache key (relative path to file in cache) for an object"""
     if isnumpy(obj):
         # Key is byte view sha1 hash with .h5 extension           
         byteview = obj.view(numpy.uint8)
         key = str(hashlib.sha1(byteview).hexdigest()) + '.h5' 
     elif isurl(obj):
         # key is URL filename with an appended hash (for uniqueness)
         p = urlparse.urlsplit(obj)
         urlquery = urlparse.urlunsplit([p[0],p[1],p[2],p[3],None])        
         urlpath = urlparse.urlunsplit([p[0],p[1],p[2],None,None])
         urlhash = self._hash(obj)
         (filename, ext) = splitextension(path.basename(urlpath))
         key = str(urlhash) + str(ext)
     elif os.path.isfile(obj):
         # within cache?
         filebase = obj.split(self.root(),1)
         if len(filebase) == 2:
             # key is subpath within cache
             key = filebase[1][1:]
         else:
             # key is filename with unique appended hash
             (head, tail) = os.path.split(obj)
             (filename, ext) = splitextension(tail)                 
             namehash = hashlib.sha1(tail).hexdigest()                 
             key = filename + '_' + str(namehash[0:7]) + ext
     elif (path.isfile(self.abspath(obj)) or path.isdir(self.abspath(obj))):
         key = obj   # Already a cache key
     elif isstring(obj):
         key = obj   # Use arbitrary string if not file or url
     else:
         raise CacheError('[bobo.cache][ERROR]: Unsupported object for constructing key')
     return key
Ejemplo n.º 2
0
 def __getitem__(self, item):
     if isstring(item):
         # assume that item is a row from a viset csv file
         return ImageDetection(cache=self._cache).parse(item)
     else:
         # Inefficient method for random stream access
         print type(item)
         f = open(self._csvfile, 'r')
         for i in range(item):
             line = f.readline()
             if len(line) == 0:
                 raise IndexError('Invalid index "%d"' % item)  # end of file
         f.close()
         row = line.split()
         return (ImageDetection(row[0], category=row[1], xmin=row[2], ymin=row[3], xmax=row[4], ymax=row[5], cache=self._cache))