Demo entry 6641489

#生成video代码,可以直接运行

   

Submitted by liulongxiao on Sep 18, 2017 at 17:09
Language: Python 3. Code size: 2.4 kB.

import cv2
from scipy.io import loadmat
import numpy as np


def convert_vedio(model, vedio_filename, out, fps=None):
    cap = cv2.VideoCapture(vedio_filename)
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    input_fps = cap.get(cv2.CAP_PROP_FPS)
    if fps is None:
        fps = input_fps
    every_pereps = int(input_fps // fps)
    video = cv2.VideoWriter(
        out,
        cv2.VideoWriter_fourcc('M', 'P', '4', '2'),
        fps,
        size
    )
    i = 0
    while True:
        _, frame = cap.read()
        i += 1
        print(i)
        if not _:
            break
        if (i % every_pereps == 0):
            video.write(model.predict(frame))
    video.release()
    cap.release()



def captureit(model, vedio_mat,CLASSES,out,every_N_fps=1):
    data=loadmat(vedio_mat)['Ori_H']
    total_fps=data.shape[2]
    video = cv2.VideoWriter(
        out,
        cv2.VideoWriter_fourcc('M', 'P', '4', '2'),
        30//every_N_fps,
        (data.shape[1],data.shape[0])
    )
    caps=[]
    for i in range(total_fps):
        if i%every_N_fps==0:
            pic=np.stack([data[:,:,i],data[:,:,i],data[:,:,i]],axis=-1).copy()
            image_handeled,flag=model.detect_and_predict(pic,CLASSES,thresh_hold=0.5)
            video.write(image_handeled)
            if flag:
                caps.append(i)
    video.release()
    return caps

def array_to_vedio(thearray,out,fps=30):
    video = cv2.VideoWriter(
        out,
        cv2.VideoWriter_fourcc('M', 'P', '4', '2'),
        fps,
        (thearray.shape[1], thearray.shape[0])
    )
    for i in range(thearray.shape[2]):
        video.write(thearray[:,:,i])
    video.release()






if __name__ == '__main__':
    from my_model_detection import CONFIG_bisai, TheModel,CONFIG_resnet
    import tensorflow as tf
    import os
    train_path='ques3/mat'
    save_path='ques3/result'
    filenames=os.listdir('ques3/mat')
    filenames=filenames[4:]
    m = TheModel(CONFIG_bisai)
    m.inference()
    for filename in filenames:
        result=captureit(m,vedio_mat=os.path.join(train_path,filename),CLASSES=[1],out=os.path.join(save_path,filename+'.avi'),every_N_fps=1)
        with open(os.path.join(save_path,filename+'.txt'),'w') as f:
            f.write(result.__repr__())

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).