def __init__(self, video_path, pred_path): # Dealing with paths self.video_path = video_path self.pred_path = pred_path self.vid_name_ext = os.path.split(video_path)[1] self.vid_name_root, self.vid_ext = os.path.splitext(self.vid_name_ext) # Operational parameters self.time_display = 150 # range of frame when plotting graph mpl.rcParams.update({'font.size': 8}) # Video data related print("\n=================================\nNow inferring video {}". format(self.vid_name_root)) print("Loading video data and prediction arrays...") self.vid_reader = skv.FFmpegReader(video_path) self.vid_shape = self.vid_reader.getShape() self.predictions = np.load( pred_path) / 255 # prediction data should be stored as np.uint8 self.predictions_masked = np.ma.masked_where(self.predictions < 0.5, self.predictions) # Lists/ data recorder self.rotation_results = []
def sample_video_frames(video_file, frame_idxs): cap = cv2.VideoCapture(video_file) video_frames = [] n_frames = 0 while cap.isOpened(): ret, frame = cap.read() if ret: if n_frames in frame_idxs: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) video_frames.append(frame) else: break n_frames += 1 cap.release() if len(video_frames) == 0: # try loading from skvideo instead n_frames = 0 in_params = {'-vcodec': 'h264'} reader = vio.FFmpegReader(video_file, inputdict=in_params) for frame in reader.nextFrame(): if isinstance(frame, np.ndarray): if n_frames in frame_idxs: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) video_frames.append(frame) else: break n_frames += 1 reader.close() assert len(video_frames) == len(frame_idxs) return video_frames
def video_to_images(in_video_name): vid_path = os.path.join(trajectories_dir, video_dir, in_video_name) videogen = skvio.FFmpegReader(vid_path).nextFrame() images = [] for img in videogen: images.append(img) return images
def _get_video_info(self, video_src): video_name_with_ext = os.path.split(video_src)[1] video_name_root, ext = os.path.splitext(video_name_with_ext) vreader = skv.FFmpegReader(video_src) m, w, h, channels = vreader.getShape() image_scaling_factor = np.linalg.norm((240,320))/np.linalg.norm((h,w)) shape_correct = self._inspectVideoShape(w, h) return video_name_root, ext, vreader, (m, w, h, channels), shape_correct, image_scaling_factor
def load_video_generator(filename, ffmpeg_path=None, libav_path=None): """Returns a generator that can load a video from file frame-by-frame. This function returns a generator `reader` that can load a video from a file frame-by-frame. Every call to `reader.nextFrame()` will return a single frame of the video as a NumPy array with dimensions (M, N) or (M, N, C), where M is the height, N is the width, and C is the number of channels (will be either 1 for grayscale or 3 for RGB). Parameters ---------- filename : str Video file name ffmpeg_path : str, optional, default: system's default path Path to ffmpeg library. libav_path : str, optional, default: system's default path Path to libav library. Returns ------- reader : skvideo.io.FFmpegReader | skvideo.io.LibAVReader A Scikit-Video reader object Examples -------- >>> from skvideo import datasets >>> reader = load_video_generator(datasets.bikes()) >>> for frame in reader.nextFrame(): ... pass """ if not has_skvideo: raise ImportError("You do not have scikit-video installed. " "You can install it via $ pip install sk-video.") # Set the path if necessary set_skvideo_path(ffmpeg_path, libav_path) # Try loading if skvideo._HAS_FFMPEG: reader = svio.FFmpegReader(filename) elif skvideo._HAS_AVCONV: reader = svio.LibAVReader(filename) else: raise ImportError("You have neither ffmpeg nor libav (which comes " "with avprobe) installed.") logging.getLogger(__name__).info("Loaded video from file '%s'." % filename) return reader
def main(model_path, data_dir, prediction_path): # Grab all the input videos video_extensions = ["*.pgm", "*.mp4"] videos = [] for extension in video_extensions: video_paths = os.path.join(data_dir, extension) videos += glob(video_paths) # load Deep learning model model = load_model( model_path, custom_objects={"dice_coef_multilabel": dice_coef_multilabel}) # Loop for each video for vid in videos: # Define video's specific parameters vid_name = os.path.split(vid)[1] # with extension vid_ext = os.path.splitext(vid_name)[1] output_path = prediction_path + os.path.splitext(vid_name)[0] + ".npy" vid_reader = skv.FFmpegReader(vid) num_frames = vid_reader.getShape()[0] batch_array = np.zeros((num_frames, 240, 320, 4)).astype(np.uint8) print("Current video:", vid) print("with num_frames = ", num_frames) # Loop for each frame for idx, frame in enumerate(vid_reader.nextFrame()): # Printing progress print("\rNow is at %d/%d" % (idx, num_frames), end="", flush=True) # Preprocessing before DL network if vid_ext == ".pgm" or vid_name == "4.5mA_0.0ms_180s.mp4" or "max" in vid_name: frame = frame[:, 36:356, :] frame_gray = rgb2gray(frame) / 255 prediction = model.predict(frame_gray) batch_array[idx, ] = (prediction[0, ] * 255).astype(np.uint8) print("\n") print("Saving prediction: ", output_path) np.save(output_path, batch_array)
def read_video_frames(video_file, desired_fps): fps = get_video_fps(video_file) assert desired_fps <= fps, 'the video {} has {} fps'.format(video_file, fps) cap = cv2.VideoCapture(video_file) sample_period = round(fps / desired_fps) video_frames = [] n_frames = 0 while cap.isOpened(): ret, frame = cap.read() if ret: if int(n_frames % sample_period) == 0: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) video_frames.append(frame) else: break n_frames += 1 cap.release() # fallback method to load videos if len(video_frames) == 0: # try loading from skvideo instead n_frames = 0 in_params = {'-vcodec': 'h264'} reader = vio.FFmpegReader(video_file, inputdict=in_params) for frame in reader.nextFrame(): if isinstance(frame, np.ndarray): if int(n_frames % sample_period) == 0: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) video_frames.append(frame) else: break n_frames += 1 reader.close() assert len(video_frames) > 0, 'failed to read {}'.format(video_file) return video_frames
def setup(self, video_path): assert os.path.exists(video_path), 'Error! Path {} doesnot exist'.format(video_path) self.path = video_path if self.vreader == 0: #skvideo if self.cap is not None: self.vid.close() self.cap.close() self.cap = skvid.FFmpegReader(video_path) self.vid = self.cap.nextFrame() #a generator #self.frame_rate = float(self.cap.probeInfo['video']['@avg_frame_rate'].split('/')[0]) self.nframes = self.cap._probCountFrames() self.duration = self._get_duration(self.cap, round_up = False) #self.frame_rate = self.nframes / self.duration #obsolete elif self.vreader == 1: #opencv if self.cap is not None: self.cap.release() self.nframes, self.duration = get_opencv_nframes(video_path, True) self.duration_method = 'opencv_manual_loop' self.cap = cv2.VideoCapture(video_path) self.nframes_eff = int(self.duration / self.stride_in_sec) if self.stride_in_sec else self.nframes #self.fsteps = self.frame_rate*self.stride_in_sec if self.stride_in_sec else 1 #frame step as float, obsolete self.fid = 0 self.valid_fid = np.linspace(0, self.nframes, self.nframes_eff, endpoint = False).astype(int).tolist()
force_gpu_compatible=True) config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False, gpu_options=gpu_options) config.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_2 # start a session sess = tf.Session(config=config) # open video capture if FLAGS.video is "": print("Webcam reading not implemented. Exiting") quit() else: inputparameters = {} outputparameters = {} reader = skio.FFmpegReader(FLAGS.video, inputdict=inputparameters, outputdict=outputparameters) i = 0 for frame in reader.nextFrame(): # predict ch = predict_mask(frame, str(i), sess, pl, mask, FLAGS, DATA) if ch == 27: break # add to frame nr. i += 1 # clean up cv2.destroyAllWindows()
video_file = config['video_file'] prefix = config['prefix'] test_split = config['test_split'] video_begin = time.strptime(config['begin'], '%Y-%m-%d-%H-%M-%S') video_begin = time.mktime(video_begin) if module == 'cv2': reader = cv2.VideoCapture(video_file) nb_frames = int(reader.get(CV_CAP_PROP_FRAME_COUNT)) fps = reader.get(CAP_PROP_FPS) elif module == 'skvideo': import skvideo.io as skio print('loading video using skvideo, this may take some time...') reader = skio.FFmpegReader(video_file) nb_frames = reader.inputframenum fps = reader.inputfps labels = np.ones(nb_frames, dtype=np.int16) * -1 # initialize label as -1 time_in_secs = np.arange(nb_frames) / fps + video_begin train_indices = [] test_indices = [] # process annotation annotations = config['annotation'] for annotation in config['annotation']: type_ = annotation['type'] begin = time.strptime(annotation['begin'], '%Y-%m-%d-%H-%M-%S') end = time.strptime(annotation['end'], '%Y-%m-%d-%H-%M-%S') begin = time.mktime(begin) end = time.mktime(end)
import numpy as np import cv2 import skvideo.io as sk cap = sk.FFmpegReader('../input/video/02.avi') Cascade = cv2.CascadeClassifier('../data/model/lbp_17/cascade.xml') i = 0 for frame in cap.nextFrame(): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) point = Cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=100, minSize=(120, 120), maxSize=(140, 140)) if len(point) > 0 and i % 2 == 0: for rect in point: cv2.rectangle(gray, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0,255), thickness=2) i += 1 cv2.imshow('frame',gray) cv2.waitKey(1) point = None cap.release() cv2.destroyAllWindows()
def __enter__(self): self.reader = _vio.FFmpegReader(self.path) self.proc.__start__(self.name) print_status("processing: {0}".format(self.name), end='', flush=True) return self
help='Path to the output video file') parser.add_argument('-saved_model', required=True, help='Path to the trained model') parser.add_argument('-device', default=0, type=int, help='CUDA device') parser.add_argument('-axis', default=1, type=int, help='Axis to stack RGB image and depth prediction') args = parser.parse_args() # CUDA device device = "cuda:{}".format(args.device) if torch.cuda.is_available() else 'cpu' # Create the reader and writer reader = io.FFmpegReader(filename=args.video_in) writer = io.FFmpegWriter(filename=args.video_out) # Trained model model = networks.MultiPurposeCNN().to(device) model.load_state_dict(torch.load(args.saved_model)) model.eval() cmapper = cm.get_cmap('plasma') idx = 0 for frame_in in reader.nextFrame(): idx += 1 print("Predicting frame {:05d} of {}".format(idx, reader.getShape()[0])) # Predict depth
def __init__(self, input_video_path, output_video_path): self.vreader = skv.FFmpegReader(input_video_path) self.vwriter = skv.FFmpegWriter(output_video_path) self.vid_name = fullfile(input_video_path)[0] self.vid_name_root = fullfile(input_video_path)[1][1] self.num_frames, self.vid_h, self.vid_w, self.vid_channels = self.vreader.getShape()
import numpy as np import os from glob import glob # weights_dir = 'experiment_results/visualisation/delayed_activation/gen-15_species-585/weights' # weights_paths = sorted(glob(os.path.join(weights_dir, "*"))) # for idx,w_path in enumerate(weights_paths): # w = np.load(w_path) # print("%d: num of nan = %d" % (idx, np.sum(np.isnan(w)))) # if idx > 30: # break import numpy as np import skvideo.io as skv import skvideo.datasets filename = skvideo.datasets.bigbuckbunny() vid_in = skv.FFmpegReader(filename) data = skv.ffprobe(filename)['video'] rate = data['@r_frame_rate'] T = np.int(data['@nb_frames']) print(type(rate)) print(rate)