Convert str to numpy.ndarray

后端 未结 1 987
南旧
南旧 2020-12-20 09:12

I\'m creating a system to share video with opencv but I\'ve got a problem. I\'ve a server and a client but when I send informations to the server, the must be bytes. I send

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 09:43

    Your str(frame).encode() is wrong. If you print it to terminal, then you will find it is not the data of the frame.

    An alternative method is to use tobytes() and frombuffer().

    ## read
    ret, frame = cap.read()
    sz = frame.shape
    
    ## tobytes 
    frame_bytes = frame.tobytes()
    print(type(frame_bytes))
    # 
    
    ## frombuffer and reshape 
    frame_frombytes = np.frombuffer(frame_bytes, dtype=np.uint8).reshape(sz)
    print(type(frame_frombytes))
    ## 
    
    ## test whether they equal or not 
    print(np.array_equal(frame, frame_frombytes))
    

    0 讨论(0)
提交回复
热议问题