예제 #1
0
    def map(self, key, value):
        """
        Args:
            key: Image name
            value: Image as jpeg byte data

        Yields:
            A tuple in the form of (key, value)
            key: Constant dummy value
            value: (l2sqr_dist, value)
        """
        try:
            image = imfeat.resize_image(imfeat.image_fromstring(value, {'type': 'numpy', 'mode': 'bgr', 'dtype': 'uint8'}),
                                        100, 100)
        except:
            hadoopy.counter('DATA_ERRORS', 'ImageLoadError')
            return

        # Distance metric
        diff = image - self.target_image
        dist = np.sum(diff * diff)

        # Output
        if dist < self.min_dist:
            self.min_dist = dist
            self.min_key = key
            self.min_value = value
예제 #2
0
 def __init__(self):
     path = 'target.jpg'
     self.target_image = np.asfarray(
         imfeat.resize_image(cv2.imread(path), 100, 100))
     self.min_dist = float('inf')
     self.min_key = None
     self.min_value = None
예제 #3
0
 def test_ainterface(self):
     """Simple test of the basic feature interface"""
     features = [
         imfeat.ObjectBank(),
         imfeat.GIST(),
         imfeat.HOGLatent(2),
         imfeat.Autocorrelogram(),
         imfeat.GradientHistogram(),
         imfeat.Histogram('gray'),
         imfeat.RHOG(gray=False),
         imfeat.RHOG(gray=True),
         imfeat.Moments('rgb', 2),
         imfeat.Histogram('rgb'),
         imfeat.SpatialHistogram(mode='rgb', num_rows=2, num_cols=2),
         imfeat.TinyImage()
     ]
     feat_sz = {}
     for image_fn in glob.glob('test_images/*'):
         if image_fn in ['test_images/test3.gif']:
             continue
         for feat_num, feature in enumerate(features):
             prev_f = None
             for load_func in [
                     cv.LoadImage, cv.LoadImageM, cv2.imread, Image.open
             ]:
                 f = feature(
                     imfeat.resize_image(load_func(image_fn), 100, 100))
                 self.assertEqual(feat_sz.setdefault(feat_num, f.size),
                                  f.size)
                 if prev_f is None:
                     prev_f = f
                 if load_func != Image.open:  # Skip PIL as the jpeg loading produces different data
                     np.testing.assert_equal(prev_f, f)
예제 #4
0
    def map(self, key, value):
        """
        Args:
            key: Image name
            value: Image as jpeg byte data

        Yields:
            A tuple in the form of (key, value)
            key: Constant dummy value
            value: (l2sqr_dist, key, value)
        """
        try:
            image = imfeat.resize_image(
                imfeat.image_fromstring(value, {
                    'type': 'numpy',
                    'mode': 'bgr',
                    'dtype': 'uint8'
                }), 100, 100)
        except:
            hadoopy.counter('DATA_ERRORS', 'ImageLoadError')
            return

        # Distance metric
        diff = image - self.target_image
        dist = np.sum(diff * diff)

        yield '', (dist, key, value)
