【发布时间】:2017-10-28 09:24:54
【问题描述】:
我正在尝试从视频中读取帧,但提取帧后视频无用。所以我决定使用 TempFile 模块。但是我使用cv2.VideoCapture(NamedTempFile.name) 获取的框架始终是黑色的。我想知道这是否是正确的方法。如果没有,有没有更好的方法?
我在下面附上了我的代码的视频捕获部分。
我也怀疑 get_temp_video 函数可能是错误的。
def get_temp_video(url, temp_file):
r = requests.get(url, stream=True)
for chunk in r.iter_content(chunk_size=1024):
if chunk:
temp_file.write(chunk)
return temp_file
def get_frame(video_url):
named_temp_file = NamedTemporaryFile()
named_temp_file = get_temp_video(video_url, named_temp_file)
named_temp_file.seek(0)
video = cv2.VideoCapture(named_temp_file.name)
while video.isOpened():
ret, frame = video.read()
if ret:
temp_file = TemporaryFile()
np.save(temp_file, frame)
temp_file.seek(0)
upload_to_some_where(temp_file.read())
temp_file.close()
else:
break
video.release()
named_temp_file.close()
【问题讨论】:
标签: python opencv video temporary-files