我尝试进行加密的视频 session ,因此数据将以UDP模式发送,我在python中使用openCV访问了网络摄像头,是否有任何办法以位的形式从网络摄像头访问数据,以便我可以进行encript-transfer-decript和显示所需台上的图像?或者还有什么其他库(python / java / c / c++)可以帮助我从网络摄像头获取数据?
最佳答案
我在Ubuntu 16.04上使用适用于Python(3.5)的OpenCV 3.3。
对于您的问题,要加密从视频中获取的帧并通过TCP / UDP进行转换:
(1) use `cap.read` to take frame form `videoCapture` as `np.array`
(2) use `np.tobytes` to convert from `np.array` to `bytes`
(3) do encryption on bytes(can be switched with step 2).
(4) transfer `bytes` using TCP/UDP
(1) receive from A
(2) do decryption on the received bytes
(3) use `np.frombuffer` to convert from `bytes` to `np.array`, then `reshape`. you get the data.
示例代码段:
cap = cv2.VideoCapture(0)
assert cap.isOpened()
## (1) read
ret, frame = cap.read()
sz = frame.shape
## (2) numpy to bytes
frame_bytes = frame.tobytes()
print(type(frame_bytes))
# <class 'bytes'>
## (3) encode
# ...
## (4) transfer
# ...
cap.release()
## (1) receive
## (2) decode
## (3) frombuffer and reshape to the same size on Side A
frame_frombytes = np.frombuffer(frame_bytes, dtype=np.uint8).reshape(sz)
print(type(frame_frombytes))
## <class 'numpy.ndarray'>
相关答案:
(1)Convert str to numpy.ndarray
(2)Can't write video by opencv in Python
(3)How to transfer cv::VideoCapture frames through socket in client-server model (OpenCV C++)?