python-opencv
read video
import cv2
video_data=cv2.VideoCapture(video)
rval,frame=video_data.read()
full_frame_num=int(video_data.get(cv2.CAP_PROP_FRAME_COUNT))
#print("######full_frame_num:{}".format(full_frame_num))
frame_num=0
#for frame_num in range(full_frame_num):
while rval:
frame=imresize(frame,img_size)
rval,frame=video_data.read()
video_data.release()
edit video and save
def video_show(video_path):
if not os.path.exists(video_result_file):
os.makedirs(video_result_file)
video_result=os.path.join(video_result_file,video_path.split('/')[-1])
predict_file_name='{}_predict.txt'.format(video_path.split('/')[-1].split('.')[0])
predict_file=os.path.join(predict_file_path,predict_file_name)
data_list=cal_score(predict_file)
word_x=20
word_y=20
video_data=cv2.VideoCapture(video_path)
fps_video=video_data.get(cv2.CAP_PROP_FPS)
frame_width=int(video_data.get(CAP_PROP_FRAME_WIDTH))
frame_height=int(video_data.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter(video_result,fourcc, fps_video, (frame_width,frame_height))
#frame_num=int(video_data.get(cv2.CAP_PROP_FRAME_COUNT))
frame_id=0
rval,frame=video_data.read()
while rval:
cv2.putText(frame, 'score:%.6f' %data_list[frame_id], (word_x, word_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,255), 2)
#print("frame_id:{}".format(frame_id))
out.write(frame)
cv2.imshow('video',frame)
cv2.waitKey(1) ######important!!!
rval,frame=video_data.read()
frame_id+=1
video_data.release()
out.release()
cv2.destroyAllWindows()
创建VideoWriter时图像的高宽要与write写入的高宽一致,否则写入不成功;
imshow时加入waitKey才能看到显示的视频。