[英]Convert str to numpy.ndarray
我正在創建一個與opencv共享視頻的系統,但是我遇到了問題。 我有一個服務器和一個客戶端,但是當我向服務器發送信息時,必須為字節。 我發送兩件事:
ret, frame = cap.read()
ret是一個booland框架,是數據視頻,一個numpy.ndarray ret沒問題,但是是框架:我先將其轉換為字符串,然后轉換為字節:
frame = str(frame).encode()
connexion_avec_serveur.send(frame)
我現在想再次轉換一個numpy.ndarray中的幀。
您的str(frame).encode()
錯誤。 如果將其打印到終端,則會發現它不是框架的數據。
另一種方法是使用tobytes()
和frombuffer()
。
## read
ret, frame = cap.read()
sz = frame.shape
## tobytes
frame_bytes = frame.tobytes()
print(type(frame_bytes))
# <class 'bytes'>
## frombuffer and reshape
frame_frombytes = np.frombuffer(frame_bytes, dtype=np.uint8).reshape(sz)
print(type(frame_frombytes))
## <class 'numpy.ndarray'>
## test whether they equal or not
print(np.array_equal(frame, frame_frombytes))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.