首页 > 解决方案 > 将 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都可以并且可以查看

标签: pythonpython-3.xnumpyvideo

解决方案


推荐阅读