from fastai.gen_doc.nbdoc import * from fastai.vision import * import PIL img = open_image('imgs/cat_example.jpg') img img.show() _,axs = plt.subplots(1,4,figsize=(12,4)) for i,ax in enumerate(axs): img.show(ax=ax, title=f'Copy {i+1}') img.data.shape show_doc(Image, title_level=3) show_doc(open_image) from fastai.vision import * path_data = untar_data(URLs.PLANET_TINY); path_data.ls() il = ImageList.from_folder(path_data/'train'); il il.convert_mode = 'L' il.open(il.items[10]) mode = '1' open_image(il.items[10],convert_mode=mode) show_doc(Image.show, arg_comments ={ 'ax': 'matplotlib.pyplot axes on which show the image', 'figsize': 'Size of the figure', 'title': 'Title to display on top of the graph', 'hide_axis': 'If True, the axis of the graph are hidden', 'cmap': 'Color map to use', 'y': 'Potential target to be superposed on the same graph (mask, bounding box, points)' }, full_name='Image.show') img.show(figsize=(2, 1), title='Little kitten') img.show(figsize=(10,5), title='Big kitten') img.shape img = open_image('imgs/cat_example.jpg', convert_mode='L'); print(img.shape) img img.show(cmap='binary') img.show(cmap='pink') defaults.cmap = 'Blues' img.show() img.data, img.shape, img.size show_doc(ImageSegment, title_level=3) show_doc(open_mask) open_mask('imgs/mask_example.png') df = pd.read_csv('imgs/mask_rle_sample.csv') encoded_str = df.iloc[1]['rle_mask']; df[:2] mask = open_mask_rle(df.iloc[0]['rle_mask'], shape=(1918, 1280)).resize((1,128,128)) mask show_doc(open_mask_rle) rle_decode(encoded_str, (1912, 1280)).shape show_doc(rle_decode) type(mask) rle_encode(mask.data) show_doc(rle_encode) img = open_image('imgs/car_example.jpg') mask = open_mask('imgs/mask_example.png') _,axs = plt.subplots(1,3, figsize=(8,4)) img.show(ax=axs[0], title='no mask') img.show(ax=axs[1], y=mask, title='masked') mask.show(ax=axs[2], title='mask only', alpha=1.) show_doc(ImagePoints, title_level=3) img = open_image('imgs/face_example.jpg') pnts = torch.load('points.pth') pnts = ImagePoints(FlowField(img.size, pnts)) img.show(y=pnts) pnts.data[:10] show_doc(ImageBBox, title_level=3) show_doc(ImageBBox.create, arg_comments={ 'h': 'height of the input image', 'w': 'width of the input image', 'bboxes': 'list of bboxes (each of those being four integers with the top, left, bottom, right convention)', 'labels': 'labels of the images (as indexes)', 'classes': 'the corresponding classes', 'pad_idx': 'padding index that will be used to group the ImageBBox in a batch', 'scale': 'if `True`, will scale the bounding boxes from -1 to 1' }) img = open_image('imgs/car_bbox.jpg') bbox = ImageBBox.create(*img.size, [[96, 155, 270, 351]], labels=[0], classes=['car']) img.show(y=bbox) show_doc(show_image) show_doc(pil2tensor) pil2tensor(PIL.Image.open('imgs/cat_example.jpg').convert("RGB"), np.float32).div_(255).size() pil2tensor(PIL.Image.open('imgs/cat_example.jpg').convert("I"), np.float32).div_(255).size() pil2tensor(PIL.Image.open('imgs/mask_example.png').convert("L"), np.float32).div_(255).size() pil2tensor(np.random.rand(224,224,3).astype(np.float32), np.float32).size() pil2tensor(PIL.Image.open('imgs/cat_example.jpg'), np.float32).div_(255).size() pil2tensor(PIL.Image.open('imgs/mask_example.png'), np.float32).div_(255).size() show_doc(image2np) show_doc(scale_flow) show_doc(bb2hw) show_doc(tis2hw) show_doc(show_all) show_doc(plot_flat) show_doc(plot_multi) show_doc(show_multi) show_doc(Image.apply_tfms, arg_comments={ 'tfms': '`Transform` or list of `Transform`', 'do_resolve': 'if False, the values of random parameters are kept from the last draw', 'xtra': 'extra arguments to pass to the transforms', 'size': 'desired target size', 'mult': 'makes sure the final size is a multiple of mult', 'resize_method': 'how to get to the final size (crop, pad, squish)', 'padding_mode': "how to pad the image ('zeros', 'border', 'reflection')" }) def get_class_ex(): return open_image('imgs/cat_example.jpg') def get_seg_ex(): return open_image('imgs/car_example.jpg'), open_mask('imgs/mask_example.png') def get_pnt_ex(): img = open_image('imgs/face_example.jpg') pnts = torch.load('points.pth') return img, ImagePoints(FlowField(img.size, pnts)) def get_bb_ex(): img = open_image('imgs/car_bbox.jpg') return img, ImageBBox.create(*img.size, [[96, 155, 270, 351]], labels=[0], classes=['car']) tfms = get_transforms() _, axs = plt.subplots(2,4,figsize=(12,6)) for ax in axs.flatten(): img = get_class_ex().apply_tfms(tfms[0], get_class_ex(), size=224) img.show(ax=ax) tfms = get_transforms() _, axs = plt.subplots(2,4,figsize=(12,6)) for ax in axs.flatten(): img,mask = get_seg_ex() img = img.apply_tfms(tfms[0], size=224) mask = mask.apply_tfms(tfms[0], do_resolve=False, size=224) img.show(ax=ax, y=mask) tfms[0][4] tfms = get_transforms() _, axs = plt.subplots(2,4,figsize=(12,6)) for ax in axs.flatten(): img,pnts = get_pnt_ex() img = img.apply_tfms(tfms[0], size=224) pnts = pnts.apply_tfms(tfms[0], do_resolve=False, size=224) img.show(ax=ax, y=pnts) tfms = get_transforms() _, axs = plt.subplots(2,4,figsize=(12,6)) for ax in axs.flatten(): img,bbox = get_bb_ex() img = img.apply_tfms(tfms[0], size=224) bbox = bbox.apply_tfms(tfms[0], do_resolve=False, size=224) img.show(ax=ax, y=bbox) show_doc(ResizeMethod, doc_string=False, title_level=3) show_doc(Transform, title_level=3, alt_doc_string="Create a `Transform` for `func` and assign it a priority `order`.") show_doc(RandTransform, title_level=3) show_doc(RandTransform.resolve) show_doc(TfmAffine, title_level=3) show_doc(TfmCoord, title_level=3) show_doc(TfmLighting, title_level=3) show_doc(TfmPixel, title_level=3) show_doc(TfmCrop, title_level=3) show_doc(Image.affine) show_doc(Image.clone) show_doc(Image.coord) show_doc(Image.lighting) show_doc(Image.pixel) show_doc(Image.refresh) show_doc(Image.resize) show_doc(Image.save) show_doc(FlowField, title_level=3) show_doc(Image.crop_pad) show_doc(Image.contrast) show_doc(Image.brightness) show_doc(Image.flip_lr) show_doc(Image.pad) show_doc(Image.pixel) show_doc(Image.zoom) show_doc(Image.dihedral) show_doc(ImageSegment.refresh) show_doc(Image.jitter) show_doc(Image.squish) show_doc(Image.skew) show_doc(Image.perspective_warp) show_doc(Image.zoom_squish) show_doc(Image.crop) show_doc(Image.tilt) show_doc(Image.rotate) show_doc(ImageSegment.lighting) show_doc(Image.symmetric_warp) show_doc(Image.dihedral_affine) show_doc(ImagePoints.pixel) show_doc(ImageBBox.clone) show_doc(ImagePoints.refresh) show_doc(ImagePoints.coord) show_doc(Image.set_sample) show_doc(ImageSegment.show) show_doc(ImagePoints.show) show_doc(ImagePoints.clone) show_doc(ImagePoints.lighting) show_doc(Transform.calc) show_doc(Image.flip_affine) show_doc(ImageBBox.show) show_doc(ImagePoints.resize) show_doc(ImageSegment.reconstruct) show_doc(Image.cutout)