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: (Image name, (x, y, w, h)) value: face image (.png) """ try: image = imfeat.image_fromstring(value, { 'type': 'numpy', 'dtype': 'uint8', 'mode': 'gray' }) image_color = imfeat.image_fromstring(value, { 'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr' }) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') return faces = _detect_faces(image, self._cascade) for x, y, w, h in faces: yield (key, (x, y, w, h)), imfeat.image_tostring( image_color[y:y + h, x:x + w, :], '.png')
def _verify_image(image_binary): import imfeat try: imfeat.image_fromstring(image_binary) except IOError: return False return True
def test_tostring(self): for fn in ['lena.jpg', 'lena.pgm', 'lena.ppm']: for i in load_images(fn): for ext in ['jpeg', 'png']: o = imfeat.image_tostring(i, ext) o2 = imfeat.image_tostring(imfeat.image_fromstring(imfeat.image_tostring(i, ext)), ext) if ext == 'png': np.testing.assert_equal(o, o2) s = imfeat.image_fromstring(o) s2 = imfeat.image_fromstring(o2) # No more than 9% of the pixels differ by more than 3 self.assertLess(np.mean(np.abs(s - s2) > 3), .09)
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)
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
def main(): exemplars = sorted(pickle.load(open('exemplars.pkl')), key=lambda x: x[0][2], reverse=True)[:100] with open('exemplars_best.pkl', 'w') as fp: pickle.dump(exemplars, fp, -1) hdfs_output = 'exemplarbank/output/%s/' % '1341790878.92' #hadoopy.launch_frozen('/user/brandyn/aladdin_results/keyframe/9/keyframe', hdfs_output + 'frame_pred', 'predict_video_frame.py', cmdenvs=['EXEMPLARS=exemplars_best.pkl', 'CELL_SKIP=1'], remove_output=True, files=['exemplars_best.pkl']) local_out = 'frame_preds/' try: shutil.rmtree(local_out) except OSError: pass os.makedirs(local_out) for num, (data, (pyramid, num_boxes)) in enumerate(hadoopy.readtb(hdfs_output + 'frame_pred')): if np.sum(pyramid): pyramid_norm = pyramid / float(num_boxes) pyramid_prob = np.sqrt(pyramid / float(np.max(pyramid))) p = np.sum(pyramid_norm) f = imfeat.image_fromstring(data['frame']) pyramid_prob_frame = cv2.resize(pyramid_prob, (f.shape[1], f.shape[0])) pyramid_prob_frame_color = COLORS[(pyramid_prob_frame * 255).astype(np.int), :] alpha = .5 beta = alpha * pyramid_prob_frame beta = beta.reshape((beta.shape[0], beta.shape[1], 1)) else: beta = 0. f = ((1 - beta) * f + beta * pyramid_prob_frame_color).astype(np.uint8) print(p) open(local_out + '%f-%d.jpg' % (p, num), 'w').write(imfeat.image_tostring(f, 'jpg'))
def annotation_masks_to_hbase(self): import redis import ast import imfeat import cv2 r = redis.StrictRedis(port=6381, db=6) responses = [r.hgetall(x) for x in r.keys()] print('Total Responses[%d]' % len(responses)) image_class_segments = {} # [row][class_name] = segments for x in responses: if 'user_data' in x: x['user_data'] = ast.literal_eval(x['user_data']) if x['user_data']['segments']: row_key = x['image'] image_class_segments.setdefault(row_key, {}).setdefault(x['user_data']['name'], []).append(x['user_data']['segments']) for row_key, class_segments in image_class_segments.items(): columns = dict((x, y.value) for x, y in self.hb.getRowWithColumns(self.images_table, row_key, [self.image_column, self.superpixel_column])[0].columns.items()) image = imfeat.image_fromstring(columns[self.image_column]) segments = json.loads(columns[self.superpixel_column]) class_masks = {} class_masks = np.zeros((image.shape[0], image.shape[1], self.texton_num_classes)) for name, user_segment_groups in class_segments.items(): cur_mask = np.zeros(image.shape[:2], dtype=np.uint8) for user_segment_group in user_segment_groups: for user_segment in user_segment_group: hull = segments[user_segment] hull = np.asarray(hull).astype(np.int32).reshape(1, -1, 2) mask_color = 255 cv2.drawContours(cur_mask, hull, -1, mask_color, -1) cv2.drawContours(cur_mask, hull, -1, mask_color, 2) class_masks[:, :, self.texton_classes[name]['mask_num']] = cur_mask / 255. class_masks_ser = picarus.api.np_tostring(class_masks) print('Storing row [%s]' % repr(row_key)) self.hb.mutateRow(self.images_table, row_key, [hadoopy_hbase.Mutation(column=self.masks_gt_column, value=class_masks_ser)])
def inner(num_rows, **kw): row_cols = hadoopy_hbase.scanner(self.hb, self.images_table, columns=[self.image_column, self.indoor_class_column], **kw) for x, (_, cols) in enumerate(row_cols): print(repr(x)) if x >= num_rows: break yield cols[self.indoor_class_column], imfeat.image_fromstring(cols[self.image_column])
def _map(self, row, image_binary): try: image = imfeat.image_fromstring(image_binary) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') out = self.sp(image, 25) hulls = self.sp.label_image_to_contours(out, 1.) yield row, json.dumps(hulls, separators=(',', ':'))
def map(self, image_id, image_binary): try: boxes = self.image_box_fns[image_id] except KeyError: pass else: image = imfeat.image_fromstring(image_binary) size_array = np.array([ image.shape[0], image.shape[1], image.shape[0], image.shape[1] ]) scale_boxes = {} for box, fn in boxes: fbox = size_array * box scale = int( np.round(np.log2( (fbox[2] - fbox[0]) / feature.PATCH_SIZE))) scale_check = int( np.round(np.log2( (fbox[3] - fbox[1]) / feature.PATCH_SIZE))) if scale != scale_check: raise ValueError('Box is not square.') scale_boxes.setdefault(scale, []).append((box, fn)) # Order boxes and fn's by scale for scale in range(max(scale_boxes.keys()) + 1): if scale > 0: height, width = np.array(image.shape[:2]) / 2 image = image[:height * 2, :width * 2, :] if min(width, height) < 1: raise ValueError('Image is too small') image = cv2.resize(image, (width, height)) if image is None: # NOTE(brandyn): It is too small raise ValueError('Image is too small') try: boxes = scale_boxes[scale] except KeyError: continue size_array = np.array([ image.shape[0], image.shape[1], image.shape[0], image.shape[1] ]) for box, fn in boxes: box = np.round(size_array * box).astype(np.int) print(box) if self.type == 'image': image_box = np.ascontiguousarray( image[box[0]:box[2], box[1]:box[3], :]) yield fn, imfeat.image_tostring(image_box, 'png') elif self.type == 'feature': image_box = np.ascontiguousarray( image[box[0]:box[2], box[1]:box[3], :]) yield fn, feature.compute_patch(image_box) elif self.type == 'box': image2 = image.copy() cv2.rectangle(image2, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 4) yield fn, imfeat.image_tostring(image2, 'jpg') else: raise ValueError(self.type)
def map(self, image_id, image_binary): image = resize(imfeat.image_fromstring(image_binary)) print(image.shape) st = time.time() box_num = -1 for box_num, (box, f) in enumerate(feature.image_patch_features_dense(image, normalize_box=True)): yield (image_id, box.tolist()), np.dot(self.coefs, f.reshape((f.size, 1))).ravel() + self.intercepts hadoopy.counter('stats', 'num_boxes', box_num + 1) print('ImageTime[%f]' % (time.time() - st))
def reduce(self, key, values): """ For this to work we need a modified partitioner on the substring before the first tab. This method operates on a single tile at level 0, and more tiles at higher zoom levels (4x each level). Args: key: (tile_id, subtile_id) values: Iterator of [dist, images] where images are power of 2 JPEG images in descending order by size Yields: Tuple of (key, value) where key: Tile name value: JPEG Image Data """ # Select minimum distance image, throw away score, and order images from smallest to largest powers of 2 self._sub_tiles[key] = min(values, key=lambda x: x[0])[1][::-1] # As the images were JPEG, we need to make them arrays again self._sub_tiles[key] = [ imfeat.image_fromstring(x) for x in self._sub_tiles[key] ] # If we don't have all of the necessary subtiles. if len(self._sub_tiles) != _subtiles_per_tile: return self._verify_subtile_keys() for level in range(_levels): # Each image is smaller than the tile scale = 2**level num_tiles = scale * scale subtiles_per_tile_len = _subtiles_per_tile_length / scale subtiles_per_tile = subtiles_per_tile_len**2 subtile_len = _subtile_length * scale cur_subtiles = [(self._find_output(key, scale, subtiles_per_tile_len, subtile_len), images[level]) for key, images in self._sub_tiles.items()] cur_subtiles.sort(key=lambda x: x[0]) cur_tile = np.zeros((_tile_length, _tile_length, 3), dtype=np.uint8) assert len(cur_subtiles) / subtiles_per_tile == num_tiles cur_outtile = None for subtile_ind, ((xouttile, youttile, xoffset, yoffset), image) in enumerate(cur_subtiles): if cur_outtile is None: cur_outtile = (xouttile, youttile) assert cur_outtile == (xouttile, youttile) #cur_tile.paste(image, (xoffset, yoffset)) # TODO Suspect cur_tile[yoffset:yoffset + image.shape[0], xoffset:xoffset + image.shape[1], :] = image if not (subtile_ind + 1) % subtiles_per_tile: tile_name = '%d_%d_%d.jpg' % (level, xouttile, youttile) yield tile_name, imfeat.image_tostring(cur_tile, 'jpg') cur_tile = np.zeros((_tile_length, _tile_length, 3), dtype=np.uint8) cur_outtile = None self._sub_tiles = {}
def map(self, name, image_or_data): if isinstance(image_or_data, str): try: image = imfeat.image_fromstring(image_or_data) except: hadoopy.counter("DATA_ERRORS", "ImageLoadError") return else: image = image_or_data yield name, self._feat(image)
def _map(self, row, image_binary): try: if not image_binary: raise ValueError image = imfeat.image_fromstring(image_binary) except: hadoopy.counter('ERROR', 'FEATURE') print('Error on row[%r]' % row) else: yield row, picarus.api.np_tostring(self._feat.compute_feature(image))
def map(self, name, image_or_data): if isinstance(image_or_data, str): try: image = imfeat.image_fromstring(image_or_data) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') return else: image = image_or_data yield name, self._feat(image)
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: (Image name, (x, y, w, h)) value: face image (.png) """ try: image = imfeat.image_fromstring(value, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'gray'}) image_color = imfeat.image_fromstring(value, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr'}) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') return faces = _detect_faces(image, self._cascade) for x, y, w, h in faces: yield (key, (x, y, w, h)), imfeat.image_tostring(image_color[y:y + h, x:x + w, :], '.png')
def _get_image(): params = dict(bottle.request.params) try: data = base64.b64decode(params['image_b64']) del params['image_b64'] except KeyError: try: data = bottle.request.files['image'].file.read() except KeyError: raise ValueError('Missing image') print('ImageData[%s]' % str(data[:25])) return imfeat.image_fromstring(data), params
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
def cluster_points_local(self, **kw): row_cols = hadoopy_hbase.scanner(self.hb, self.images_table, columns=[self.image_column], **kw) feature_func = imfeat.HOGLatent(16) num_clusters = 100 features = [] for row, columns in row_cols: image = imfeat.image_fromstring(columns[self.image_column]) features.append(feature_func.compute_dense(image)) features = np.vstack(features) clusters = sp.cluster.vq.kmeans(features, num_clusters)[0] print(clusters.shape) json.dump(clusters.tolist(), open('clusters.js', 'w'))
def reduce(self, key, values): """ For this to work we need a modified partitioner on the substring before the first tab. This method operates on a single tile at level 0, and more tiles at higher zoom levels (4x each level). Args: key: (tile_id, subtile_id) values: Iterator of [dist, images] where images are power of 2 JPEG images in descending order by size Yields: Tuple of (key, value) where key: Tile name value: JPEG Image Data """ # Select minimum distance image, throw away score, and order images from smallest to largest powers of 2 self._sub_tiles[key] = min(values, key=lambda x: x[0])[1][::-1] # As the images were JPEG, we need to make them arrays again self._sub_tiles[key] = [imfeat.image_fromstring(x) for x in self._sub_tiles[key]] # If we don't have all of the necessary subtiles. if len(self._sub_tiles) != _subtiles_per_tile: return self._verify_subtile_keys() for level in range(_levels): # Each image is smaller than the tile scale = 2 ** level num_tiles = scale * scale subtiles_per_tile_len = _subtiles_per_tile_length / scale subtiles_per_tile = subtiles_per_tile_len ** 2 subtile_len = _subtile_length * scale cur_subtiles = [(self._find_output(key, scale, subtiles_per_tile_len, subtile_len), images[level]) for key, images in self._sub_tiles.items()] cur_subtiles.sort(key=lambda x: x[0]) cur_tile = np.zeros((_tile_length, _tile_length, 3), dtype=np.uint8) assert len(cur_subtiles) / subtiles_per_tile == num_tiles cur_outtile = None for subtile_ind, ((xouttile, youttile, xoffset, yoffset), image) in enumerate(cur_subtiles): if cur_outtile is None: cur_outtile = (xouttile, youttile) assert cur_outtile == (xouttile, youttile) #cur_tile.paste(image, (xoffset, yoffset)) # TODO Suspect cur_tile[yoffset:yoffset + image.shape[0], xoffset:xoffset + image.shape[1], :] = image if not (subtile_ind + 1) % subtiles_per_tile: tile_name = '%d_%d_%d.jpg' % (level, xouttile, youttile) yield tile_name, imfeat.image_tostring(cur_tile, 'jpg') cur_tile = np.zeros((_tile_length, _tile_length, 3), dtype=np.uint8) cur_outtile = None self._sub_tiles = {}
def object_rec_parse(self, *args, **kw): import cv2 try: for image_fn, objects in self.object_rec_parse_fn(*args, **kw): yield cv2.imread(image_fn), objects except NotImplementedError: import imfeat try: for image_url, objects in self.object_rec_parse_url(*args, **kw): try: yield imfeat.image_fromstring(urllib.urlopen(image_url).read()), objects except: logging.warn('Cannot download/parse [%s], skipping...' % image_url) except NotImplementedError: raise NotImplementedError
def replay(): datas = pickle.load(open('results.pkl')) hs = HBaseCrawlerStore() import imfeat for data in datas: for x in data['result']['items']: try: r = requests.get(x['link'], timeout=10) image = r.content if r.status_code != 200: continue except (requests.exceptions.TooManyRedirects, requests.exceptions.ConnectionError, requests.exceptions.Timeout): continue try: imfeat.image_fromstring(image) except IOError: continue query = data['query'].encode('utf-8') class_name = data['query'].encode('utf-8')[:-5].lower() source = 'google' snippet = x['snippet'].encode('utf-8') url = x['link'].encode('utf-8') hs.store(image, class_name, source, query=query, snippet=snippet, url=url) print(class_name)
def map(self, name, image_data): try: image = imfeat.image_fromstring(image_data) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') return if self.filter_side is not None and min(image.shape[0], image.shape[1]) < self.filter_side: hadoopy.counter('DATA_ERRORS', 'ImageTooSmallPre') return if self.max_side is not None: image = imfeat.resize_image_max_side(image, self.max_side) if self.filter_side is not None and min(image.shape[0], image.shape[1]) < self.filter_side: hadoopy.counter('DATA_ERRORS', 'ImageTooSmallPost') return yield name, imfeat.image_tostring(image, 'jpg')
def map(self, image_id, image_binary): image = imfeat.image_fromstring(image_binary) print(image.shape) st = time.time() box_num = -1 for box_num, (box, f) in enumerate(feature.image_patch_features_dense(image, normalize_box=True)): scores = np.dot(self.coefs, f.reshape((f.size, 1))) + self.intercepts pred_common = [image_id, box.tolist(), f.tolist()] for score, preds in zip(scores, self.preds): pred = self.output_formatter([float(score[0])] + pred_common) if len(preds) >= self.max_hard: heapq.heappushpop(preds, pred) else: heapq.heappush(preds, pred) hadoopy.counter("stats", "num_boxes", box_num + 1) print("ImageTime[%f]" % (time.time() - st))
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
def map(self, image_id, image_binary): image = imfeat.image_fromstring(image_binary) print(image.shape) st = time.time() box_num = -1 for box_num, (box, f) in enumerate(feature.image_patch_features_dense(image, normalize_box=True)): scores = np.dot(self.coefs, f.reshape((f.size, 1))) + self.intercepts pred_common = [image_id, box.tolist(), f.tolist()] for score, preds in zip(scores, self.preds): pred = self.output_formatter([float(score[0])] + pred_common) if len(preds) >= self.max_hard: heapq.heappushpop(preds, pred) else: heapq.heappush(preds, pred) hadoopy.counter('stats', 'num_boxes', box_num + 1) print('ImageTime[%f]' % (time.time() - st))
def map(self, image_id, image_binary): try: boxes = self.image_box_fns[image_id] except KeyError: pass else: image = imfeat.image_fromstring(image_binary) size_array = np.array([image.shape[0], image.shape[1], image.shape[0], image.shape[1]]) scale_boxes = {} for box, fn in boxes: fbox = size_array * box scale = int(np.round(np.log2((fbox[2] - fbox[0]) / feature.PATCH_SIZE))) scale_check = int(np.round(np.log2((fbox[3] - fbox[1]) / feature.PATCH_SIZE))) if scale != scale_check: raise ValueError("Box is not square.") scale_boxes.setdefault(scale, []).append((box, fn)) # Order boxes and fn's by scale for scale in range(max(scale_boxes.keys()) + 1): if scale > 0: height, width = np.array(image.shape[:2]) / 2 image = image[: height * 2, : width * 2, :] if min(width, height) < 1: raise ValueError("Image is too small") image = cv2.resize(image, (width, height)) if image is None: # NOTE(brandyn): It is too small raise ValueError("Image is too small") try: boxes = scale_boxes[scale] except KeyError: continue size_array = np.array([image.shape[0], image.shape[1], image.shape[0], image.shape[1]]) for box, fn in boxes: box = np.round(size_array * box).astype(np.int) print(box) if self.type == "image": image_box = np.ascontiguousarray(image[box[0] : box[2], box[1] : box[3], :]) yield fn, imfeat.image_tostring(image_box, "png") elif self.type == "feature": image_box = np.ascontiguousarray(image[box[0] : box[2], box[1] : box[3], :]) yield fn, feature.compute_patch(image_box) elif self.type == "box": image2 = image.copy() cv2.rectangle(image2, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 4) yield fn, imfeat.image_tostring(image2, "jpg") else: raise ValueError(self.type)
def map(self, name, image_data): try: image = imfeat.image_fromstring(image_data) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') return if self.filter_side is not None and min( image.shape[0], image.shape[1]) < self.filter_side: hadoopy.counter('DATA_ERRORS', 'ImageTooSmallPre') return if self.max_side is not None: image = imfeat.resize_image_max_side(image, self.max_side) if self.filter_side is not None and min( image.shape[0], image.shape[1]) < self.filter_side: hadoopy.counter('DATA_ERRORS', 'ImageTooSmallPost') return yield name, imfeat.image_tostring(image, 'jpg')
def segmentation_boxes(self): """ Yields: Dataset as specified by 'split' Data is in the form of (masks, numpy array), where masks is a dict of boolean masks with keys as class names """ classes = json.load(open('classes.js')) for row_key, columns in self.data_source.row_column_values(['gt']): if not row_key.startswith('sun397train'): continue columns = dict(columns) print(columns.keys()) print(repr(row_key)) image = imfeat.image_fromstring(self.data_source.value(row_key, 'image')) masks = (255 * picarus.api.np_fromstring(columns['gt'])).astype(np.uint8) class_masks = dict((y, np.ascontiguousarray(masks[:, :, x])) for x, y in enumerate(classes)) yield class_masks, image
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
def segmentation_boxes(self): """ Yields: Dataset as specified by 'split' Data is in the form of (masks, numpy array), where masks is a dict of boolean masks with keys as class names """ classes = json.load(open('classes.js')) for row_key, columns in self.data_source.row_column_values(['gt']): if not row_key.startswith('sun397train'): continue columns = dict(columns) print(columns.keys()) print(repr(row_key)) image = imfeat.image_fromstring( self.data_source.value(row_key, 'image')) masks = (255 * picarus.api.np_fromstring(columns['gt'])).astype( np.uint8) class_masks = dict((y, np.ascontiguousarray(masks[:, :, x])) for x, y in enumerate(classes)) yield class_masks, image
def _mask_annotation_render(self, row_key, class_segments, image_key, image_superpixel_key, min_votes=1): columns = dict((x, y.value) for x, y in self.hb.getRowWithColumns(self.images_table, row_key, [image_key, image_superpixel_key])[0].columns.items()) image = imfeat.image_fromstring(columns[image_key]) segments = json.loads(columns[image_superpixel_key]) class_masks = {} class_masks = np.zeros((image.shape[0], image.shape[1], self.texton_num_classes)) for name, user_segment_groups in class_segments.items(): cur_mask = np.zeros(image.shape[:2], dtype=np.uint8) segment_counts = {} for x in sum(user_segment_groups, []): try: segment_counts[x] += 1 except KeyError: segment_counts[x] = 1 valid_segments = [x for x, y in segment_counts.items() if y >= min_votes] for user_segment in valid_segments: hull = segments[user_segment] hull = np.asarray(hull).astype(np.int32).reshape(1, -1, 2) mask_color = 255 cv2.drawContours(cur_mask, hull, -1, mask_color, -1) cv2.drawContours(cur_mask, hull, -1, mask_color, 2) class_masks[:, :, self.texton_classes[name]['mask_num']] = cur_mask / 255. return class_masks, image
def _map(self, row, image_binary): try: image = imfeat.image_fromstring(image_binary) except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError') yield row, picarus.api.np_tostring(self._feat(image))
def map(self, image_id, image_binary): image = imfeat.image_fromstring(image_binary) for box, f in feature.image_patch_features_dense(image, normalize_box=True): yield (image_id, box.tolist()), f
import os import random import numpy as np import imfeat import picarus.modules import picarus.api logging.basicConfig(level=logging.DEBUG) a = hadoopy_hbase.connect() hrc = picarus.modules.HashRetrievalClassifier() hrc.load(open('sun397_feature_index.pb').read()) for num, (row, cols) in enumerate(hadoopy_hbase.scanner(a, 'images', start_row='sun397train')): if num > 2: break print cols['feat:superpixel'][:50] image = imfeat.image_fromstring(cols['data:image_320']) print imfeat.image_fromstring(cols['data:image']).shape print imfeat.image_fromstring(cols['data:image_320']).shape print('image_75sq[%d]' % len(cols['data:image_75sq'])) print row cur_f = picarus.api.np_fromstring(cols['feat:gist']) cur_h = np.fromstring(cols['hash:gist'], dtype=np.uint8) print 'HOG', picarus.api.np_fromstring(cols['feat:bovw_hog_levels2_sbin16_blocks1_clusters100']) print 'Hash Bits[%d]' % (cur_h.size * 8,) print 'Feature Dims[%d]' % (cur_f.size,) f = hrc.feature(image) h = hrc.hasher(f).ravel() #print cur_f #print f print cur_h print h
import random import numpy as np import imfeat import picarus.modules import picarus.api logging.basicConfig(level=logging.DEBUG) a = hadoopy_hbase.connect() hrc = picarus.modules.HashRetrievalClassifier() hrc.load(open('sun397_feature_index.pb').read()) for num, (row, cols) in enumerate( hadoopy_hbase.scanner(a, 'images', start_row='sun397train')): if num > 2: break print cols['feat:superpixel'][:50] image = imfeat.image_fromstring(cols['data:image_320']) print imfeat.image_fromstring(cols['data:image']).shape print imfeat.image_fromstring(cols['data:image_320']).shape print('image_75sq[%d]' % len(cols['data:image_75sq'])) print row cur_f = picarus.api.np_fromstring(cols['feat:gist']) cur_h = np.fromstring(cols['hash:gist'], dtype=np.uint8) print 'HOG', picarus.api.np_fromstring( cols['feat:bovw_hog_levels2_sbin16_blocks1_clusters100']) print 'Hash Bits[%d]' % (cur_h.size * 8, ) print 'Feature Dims[%d]' % (cur_f.size, ) f = hrc.feature(image) h = hrc.hasher(f).ravel() #print cur_f #print f print cur_h
def load_data(self, image_data, image_metadata): image_metadata['tags'] = image_metadata['tags'].split() image = imfeat.image_fromstring(image_data, {'type': 'numpy', 'dtype': 'uint8', 'mode': 'bgr'}) return image, image_metadata
def _map(self, row, image_binary): try: image = imfeat.image_fromstring(image_binary) yield row, imfeat.image_tostring(imfeat.resize_image_max_side(image, self.max_side), 'jpg') except: hadoopy.counter('DATA_ERRORS', 'ImageLoadError')