python - 将 Numpy 数组转换为视频(mp4)
问题描述
我有一个视频(mp4),我将其读入 Numpy 数组并进行编辑。现在我想将数组转换回视频
我已经尝试使用 VideoWriter 但创建的 mp4 总是损坏
frame_array = []
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while cap.isOpened():
ret, image_np = cap.read()
if ret == True:
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
i=i+1
frame_array.append(image_np)
cv2.imwrite('pictures/'+ str(i) + '.jpg',image_np)
else:
break
print("Everything detected")
out = cv2.VideoWriter('video.mp4',cv2.VideoWriter_fourcc(*'DIVX'), 30, (640, 480))
for i in frame_array:
out.write(i)
out.release()
创建的 mp4 总是损坏,无法打开。每个保存的图片cv2.imwrite
都可以并且可以查看
解决方案
推荐阅读
- java - FeignClient 使用 application/x-www-form-urlencoded 正文创建 POST
- terraform - 如何将 Terraform 降级到以前的版本?
- python - ModuleNotFoundError:没有名为“sklearn.externals.six”的模块
- typescript - 获取记录键
(或使用推断的键和类型值定义对象)在 TypeScript 中 - python - 为什么不打印最大值和最小值?
- azure - Azure APIM:在自托管下开发时,站点可以在非编辑模式下启动吗?
- xslt - 根据存储在另一个元素中的键选择 XML 元素
- kubernetes - Kubernetes 延迟部署
- swift - 我们如何将 UITapGestureRecognizer 分配给另一个视图?
- javascript - 页面上有多个 javascript 计数器