本文主要是介绍怎样将图片转视频 avi 格式 cv2.VideoWriter cv2.VideoWriter_fourcc,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
cap = cv2.VideoCapture(“Ped2.mp4”)#读取视频
fps = int(cap.get(cv2.CAP_PROP_FPS))#读取视频FPS值
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), #读取视频大小
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*‘MJPG’)#编码方式
videoWriter1 = cv2.VideoWriter(‘Ped_Detect2.avi’,fourcc, fps, size) #创建写入对象
ret, frame = cap.read()#读取视频
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with detection_graph.as_default():
with tf.Session(graph=detection_graph , config=config) as sess:
while ret:
start = time.clock()
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break
image_np = frame
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)
end = time.clock()
print(‘frame:’, 1.0 / (end - start))
ret, frame = cap.read()
videoWriter1.write(image_np)
cv2.imshow(“capture”, image_np)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
将视频保存下来
import cv2 # 3.4.2
import os
fps = 24 # 视频帧率
print(cv2.__version__) # 3.4.2
# fourcc = cv2.CV_FOURCC('M', 'J', 'P', 'G')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
videoWriter = cv2.VideoWriter('123.avi', fourcc, fps, (1280, 720)) # (1280, 720)为视频大小
img_names = os.listdir("carData")
img_names.sort()
for i in range(0, len(img_names)): img_path = os.path.join("carData", img_names[i]) img = cv2.imread(img_path) # cv2.imshow('img', img12) # cv2.waitKey(1000/int(fps)) videoWriter.write(img)
videoWriter.release()
print("finish\n")
在使用视频播放器对存储视频进行播放的时候,出现了以下错误:
这篇关于怎样将图片转视频 avi 格式 cv2.VideoWriter cv2.VideoWriter_fourcc的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!