Skip to content

Instantly share code, notes, and snippets.

@ahundt
Last active October 26, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahundt/58f802fd6f28db2f65c6887ab7ee4dca to your computer and use it in GitHub Desktop.
Save ahundt/58f802fd6f28db2f65c6887ab7ee4dca to your computer and use it in GitHub Desktop.
import os
import h5py
import io
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
def JpegToNumpy(jpeg):
stream = io.BytesIO(jpeg)
image = Image.open(stream)
return np.asarray(image, dtype=np.uint8)
def ConvertImageListToNumpy(data):
images = []
for raw in data:
img = JpegToNumpy(raw)
images.append(img)
return np.array(images, dtype=np.uint8)
# First download an example like the one linked here
# https://archive.org/download/johns_hopkins_costar_dataset/blocks_only/2018-05-23-18-00-08_example000001.success.h5f
# Then update the path below to reference your downloaded file
path = ('~/.keras/datasets/costar_block_stacking_dataset_v0.4/blocks_only/'
'2018-05-23-18-00-08_example000001.success.h5f')
# Open the h5f file using h5py
with h5py.File(os.path.expanduser(path), 'r') as data:
# You can use data[key] to access a particular feature column.
# Refer to "Dataset Features and Time Steps" section for
# more information on available features.
# Here, we get the frames for each goal in the file
goal_frames = np.unique(data['gripper_action_goal_idx'])
# Get the images corresponding to the goals
image_list = np.array(data['image'])[goal_frames]
# The images are stored as JPEG files.
# We now use functions defined above to convert the images to np arrays
images = ConvertImageListToNumpy(image_list)
# Turn the images into a tile
tiled_images = np.squeeze(np.hstack(images))
# Show the images with pyplot
plt.imshow(tiled_images)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment