简体   繁体   中英

How to save integer alongside of a np.array

I am doing a ConvNet and need to save one image alongside an integer (1 or 0), that indicates if an action was taken or not. How can I do it? I tried this:

key_pressed = np.array(key_check(self.key)) # key_check() returns 1 or 0
window = np.array(window) # window is opened with cv2

print(window.shape, key_pressed.shape) # (474, 31) (1,)
print(np.concatenate((window, key_pressed)))
      ^^
ValueError: all the input arrays must have same number of dimensions

Is doing what I want even possible with numpy, seeing that the arrays are of different sizes? Any ideas on how to save this data?

NumPy won't let you simply concatenate things of arbitrary sizes: that breaks the functional definition of an array or matrix. You're trying to make a simple juxtaposition, not a functional concatenation. All you're trying to do is display them side by side, right? This isn't a matrix operation. For instance, do you really envision doing a matrix multiplication on the resulting glued array???

You need a simple container to hold both items. I expect that a trivial class will do the job for you. Write a combined display method for the resulting pair, and use that for your rendering.

Have a look at scipy.io.savemat and loadmat :

savemat(your_path, dict(key=key_pressed, window=window))

Then using loadmat you'll get back a dict with the key and the frame that you want. There is even a do_compression parameter.

Edit: the incompatibility in shapes have nothing to do with it, since you're not creating a new array but saving key and window separately in the same file.

In [53]: from scipy import io

In [54]: a = np.zeros([3, 4])

In [55]: b = np.ones([5, 17])

In [56]: io.savemat("foo.mat", dict(a=a, b=b))

it works like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM