关于opencv python 获取视频流的一帧
各位大佬:
1.我是在centos6.9系统编译的python3.6.3版本,地址是/usr/local/python3,然后使用bin文件下的pip3安装了opencv_python库
2.在/usr/local/python3/lib/python3.6/site-packages/下生成opencv_python-3.3.0.10.dist-info和cv2目录
3.然后使用命令运行/usr/local目录下的snapshot.py脚本文件,即:/usr/local/python3/bin/python3 snapshot.py 提示错误:
OpenCV Error: Unknown error code -10 (Raw image encoder error: Empty JPEG image (DNL not supported)) in throwOnEror, file /io/opencv/modules/imgcodecs/src/grfmt_base.cpp, line 140Traceback (most recent call last):
File "snapshot.py", line 21, in <module>
main()
File "snapshot.py", line 11, in main
ret,arr = cv2.imencode('.jpg', frame)
cv2.error: /io/opencv/modules/imgcodecs/src/grfmt_base.cpp:140: error: (-10) Raw image encoder error: Empty JPEG image (DNL not supported) in function throwOnEror
请教大佬是什么原因,在windows下是可以按照程序截取到帧图片
import cv2
import numpy as np
from sys import argv
import base64
def main():
cap = cv2.VideoCapture("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov")
for i in range(1, 500):
ret,frame = cap.read()
ret,arr = cv2.imencode('.jpg', frame)
a = arr.tostring()
fp = open('test'+str(i)+'.jpg', 'wb')
fp.write(a)
fp.close()
fps = cap.get(5)
print(fps)
cap.release()
if __name__ == '__main__':
main()
回答:
你这个报错是opencv没抽到帧所以保存成图片报错了
同样的代码我在centos6环境下测试正常
import cv2
import os
import sys
def snaprtsp(rtspurl, filename):
try: cap = cv2.VideoCapture(rtspurl)
ret, frame = cap.read()
ret, arr = cv2.imencode('.jpg', frame)
a = arr.tostring()
fp = open(filename, 'wb')
fp.write(a)
fp.close()
except Exception as e:
print(str(e))
cap.release()
cap.release()
if name == '__main__':
# argv1 rtsp stream url# argv2 jpg file path and name
rtspurl = sys.argv[1]
jpgfile = sys.argv[2]
snaprtsp(rtspurl, jpgfile)
result = os.path.exists(jpgfile)
i = 1
while not result:
if i > 3 :
break
snaprtsp(rtspurl, jpgfile)
result = os.path.exists(jpgfile)
i = i + 1
if result :
print("Sucess")
else:
回答:
请问楼主解决了吗?请指教
以上是 关于opencv python 获取视频流的一帧 的全部内容, 来源链接: utcz.com/a/165306.html