python-opencv

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才能看到显示的视频。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值