Skip to content

Instantly share code, notes, and snippets.

@nonodev96
Last active October 15, 2024 12:41
Show Gist options
  • Save nonodev96/b8f25dfc12536d76c1d27bb0d23c9def to your computer and use it in GitHub Desktop.
Save nonodev96/b8f25dfc12536d76c1d27bb0d23c9def to your computer and use it in GitHub Desktop.
Read npy and npz in browser with js
import npyjs from 'npyjs'
import JSZip from 'jszip'
import JSZipUtils from 'jszip-utils'
const handleClick_debug_npz = async () => {
const file = process.env.REACT_APP_PATH + '/datasets/03-image-classification/kmnist/kmnist-train-imgs.npz'
await JSZipUtils.getBinaryContent(file, async (err, data) => {
if (err) {
throw err // or handle the error
}
const jsZip = new JSZip()
const npzFiles = await jsZip.loadAsync(data)
for (const [npy_filename, npy_data] of Object.entries(npzFiles.files)) {
if (!npy_filename.endsWith('.npy')) {
console.error('error .npy')
return
}
const npy_array_buffer = await npzFiles.files[npy_filename].async("arraybuffer")
console.log({ npy_filename, npy_data, npy_array_buffer: npy_array_buffer })
const _npyjs_ = new npyjs()
console.log(await _npyjs_.parse(npy_array_buffer))
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment