Ejemplo n.º 1
0
class Image():
    cachedimage = None
    image = None
    cache = None
    
    def __init__(self, image=None, cache=Cache()):
        if isnumpy(image):
            self.image = image
        elif image is not None:
            self.cachedimage = CachedObject(image, cache)
        self.cache = cache
        
    def __repr__(self):
        if self.image is not None:
            return str('<bobo.image: image=(%d,%d)>' % (self.image.shape[0], self.image.shape[1]))
        else:
            return str('<bobo.image: uri="%s">' % (self.cachedimage.uri))            

    def parse(self, row):
        """Parse a row from a viset csv textfile into image"""
        self.cachedimage = CachedObject(row, cache=self.cache)        
        self.image = None
        return self
    
    def load(self):
        """Load images from cache"""
        if self.image is not None:
            return self.image
        elif self.cachedimage is not None:
            try:
                quietprint('[bobo.image]: loading "%s"'% self.cachedimage.uri, True);                
                self.image = self.cachedimage.load()
            except (httplib.BadStatusLine, urllib2.URLError, urllib2.HTTPError):
                quietprint('[bobo.image][WARNING]: download failed - ignoring image', True);
            except CacheError:
                quietprint('[bobo.image][WARNING]: cache error during download - ignoring image', True);                
            except IOError:
                quietprint('[bobo.image][WARNING]: IO error during download - ignoring image', True);                
            except:
                #raise
                quietprint('[bobo.image][WARNING]: error during download - ignoring image', True);                
                pass
                
            return self.image
        else:
            return None

    def show(self):
        if self.load() is not None:
            imshow(self.image)

    def filename(self):
        return (self.cachedimage.filename())
Ejemplo n.º 2
0
 def __init__(self, image=None, category=None, cache=Cache()):
     if isnumpy(image):
         self.image = image
     elif image is not None:
         self.cachedimage = CachedObject(image, cache)
     self.category = category
     self.cache = cache
Ejemplo n.º 3
0
 def parse(self, row):
     """Parse a row from a viset csv textfile into image and category"""
     rowlist = row.encode('utf-8').split()
     self.category = rowlist[1]
     self.cachedimage = CachedObject(rowlist[0], cache=self.cache)        
     self.image = None
     return self
Ejemplo n.º 4
0
 def __init__(self, image=None, category=None, xmin=None, xmax=None, ymin=None, ymax=None, cache=Cache()):
     if isnumpy(image):
         self.image = image
     elif image is not None:
         self.cachedimage = CachedObject(image, cache)
     self.category = category
     self.cache = cache
     self.xmin = float(xmin)
     self.xmax = float(xmax)
     self.ymin = float(ymin)
     self.ymax = float(ymax)
Ejemplo n.º 5
0
class ImageCategory():
    cachedimage = None
    image = None
    category = None
    cache = None
    
    def __init__(self, image=None, category=None, cache=Cache()):
        if isnumpy(image):
            self.image = image
        elif image is not None:
            self.cachedimage = CachedObject(image, cache)
        self.category = category
        self.cache = cache
        
    def __repr__(self):
        if self.image is not None:
            return str('<bobo.image.imcategory: image=(%d,%d), category="%s">' % (self.image.shape[0], self.image.shape[1], self.category))
        else:
            return str('<bobo.image.imcategory: uri="%s", category="%s">' % (self.cachedimage.uri, self.category))            

    def __eq__(self, other):
        return self.category.lower() == other.category.lower()

    def __ne__(self, other):
        return self.category.lower() != other.category.lower()

    
    def __hash__(self):
        return hash(self.category.lower())
                
    def parse(self, row):
        """Parse a row from a viset csv textfile into image and category"""
        rowlist = row.encode('utf-8').split()
        self.category = rowlist[1]
        self.cachedimage = CachedObject(rowlist[0], cache=self.cache)        
        self.image = None
        return self
    
    def load(self):
        """Load images from cache"""
        if self.image is not None:
            return self.image
        elif self.cachedimage is not None:
            try:
                quietprint('[bobo.image]: loading "%s"'% self.cachedimage.uri, True);                
                self.image = self.cachedimage.load()
                if self.cachedimage.size() < 10000:
                    quietprint('[bobo.image][WARNING]: invalid download size - ignoring image', True);                                    
                    os.remove(self.cachedimage.filename())
                    return None
            except (httplib.BadStatusLine, urllib2.URLError, urllib2.HTTPError):
                quietprint('[bobo.image][WARNING]: download failed - ignoring image', True);
                self.cachedimage.discard()                
            except CacheError:
                quietprint('[bobo.image][WARNING]: cache error during download - ignoring image', True);
                self.cachedimage.discard()                
            except IOError:
                quietprint('[bobo.image][WARNING]: IO error during download - ignoring image', True);
                self.cachedimage.discard()                
            except:
                raise
            return self.image
        else:
            return None

    def iscategory(self, category):
        return (self.category.lower() == category.lower())
                
    def show(self):
        if self.load() is not None:
            imshow(self.image)

    def export(self):
        """List suitable for export"""
        return [os.path.join(self.cache.root(), self.cachedimage.uri), self.category]

    def filename(self):
        return (self.cachedimage.filename())
Ejemplo n.º 6
0
 def parse(self, row):
     """Parse a row from a viset csv textfile into image"""
     self.cachedimage = CachedObject(row, cache=self.cache)        
     self.image = None
     return self
Ejemplo n.º 7
0
class ImageDetection():
    cachedimage = None
    image = None
    category = None
    cache = None
    xmin = None
    xmax = None
    ymin = None
    ymax = None
    
    def __init__(self, image=None, category=None, xmin=None, xmax=None, ymin=None, ymax=None, cache=Cache()):
        if isnumpy(image):
            self.image = image
        elif image is not None:
            self.cachedimage = CachedObject(image, cache)
        self.category = category
        self.cache = cache
        self.xmin = float(xmin)
        self.xmax = float(xmax)
        self.ymin = float(ymin)
        self.ymax = float(ymax)
        
    def __repr__(self):
        if self.image is not None:
            return str('<bobo.image.imdetection: image=(%d,%d), category="%s, bbox=(%d,%d,%d,%d)">' % (self.image.shape[0], self.image.shape[1], self.category, self.xmin,self.ymin,self.xmax,self.ymax))
        else:
            return str('<bobo.image.imdetection: uri="%s", category="%s", bbox=(%d,%d,%d,%d)">' % (self.image.shape[0], self.image.shape[1], self.category, self.xmin,self.ymin,self.xmax,self.ymax))

    def parse(self, row):
        """Parse a row from a viset csv textfile into image and category"""
        rowlist = row.encode('utf-8').split()
        self.category = rowlist[1]
        self.cachedimage = CachedObject(rowlist[0], cache=self.cache)        
        self.image = None
        self.xmin = float(rowlist[2])
        self.ymin = float(rowlist[3])
        self.xmax = float(rowlist[4])
        self.ymax = float(rowlist[5])
        return self
    
    def load(self):
        """Load images from cache"""
        if self.image is not None:
            return self.image
        elif self.cachedimage is not None:
            try:
                quietprint('[bobo.image]: loading "%s"'% self.cachedimage.uri, True);                
                self.image = self.cachedimage.load()
            except (httplib.BadStatusLine, urllib2.URLError, urllib2.HTTPError):
                quietprint('[bobo.image][WARNING]: download failed - ignoring image', True);
            except CacheError:
                quietprint('[bobo.image][WARNING]: cache error during download - ignoring image', True);                
            except IOError:
                quietprint('[bobo.image][WARNING]: IO error during download - ignoring image', True);                
            except:
                raise
                
            return self.image
        else:
            return None

    def iscategory(self, category):
        return (self.category.lower() == category.lower())
                
    def show(self):
        if self.load() is not None:
            imbbox(self.image, self.xmin, self.xmax, self.ymin, self.ymax, bboxcaption=self.category)