Skip to content

Commit

Permalink
Add base.run_example for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Jan 21, 2020
1 parent e4f06ab commit 70f7dec
Show file tree
Hide file tree
Showing 22 changed files with 118 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ pytest -v tests
# examples
MPLBACKEND=agg python getting_started.py
for f in examples/*.py; do
MPLBACKEND=agg python $f
MPLBACKEND=agg python $f --save
done
Binary file modified examples/.readme/centerize.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/depth2rgb.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/draw.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/flow2rgb.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/instances2rgb.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/label2rgb.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/nchannel2rgb.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/plot_trajectory.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/resize.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/.readme/tile.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions examples/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import argparse
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


def run_example(function):
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument('--save', action='store_true', help='save image')
args = parser.parse_args()

img = function()

if args.save:
out_file = osp.join(here, '.readme/{}.jpg'.format(function.__name__))
imgviz.io.imsave(out_file, img)

plt.imshow(img)
plt.axis('off')
plt.show()
23 changes: 9 additions & 14 deletions examples/centerize.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def centerize():
data = imgviz.data.arc2017()

rgb = data['rgb']
Expand Down Expand Up @@ -38,13 +33,13 @@
plt.imshow(centerized2)
plt.axis('off')

out_file = osp.join(here, '.readme/centerize.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(centerize)
23 changes: 9 additions & 14 deletions examples/depth2rgb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def depth2rgb():
data = imgviz.data.arc2017()

depthviz = imgviz.depth2rgb(data['depth'], min_value=0.3, max_value=1)
Expand All @@ -27,13 +22,13 @@
plt.imshow(depthviz)
plt.axis('off')

out_file = osp.join(here, '.readme/depth2rgb.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(depth2rgb)
25 changes: 10 additions & 15 deletions examples/draw.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def draw():
img = imgviz.data.lena()
H, W = img.shape[:2]
viz = img
Expand Down Expand Up @@ -56,7 +51,7 @@

# -------------------------------------------------------------------------

plt.figure(dpi=200)
fig = plt.figure(dpi=200)

plt.subplot(121)
plt.title('original')
Expand All @@ -68,13 +63,13 @@
plt.imshow(viz)
plt.axis('off')

out_file = osp.join(here, '.readme/draw.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(draw)
23 changes: 9 additions & 14 deletions examples/flow2rgb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def flow2rgb():
data = imgviz.data.middlebury()

rgb = data['rgb']
Expand All @@ -27,13 +22,13 @@
plt.imshow(flowviz)
plt.axis('off')

out_file = osp.join(here, '.readme/flow2rgb.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(flow2rgb)
23 changes: 9 additions & 14 deletions examples/instances2rgb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def instances2rgb():
data = imgviz.data.voc()

captions = [data['class_names'][l] for l in data['labels']]
Expand Down Expand Up @@ -44,13 +39,13 @@
plt.imshow(insviz2)
plt.axis('off')

out_file = osp.join(here, '.readme/instances2rgb.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(instances2rgb)
25 changes: 9 additions & 16 deletions examples/label2rgb.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def label2rgb():
data = imgviz.data.voc()

rgb = data['rgb']
label = data['class_label']

labelviz = imgviz.label2rgb(label)

label_names = [
'{}:{}'.format(i, n) for i, n in enumerate(data['class_names'])
]
Expand Down Expand Up @@ -47,13 +40,13 @@
plt.imshow(labelviz_withname2)
plt.axis('off')

out_file = osp.join(here, '.readme/label2rgb.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(label2rgb)
23 changes: 9 additions & 14 deletions examples/nchannel2rgb.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import os.path as osp

import matplotlib.pyplot as plt
import numpy as np

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def nchannel2rgb():
data = imgviz.data.arc2017()

np.random.seed(1234) # for PCA
Expand All @@ -33,13 +28,13 @@
plt.imshow(nchannel_viz)
plt.axis('off')

out_file = osp.join(here, '.readme/nchannel2rgb.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(nchannel2rgb)
21 changes: 6 additions & 15 deletions examples/plot_trajectory.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def plot_trajectory():
data = imgviz.data.kitti_odometry()

img = imgviz.plot_trajectory(data['transforms'])

# -------------------------------------------------------------------------
return img


out_file = osp.join(here, '.readme/plot_trajectory.jpg')
imgviz.io.imsave(out_file, img)
if __name__ == '__main__':
from base import run_example

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
run_example(plot_trajectory)
25 changes: 10 additions & 15 deletions examples/resize.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import os.path as osp

import matplotlib.pyplot as plt

import imgviz


here = osp.dirname(osp.abspath(__file__))


if __name__ == '__main__':
def resize():
data = imgviz.data.arc2017()

rgb = data['rgb']
Expand All @@ -18,7 +13,7 @@

# -------------------------------------------------------------------------

plt.figure(dpi=200)
fig = plt.figure(dpi=200)

plt.subplot(121)
plt.title('rgb:\n{}'.format(rgb.shape))
Expand All @@ -30,13 +25,13 @@
plt.imshow(rgb_resized)
plt.axis('off')

out_file = osp.join(here, '.readme/resize.jpg')
plt.savefig(
out_file, bbox_inches='tight', transparent='True', pad_inches=0
)
img = imgviz.io.pyplot_fig2arr(fig)
plt.close()

img = imgviz.io.imread(out_file)
plt.imshow(img)
plt.axis('off')
plt.show()
return img


if __name__ == '__main__':
from base import run_example

run_example(resize)

0 comments on commit 70f7dec

Please sign in to comment.