summaryrefslogtreecommitdiff
path: root/compiler/imgdata2hdf5/imgdata2hdf5.py
blob: 1ff912a2fa2100270de412d418213ce2ae07a971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import h5py as h5
import numpy as np
import argparse
import glob
import os

parser = argparse.ArgumentParser()
parser.add_argument(
    "-l",
    "--data_list",
    type=str,
    help=
    "Path to the text file which lists the absolute paths of the raw image data files to be converted.",
    required=True)
parser.add_argument(
    "-o", "--output_path", type=str, help="Path to the output hdf5 file.", required=True)

args = parser.parse_args()
data_list = args.data_list
output_path = args.output_path

# Create h5 file
h5_file = h5.File(output_path, 'w')
group = h5_file.create_group("value")
# We assume the raw input data have the correct type/shape for the corresponding model
# If this flag is set in the hdf5 file, record-minmax will skip type/shape check
group.attrs['rawData'] = '1'

if os.path.isfile(data_list) == False:
    raise SystemExit("No such file. " + data_list)

# Data list
datalist = []
with open(data_list, 'r') as f:
    lines = f.readlines()
    for line in lines:
        if line.strip():
            filename = line.rstrip()
            if os.path.isfile(filename):
                datalist.append(filename)
            else:
                raise SystemExit("No such file. " + filename)

# Input files
num_converted = 0
for imgdata in datalist:
    with open(imgdata, 'rb') as f:
        sample = group.create_group(str(num_converted))
        num_converted += 1
        filename = os.path.basename(imgdata)
        sample.attrs['desc'] = filename
        raw_data = bytearray(f.read())
        # The target model is DNN for handling an input image
        sample.create_dataset('0', data=raw_data)

h5_file.close()

print("Raw image data have been packaged to " + output_path)
print("Number of packaged data: " + str(num_converted))