예제 #5
0
 def test_resize(self):
     to_np8 = lambda x: imfeat.convert_image(x, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr'})
     n = 0
     for fn in ['lena.jpg', 'lena.pgm', 'lena.ppm']:
         for i in load_images(fn):
             for h, w in [(50, 50), (100, 50), (50, 100), (1000, 100), (100, 1000)]:
                 out_arr = to_np8(imfeat.resize_image(i, h, w))
                 self.assertEqual(out_arr.shape, (h, w, 3))
                 #cv2.imwrite('resize-out-%.3d.jpg' % n, out_arr)
                 n += 1
예제 #6
0
 def test_resize(self):
     to_np8 = lambda x: imfeat.convert_image(x, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr'})
     n = 0
     for fn in ['lena.jpg', 'lena.pgm', 'lena.ppm']:
         for i in load_images(fn):
             for h, w in [(50, 50), (100, 50), (50, 100), (1000, 100), (100, 1000)]:
                 out_arr = to_np8(imfeat.resize_image(i, h, w))
                 self.assertEqual(out_arr.shape, (h, w, 3))
                 #cv2.imwrite('resize-out-%.3d.jpg' % n, out_arr)
                 n += 1
예제 #7
0
def get_content(content_id):
    if content_id.endswith('.b16.html'):
        return '<img src="/content/%s.jpg" />' % base64.b16decode(content_id[:-9])
    image_data = open(base64.b16decode(content_id[:content_id.find('.b16')])).read()
    if content_id.endswith('.b16.thumb.jpg'):
        try:
            return CACHE[content_id]
        except KeyError:
            out_data = imfeat.image_tostring(imfeat.resize_image(imfeat.image_fromstring(image_data), 200, 200), 'JPEG')
            CACHE[content_id] = out_data
            return out_data
    return image_data
예제 #8
0
 def map(self, name, image_data):
     try:
         image = imfeat.image_fromstring(image_data)
     except:
         hadoopy.counter('DATA_ERRORS', 'ImageLoadError')
         return
     try:
         image = imfeat.resize_image(image, self._image_height, self._image_width)
     except:
         hadoopy.counter('DATA_ERRORS', 'ImageLoadError')
     try:
         for x in self._feat(image):
             yield name, x
     except:
         hadoopy.counter('DATA_ERRORS', 'UnkImageType')
         return
예제 #9
0
 def test_ainterface(self):
     """Simple test of the basic feature interface"""
     features = [imfeat.ObjectBank(), imfeat.GIST(), imfeat.HOGLatent(2),
                 imfeat.Autocorrelogram(), imfeat.GradientHistogram(), imfeat.Histogram('gray'),
                 imfeat.RHOG(gray=False), imfeat.RHOG(gray=True), imfeat.Moments('rgb', 2),
                 imfeat.Histogram('rgb'), imfeat.SpatialHistogram(mode='rgb', num_rows=2, num_cols=2),
                 imfeat.TinyImage()]
     feat_sz = {}
     for image_fn in glob.glob('test_images/*'):
         if image_fn in ['test_images/test3.gif']:
             continue
         for feat_num, feature in enumerate(features):
             prev_f = None
             for load_func in [cv.LoadImage, cv.LoadImageM, cv2.imread, Image.open]:
                 f = feature(imfeat.resize_image(load_func(image_fn), 100, 100))
                 self.assertEqual(feat_sz.setdefault(feat_num, f.size), f.size)
                 if prev_f is None:
                     prev_f = f
                 if load_func != Image.open:  # Skip PIL as the jpeg loading produces different data
                     np.testing.assert_equal(prev_f, f)
예제 #10
0
 def __call__(self, image):
     image = self.convert(image)
     return np.asfarray(imfeat.resize_image(image, self.size, self.size).ravel()) / 255.
예제 #11
0
 def __call__(self, image):
     image = self.convert(image)
     return np.asfarray(
         imfeat.resize_image(image, self.size, self.size).ravel()) / 255.
예제 #12
0
 def __call__(self, image):
     image = self.convert(image)
     return self.make_feature_mask(imfeat.resize_image(image, self.size, self.size), converted=True).ravel()
class Mapper(object):
    def __init__(self):
        _target_image = cv2.imread('target.jpg')
        _target_image = cv2.resize(
            _target_image,
            (_target_image.shape[1] // _tile_length * _tile_length,
             _target_image.shape[0] // _tile_length * _tile_length))
        self.target_tiles = {}
        ytiles = _target_image.shape[0] / _tile_length
        xtiles = _target_image.shape[1] / _tile_length
        print('Xtiles[%d] Ytiles[%d]' % (xtiles, ytiles))
        assert xtiles > 0 and ytiles > 0
        xsubtiles = xtiles * _subtiles_per_tile_length
        ysubtiles = ytiles * _subtiles_per_tile_length
        self.min_dists = {}
        self.dist = lambda x, y: np.sum(np.abs(x - y))  # distpy.L2Sqr().dist
        for y in xrange(ysubtiles):
            for x in xrange(xsubtiles):
                # Defines which tile the subtile is in
                #'%.6d_%.6d' %
                tile_id = (x / _subtiles_per_tile_length,
                           y / _subtiles_per_tile_length)
                # Defines which position it is in within the tile
                #'%.6d_%.6d' %
                subtile_id = (x % _subtiles_per_tile_length,
                              y % _subtiles_per_tile_length)
                #'\t'.join(
                key = (tile_id[0], tile_id[1], subtile_id[0], subtile_id[1])
                yp = ysubtiles - y - 1  # NOTE(brandyn): Flip coordinates for y axis
                tile = _target_image[(yp * _subtile_length):((yp + 1) *
                                                             _subtile_length),
                                     (x * _subtile_length):(x + 1) *
                                     _subtile_length, :]
                self.target_tiles[key] = np.asfarray(tile)

    @staticmethod
    def _crop_image_from_str(s):
        """Load from string, crop to a square, resize to _initial_image_size

        Args:
            s: String of bytes representing a JPEG image

        Returns:
            RGB Image with height/width as _initial_image_size

        Raises:
            ValueError: Image is height/width too small (< _initial_image_size)
                or mode isn't RGB
            IOError: Image is unreadable
        """
        if isinstance(s, tuple):
            s = s[0]
        try:
            img = imfeat.image_fromstring(s)
        except IOError, e:
            hadoopy.counter('Stats', 'IMG_BAD')
            raise e
        min_side = min(img.shape[:2])
        if min_side < _initial_image_size:
            hadoopy.counter('Stats', 'IMG_TOO_SMALL')
            raise ValueError
        if img.ndim != 3:
            hadoopy.counter('Stats', 'IMG_WRONG_MODE')
            raise ValueError
        return imfeat.resize_image(img, _initial_image_size,
                                   _initial_image_size)
예제 #14
0
파일: haystack.py 프로젝트: Jeffliu/hadoopy
 def __init__(self):
     path = 'target.jpg'
     self.target_image = np.asfarray(imfeat.resize_image(cv2.imread(path), 100, 100))
예제 #15
0
 def __init__(self):
     path = 'target.jpg'
     self.target_image = np.asfarray(imfeat.resize_image(cv2.imread(path), 100, 100))
     self.min_dist = float('inf')
     self.min_key = None
     self.min_value = None
예제 #16
0
 def __init__(self):
     path = 'target.jpg'
     self.target_image = np.asfarray(
         imfeat.resize_image(cv2.imread(path), 100, 100))
예제 #17
0
def compute_feature(image):
    #feature = imfeat.Histogram('lab', num_bins=8)
    feature = picarus._features.select_feature('gist')
    return feature(imfeat.resize_image(image, 256, 256))
예제 #18
0
 def __call__(self, image):
     image = self.convert(image)
     return self.make_feature_mask(imfeat.resize_image(image, self.size, self.size), converted=True).ravel()
예제 #19
0
파일: _tiny_image.py 프로젝트: ctku/imfeat
 def __call__(self, image):
     image = self.convert(image)
     return np.asfarray(imfeat.resize_image(image, 32, 32, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr'}).ravel()) / 255.