Skip to content

Commit

Permalink
Make the hdf5 videos store as int8 format (#1559)
Browse files Browse the repository at this point in the history
* make the hdf5 video dataset type as proper int8 by padding with zeros

* add gzip compression
  • Loading branch information
lambdaloop authored Oct 20, 2023
1 parent 46bd21d commit dbe14a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
John Smith <john at example.com> Example Inc.

Jeremy Delahanty <jdelahanty@salk.edu> The Salk Institute for Biological Studies

Lili Karashchuk <lili.karashchuk@alleninstitute.org> Allen Institute of Neural Dynamics
20 changes: 15 additions & 5 deletions sleap/io/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,19 +1443,29 @@ def to_hdf5(

def encode(img):
_, encoded = cv2.imencode("." + format, img)
return np.squeeze(encoded)
return np.squeeze(encoded).astype("int8")

# pad with zeroes to guarantee int8 type in hdf5 file
frames = []
for i in range(len(frame_numbers)):
frames.append(encode(frame_data[i]))

max_frame_size = max([len(x) for x in frames])

dtype = h5.special_dtype(vlen=np.dtype("int8"))
dset = f.create_dataset(
dataset + "/video", (len(frame_numbers),), dtype=dtype
dataset + "/video",
(len(frame_numbers), max_frame_size),
dtype="int8",
compression="gzip",
)
dset.attrs["format"] = format
dset.attrs["channels"] = self.channels
dset.attrs["height"] = self.height
dset.attrs["width"] = self.width

for i in range(len(frame_numbers)):
dset[i] = encode(frame_data[i])
for i, frame in enumerate(frames):
dset[i, 0 : len(frame)] = frame

else:
f.create_dataset(
dataset + "/video",
Expand Down

0 comments on commit dbe14a8

Please sign in to comment.