问题描述
已经在python中创建了一个rtsp客户端,该客户端接收一个h264流并返回单个h264原始帧作为二进制字符串.我正在尝试即时处理每个h264帧.
have created a rtsp client in python that receives a h264 stream and returns single h264 raw frames as a binary strings. I am trying to process each h264 frames on-the-fly.
我尝试了几种方法将该帧转换为numpy数组进行处理,但均未成功.
I have unsuccessfully tried several ways to convert this frame into a numpy array for processing.
到目前为止,我知道cv2.VideoCapture仅接受文件名作为其参数,而不接受帧,也不接受StringIO对象(诸如指向缓冲区的指针的文件),但是我需要将其字符串传递给它.
So far I know that cv2.VideoCapture only accepts a file name as it argument, not a frame neither a StringIO object (file like pointer to a buffer), but I need to pass to it my string.
我也尝试过类似的事情:
I have also tried something like:
nparr = np.fromstring(frame_bin_str, np.uint8)
img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
尝试了不同的标志.但也失败了.
tried diferent flags. but also failed miserably.
在进行了许多其他失败的尝试之后,我的想法耗尽了.
after many other failed attempts , I ran out of ideas.
总结一下我需要做的事情:我在变量中有一个h264原始帧,我需要为其创建一个openvc有效的numpy数组,或者以某种方式最终以包含该单个帧的VideoCapture对象结束,因此我可以进行处理框架.
To summarize what I need to do: I have a h264 raw frame in a variable and I need to create an openvc valid numpy array of it, or somehow end up with a VideoCapture object containing that single frame, so I can process the frame.
任何指针将不胜感激.
希望这一切都有道理.
提前谢谢
推荐答案
正如Micka所建议的那样,OpenCV不支持h264 RAW格式,我们应该自己进行转换.
As Micka suggested, there is no support for h264 RAW format in OpenCV and we should convert it ourselves.
我认为您应该将nparr
调整为传入图像的形状.不需要进行解码.使用imshow显示结果并进行验证.
I think you should be reshaping the nparr
to the shape of the incoming image. Not necessary to do imdecode. Use imshow to display the result and verify.
这是我用来以类似方式转换16位RAW图像(灰度)的代码.在显示之前,我已经对图像进行了标准化.
Here is the code I used to convert a 16 bit RAW image (grayscale) in a similar way. I have renormalized my image before displaying.
framenp = np.fromstring(framestr, dtype=np.uint16).reshape((1024,1280))
#renormalizing to float
framenp = (framenp*1./framenp.max())
framenp.dtype = np.float
cv2.imshow('frame', cv2.resize(framenp, (640,480)))
这篇关于opencv单个h264原始帧作为二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!