def create_patches_at_center(img_basenames, annotation_dir, image_dir, size=50, step=40, grayscale=True, progressbar=True,downsample=1, objectclass=None, negative_discard_rate=.9): '''Extract a set of image patches with labels, from the supplied list of annotated images. Positive-labelled patches are extracted centered on the annotated bounding box; negative-lablled patches are extracted at random from any part of the image which does not overlap an annotated bounding box.''' if progressbar: pb = ProgressBar(len(img_basenames)) if not annotation_dir[-1] == os.path.sep: annotation_dir = annotation_dir + os.path.sep if not image_dir[-1] == os.path.sep: image_dir = image_dir + os.path.sep color_type = 0 if grayscale: channels=1 else: channels =3 pos = [] neg = [] s = 1 for img_filename in img_basenames: if progressbar: pb.step(s) s +=1 annotation_filename = annotation_dir + img_filename[:-3] + 'xml' boundingboxes = get_bounding_boxes_for_single_image(annotation_filename, objectclass) colortype = cv2.IMREAD_COLOR img = cv2.imread(image_dir + img_filename, colortype) height,width,channels=img.shape img = img.reshape((height, width,channels)) img = np.rollaxis(img,2) image_pos = get_image_positives(img,boundingboxes,size,downsample=downsample) pos.append(image_pos) image_neg = get_image_negatives(img,boundingboxes,size,step,downsample=downsample,discard_rate=negative_discard_rate) neg.append(image_neg) pos = [item for sublist in pos for item in sublist] neg = [item for sublist in neg for item in sublist] patches = pos+neg index = np.arange(len(patches)) np.random.seed(0) np.random.shuffle(index) np_patches = np.empty((len(patches),channels,size/downsample,size/downsample),dtype=np.uint8) np_labels = np.empty(len(patches),dtype=int) max_pos=len(pos) for i,j in zip(index,xrange(len(index))): if i < max_pos: np_patches[j,] = pos[i] np_labels[j] = 1 else: np_patches[j,] = neg[i-max_pos] np_labels[j] = 0 np_labels = np_labels.astype(np.uint8) return np_labels,np_patches
def create_patches(img_basenames, annotation_dir, image_dir, size, step, grayscale=True, progressbar=True, downsample=1, objectclass=None, negative_discard_rate=.9): '''Extract a set of image patches with labels, from the supplied list of annotated images. Positive-labelled patches are extracted centered on the annotated bounding box; negative-labelled patches are extracted at random from any part of the image which does not overlap an annotated bounding box.''' if progressbar: pb = ProgressBar(len(img_basenames)) if not annotation_dir[-1] == os.path.sep: annotation_dir = annotation_dir + os.path.sep if not image_dir[-1] == os.path.sep: image_dir = image_dir + os.path.sep color_type = 0 if grayscale: channels = 1 else: channels = 3 pos = [] neg = [] s = 1 for img_filename in img_basenames: if progressbar: pb.step(s) s += 1 annotation_filename = annotation_dir + img_filename[:-3] + 'xml' boundingboxes = get_bounding_boxes_for_single_image( annotation_filename, objectclass) #colortype = cv2.IMREAD_COLOR #img = cv2.imread(image_dir + img_filename, colortype) img = misc.imread(image_dir + img_filename) height, width, channels = img.shape img = img.reshape((height, width, channels)) img = np.rollaxis(img, 2) image_pos = get_image_positives(img, boundingboxes, size, downsample=downsample) pos.append(image_pos) image_neg = get_image_negatives(img, boundingboxes, size, step, downsample=downsample, discard_rate=negative_discard_rate) neg.append(image_neg) pos = [item for sublist in pos for item in sublist] neg = [item for sublist in neg for item in sublist] patches = pos + neg index = np.arange(len(patches)) np.random.seed(0) np.random.shuffle(index) np_patches = np.empty( (len(patches), channels, size / downsample, size / downsample), dtype=np.uint8) np_labels = np.empty(len(patches), dtype=int) max_pos = len(pos) for i, j in zip(index, range(len(index))): if i < max_pos: np_patches[j, ] = pos[i] np_labels[j] = 1 else: np_patches[j, ] = neg[i - max_pos] np_labels[j] = 0 np_labels = np_labels.astype(np.uint8) return np_labels, np_patches
def create_patches_at_center(img_basenames, annotation_dir, img_dir, size=50, step=40, grayscale=True, progressbar=True): if progressbar: pb = ProgressBar(len(img_basenames)) if not annotation_dir[-1] == os.path.sep: annotation_dir = annotation_dir + os.path.sep if not img_dir[-1] == os.path.sep: img_dir = img_dir + os.path.sep color_type = 0 if grayscale: channels=1 else: channels =3 pos = [] neg = [] s = 1 for img_filename in img_basenames: if progressbar: pb.step(s) s +=1 annotation_filename = annotation_dir + img_filename[:-3] + 'xml' boundingboxes = get_bounding_boxes_for_single_image(annotation_filename) #print '%d objects in %s' % (len(boundingboxes), img_filename) colortype = cv2.IMREAD_COLOR if grayscale: colortype = cv2.IMREAD_GRAYSCALE img = cv2.imread(img_dir + img_filename, colortype) height,width=img.shape img = img.reshape((height, width,channels)) img = np.rollaxis(img,2) image_pos = get_image_positives(img,boundingboxes, size) pos.append(image_pos) image_neg = get_image_negatives(img, boundingboxes,size,step) neg.append(image_neg) else: img = cv2.imread(img_dir + img_filename, colortype) height,width,channels=img.shape img = img.reshape((height, width,channels)) img = np.rollaxis(img,2) image_pos = get_image_positives(img,boundingboxes, size) pos.append(image_pos) #print len(pos) image_neg = get_image_negatives(img, boundingboxes,size,step) neg.append(image_neg) #print len(neg) pos = [item for sublist in pos for item in sublist] print len(pos) neg = [item for sublist in neg for item in sublist] print len(neg) patches = pos+neg print len(patches) index = np.arange(len(patches)) np.random.seed(0) np.random.shuffle(index) np_patches = np.empty((len(patches),channels,size,size),dtype=np.uint8) np_labels = np.empty(len(patches),dtype=int) max_pos=len(pos) for i,j in zip(index,xrange(len(index))): if i < max_pos: np_patches[j,] = pos[i] np_labels[j] = 1 else: np_patches[j,] = neg[i-max_pos] np_labels[j] =0 #print '\nSplit each of ' + str(N) + ' images in ' + str(len(patches)) + \ #' ' + str(size) + 'x' + str(size) + ' patches' #print 'Total number of patches: ' + str(N*(len(patches))) #return np.array(np_labels).flatten(), np_patches return np_labels,np_patches