0

I used opencv to read an image and save it to redis like this:

frame=cv2.imread('/path/to/image.png')
rd.set('frame', frame)

then,read it back a string representation like this:

[[[ 38  45  51]
  [ 38  45  51]
  [ 38  45  51]
  ..., 

  [235 217 222]]]

then I tried to get it back like this:

frameString=rd.get('frame')
mat=np.array(frameString)

but

 print mat.shape

output

 ()

then I tried

 mat=eval(frameString)

this gives me error:

    exec exp in global_vars, local_vars
  File "<console>", line 1, in <module>
  File "<string>", line 1
    [[[ 38  45  51]
             ^
SyntaxError: invalid syntax

question is

how to convert this string representation back to numpy array correctly?
Alex Luya
  • 9,412
  • 15
  • 59
  • 91
  • `pickle.dumps` is a better way of generating a string representation of a numpy array. It, in effect, uses `np.save` to serialize the array. – hpaulj Sep 20 '17 at 16:33

1 Answers1

0

The easiest thing to do would be to encode it as JSON and save that to redis.

frame=cv2.imread('/path/to/image.png')
rd.set('frame', json.dumps(frame. tolist()))


frameString=json.loads(rd.get('frame'))
mat=np.array(frameString)

You can find faster and more compact serialization formats though.

Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92