Python imageio.get_reader() Examples
The following are 30
code examples of imageio.get_reader().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
imageio
, or try the search function
.
Example #1
Source File: fsmedia.py From faceswap with GNU General Public License v3.0 | 7 votes |
def _load_one_video_frame(self, frame_no): """ Obtain a single frame from a video file. Parameters ---------- frame_no: int The frame index for the required frame Returns ------ :class:`numpy.ndarray` The image for the requested frame index, """ logger.trace("Loading video frame: %s", frame_no) reader = imageio.get_reader(self._args.input_dir, "ffmpeg") reader.set_image_index(frame_no - 1) frame = reader.get_next_data()[:, :, ::-1] reader.close() return frame
Example #2
Source File: ozy_producer.py From ozymandias with MIT License | 6 votes |
def main(n): """Stream the video into a Kafka producer in an infinite loop""" topic = choose_channel(n) video_reader = imageio.get_reader(DATA + topic + '.mp4', 'ffmpeg') metadata = video_reader.get_meta_data() fps = metadata['fps'] producer = KafkaProducer(bootstrap_servers='localhost:9092', batch_size=15728640, linger_ms=1000, max_request_size=15728640, value_serializer=lambda v: json.dumps(v.tolist())) while True: video_loop(video_reader, producer, topic, fps)
Example #3
Source File: video.py From LipReading with MIT License | 6 votes |
def __init__(self, vid_path, start=0, seq_len=1, fps=29.97): """ The VideoReader serves a generator of frame-sequences as needed. To increase performance, sequential frame access is enforced, and frames are cached into a buffer to allow for quicker repeated and sequential accesses within the allocated buffer. TODO: Buffer implementation. Could be implemented by taking a cache_size param for max(caption_size) and keeping track lo/hi indices for when to update. See `_updateCache` for draft. Note memory usage may grow significantly with high-resolution video or large cache sizes, calculated by the following formula: ``` bytes = seq_len * height * width * channels ``` A 1080p (1920x1080) video with sequence length of 30, or approximately 1 second of 30fps footage equates to: ``` 30 * 1080 * 1920 * 3 bytes ~ 186MB ``` :param vid_path: Path of the video to read from. :param start: The starting frame index to begin reading from. :param seq_len: The length of the sequence of frames to serve. """ assert os.path.isfile(vid_path) reader = imageio.get_reader(vid_path) vid_len = reader.get_length() # State. self.lo = start self._reader = reader self.cache = collections.deque(maxlen=seq_len) self.buf = np.empty(shape=(seq_len,), dtype=np.ndarray) # For convenience. self._seq_len = seq_len self._vid_len = vid_len self._fps = fps
Example #4
Source File: simple_detect_actions_on_tube.py From ACAM_Demo with MIT License | 6 votes |
def main(): reader = imageio.get_reader("person_0_tube.mp4") frames = [] for cur_frame in reader: frames.append(cur_frame) input_tube = np.stack(frames[:32], axis=0) input_tube = np.expand_dims(input_tube, axis=0) # batch dimension detector_dict = set_up_detector() prediction_probabilites = detect_on_tube(input_tube, detector_dict) top_k = 5 top_classes = np.argsort(prediction_probabilites[0,:])[:-top_k-1:-1] print("Results") for ii in range(top_k): class_id = top_classes[ii] class_str = act.ACTION_STRINGS[class_id] class_prob = prediction_probabilites[0,class_id] print("%.10s : %.3f" % (class_str, class_prob)) cv2.imshow('midframe', input_tube[0,16,:,:,::-1]) cv2.waitKey(0)
Example #5
Source File: avi_to_lmdb.py From snn_toolbox with MIT License | 6 votes |
def avi_to_frame_list(avi_filename, gray): """Creates a list of frames starting from an AVI movie. Inverts axes to have num_channels, height, width in this order. Parameters ---------- avi_filename: name of the AVI movie gray: if True, the resulting images are treated as grey images with only one channel. If False, the images have three channels. """ print('Loading {}'.format(avi_filename)) vid = imageio.get_reader(avi_filename, 'ffmpeg') if gray: data = [np.mean(np.moveaxis(im, 2, 0), axis=0, keepdims=True) for im in vid.iter_data()] print('Loaded grayscale images.') else: data = [np.moveaxis(im, 2, 0) for im in vid.iter_data()] print('Loaded RGB images.') return data
Example #6
Source File: test_screenrecord.py From uiautomator2 with MIT License | 6 votes |
def test_screenrecord(d: u2.Device): with pytest.raises(RuntimeError): d.screenrecord.stop() d.screenrecord("output.mp4", fps=10) start = time.time() with pytest.raises(RuntimeError): d.screenrecord("output2.mp4") time.sleep(3.0) d.screenrecord.stop() print("Time used:", time.time() - start) # check with imageio.get_reader("output.mp4") as f: meta = f.get_meta_data() assert isinstance(meta, dict) from pprint import pprint pprint(meta)
Example #7
Source File: image.py From faceswap with GNU General Public License v3.0 | 6 votes |
def _from_video(self): """ Generator for loading frames from a video Yields ------ filename: str The dummy filename of the loaded video frame. image: numpy.ndarray The loaded video frame. """ logger.debug("Loading frames from video: '%s'", self.location) reader = imageio.get_reader(self.location, "ffmpeg") for idx, frame in enumerate(reader): if idx in self._skip_list: logger.trace("Skipping frame %s due to skip list", idx) continue # Convert to BGR for cv2 compatibility frame = frame[:, :, ::-1] filename = self._dummy_video_framename(idx) logger.trace("Loading video frame: '%s'", filename) yield filename, frame reader.close()
Example #8
Source File: image.py From faceswap with GNU General Public License v3.0 | 6 votes |
def _get_fps(self): """ Get the Frames per Second. If the input is a folder of images than 25.0 will be returned, as it is not possible to calculate the fps just from frames alone. For video files the correct FPS will be returned. Returns ------- float: The Frames per Second of the input sources """ if self._is_video: reader = imageio.get_reader(self.location, "ffmpeg") retval = reader.get_meta_data()["fps"] reader.close() else: retval = 25.0 logger.debug(retval) return retval
Example #9
Source File: image.py From faceswap with GNU General Public License v3.0 | 5 votes |
def _get_count_and_filelist(self, fast_count, count): if self._is_video: self._reader = imageio.get_reader(self.location, "ffmpeg") self._reader.use_patch = True count, video_meta_data = self._reader.get_frame_info( frame_pts=self._video_meta_data.get("pts_time", None), keyframes=self._video_meta_data.get("keyframes", None)) self._video_meta_data = video_meta_data super()._get_count_and_filelist(fast_count, count)
Example #10
Source File: video_preprocess.py From ozymandias with MIT License | 5 votes |
def main(): """Preprocess all video files in `raw/` but ignore malformed videos""" video_files = glob.glob('raw/*.mp4') for vf in video_files: reader = imageio.get_reader(vf, format='ffmpeg') writer = imageio.get_writer(vf.replace('raw/', 'data/'), format='ffmpeg') try: format_frames(reader, writer) except RuntimeError: continue
Example #11
Source File: Project-GUI.py From Traffic-Signal-Violation-Detection-System with GNU General Public License v3.0 | 5 votes |
def open_file(self): self.filename = filedialog.askopenfilename() cap = cv2.VideoCapture(self.filename) reader = imageio.get_reader(self.filename) fps = reader.get_meta_data()['fps'] ret, image = cap.read() cv2.imwrite('G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/preview.jpg', image) self.show_image('G:/Traffic Violation Detection/Traffic Signal Violation Detection System/Images/preview.jpg')
Example #12
Source File: facerec_train.py From deepvisualminer with MIT License | 5 votes |
def detectvideo(vid_file, detector_xml_path, dest_img_dir): if not os.path.exists(dest_img_dir): os.makedirs(dest_img_dir) detector = cv2.CascadeClassifier(detector_xml_path) vid = imageio.get_reader(vid_file, 'ffmpeg') # If size and source_size are not equal, then device was probably # rotated (like a mobile) and we should compensate for the rotation. # Images will have 'source_size' dimensions but we need 'size'. metadata = vid.get_meta_data() rotate = False if metadata['source_size'] != metadata['size']: print('Rotating') rotate = True for i, img in enumerate(vid): if rotate: #img = np.transpose(img, axes=(1, 0, 2)).copy() img = np.rot90(img).copy() print('Frame ',i, img.shape) gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) min_size = (min(20, gray_img.shape[0] // 10), min(20, gray_img.shape[1] // 10)) hits = detector.detectMultiScale(gray_img, 1.1, 3, 0, min_size) #cv2.groupRectangles(hits, 2) print(len(hits), ' hits') hits_img = np.copy(img) if len(hits) > 0: for (x,y,w,h) in hits: cv2.rectangle(hits_img, (x,y), (x+w, y+h), (0,0,255), 2) cv2.imwrite(os.path.join(dest_img_dir, 'frame-%d.png'%(i)), hits_img)
Example #13
Source File: video_utils.py From videograph with GNU General Public License v3.0 | 5 votes |
def read_frames_imageio(video_fullpath, frames_save_dir): ''' much better/faster way to read video frames :param video_fullpath: :return: ''' vid = imageio.get_reader(video_fullpath, 'ffmpeg') for i, frame in enumerate(vid): frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_file_name = frames_save_dir + str(i) + '.jpeg' cv2.imwrite(frame_file_name, frame)
Example #14
Source File: utils.py From iclr2017mcnet with MIT License | 5 votes |
def load_kth_data(f_name, data_path, image_size, K, T): flip = np.random.binomial(1,.5,1)[0] tokens = f_name.split() vid_path = data_path + tokens[0] + "_uncomp.avi" vid = imageio.get_reader(vid_path,"ffmpeg") low = int(tokens[1]) high = np.min([int(tokens[2]),vid.get_length()])-K-T+1 if low == high: stidx = 0 else: if low >= high: print(vid_path) stidx = np.random.randint(low=low, high=high) seq = np.zeros((image_size, image_size, K+T, 1), dtype="float32") for t in xrange(K+T): img = cv2.cvtColor(cv2.resize(vid.get_data(stidx+t), (image_size,image_size)), cv2.COLOR_RGB2GRAY) seq[:,:,t] = transform(img[:,:,None]) if flip == 1: seq = seq[:,::-1] diff = np.zeros((image_size, image_size, K-1, 1), dtype="float32") for t in xrange(1,K): prev = inverse_transform(seq[:,:,t-1]) next = inverse_transform(seq[:,:,t]) diff[:,:,t-1] = next.astype("float32")-prev.astype("float32") return seq, diff
Example #15
Source File: effmpeg.py From faceswap with GNU General Public License v3.0 | 5 votes |
def get_fps(input_=None, print_=False, **kwargs): """ Get Frames per Second """ logger.debug("input_: %s, print_: %s, kwargs: %s", input_, print_, kwargs) input_ = input_ if isinstance(input_, str) else input_.path logger.debug("input: %s", input_) reader = imageio.get_reader(input_, "ffmpeg") _fps = reader.get_meta_data()["fps"] logger.debug(_fps) reader.close() if print_: logger.info("Video fps: %s", _fps) return _fps
Example #16
Source File: effmpeg.py From faceswap with GNU General Public License v3.0 | 5 votes |
def get_info(input_=None, print_=False, **kwargs): """ Get video Info """ logger.debug("input_: %s, print_: %s, kwargs: %s", input_, print_, kwargs) input_ = input_ if isinstance(input_, str) else input_.path logger.debug("input: %s", input_) reader = imageio.get_reader(input_, "ffmpeg") out = reader.get_meta_data() logger.debug(out) reader.close() if print_: logger.info("======== Video Info ========",) logger.info("path: %s", input_) for key, val in out.items(): logger.info("%s: %s", key, val) return out
Example #17
Source File: ffmpeg.py From faceswap with GNU General Public License v3.0 | 5 votes |
def video_fps(self): """ Return the fps of source video """ reader = imageio.get_reader(self.source_video, "ffmpeg") retval = reader.get_meta_data()["fps"] reader.close() logger.debug(retval) return retval
Example #18
Source File: utils.py From video-captioning with MIT License | 5 votes |
def playVideo(video_urls): video = imageio.get_reader(YOUTUBE_CLIPS_DIR + video_urls[0] + '.avi','ffmpeg') for frame in video: fr = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) cv2.imshow('frame',fr) if cv2.waitKey(40) & 0xFF == ord('q'): break cv2.destroyAllWindows()
Example #19
Source File: videocs.py From DeepVideoCS with BSD 2-Clause "Simplified" License | 5 votes |
def default_loader(path): reader = imageio.get_reader(path) video = np.zeros((reader._meta['nframes'], reader._meta['size'] [1], reader._meta['size'][0]), dtype=np.uint8) for i, im in enumerate(reader): video[i, :, :] = im.mean(2) return video
Example #20
Source File: utils.py From video_frame_remover with MIT License | 5 votes |
def get_frames(vid_filename, n_sample_frames=100): vid = imageio.get_reader(vid_filename, 'ffmpeg') if n_sample_frames is None: nums = range(0, len(vid)) else: nums = np.linspace(0, len(vid) - n_sample_frames, n_sample_frames, dtype=int) frames = [] for num in nums: try: image = vid.get_data(num) except RuntimeError: pass frames.append(image) return np.array(frames)
Example #21
Source File: default_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _get_data(file: Path, index: int) -> np.ndarray: with imageio.get_reader(file) as reader: return np.asarray(reader.get_data(index))
Example #22
Source File: pipeline.py From MesoNet with Apache License 2.0 | 5 votes |
def __init__(self, path): self.path = path self.container = imageio.get_reader(path, 'ffmpeg') self.length = self.container.count_frames() self.fps = self.container.get_meta_data()['fps']
Example #23
Source File: test_obj_detection.py From ACAM_Demo with MIT License | 5 votes |
def test_local_video(): main_folder_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) obj_detection_graph = os.path.join(main_folder_path, 'object_detection/weights/batched_zoo/faster_rcnn_nas_coco_2018_01_28/batched_graph/frozen_inference_graph.pb') print("Loading object detection model at %s" % obj_detection_graph) Obj_Detector = obj.Object_Detector(obj_detection_graph) test_vid_path = "chase1Person1View3Point0.mp4" print('Testing on %s' % test_vid_path) reader = imageio.get_reader(test_vid_path, 'ffmpeg') fps = reader.get_meta_data()['fps'] // 2 out_vid_path = "chase1Person1View3Point0_out.mp4" writer = imageio.get_writer(out_vid_path, fps=fps) print("Writing output video on %s" %out_vid_path) frame_cnt = 0 for test_img in reader: frame_cnt += 1 if frame_cnt % 2 == 0: continue print("frame_cnt: %i" %frame_cnt) expanded_img = np.expand_dims(test_img, axis=0) detection_list = Obj_Detector.detect_objects_in_np(expanded_img) out_img = visualize_results(test_img, detection_list, display=False) writer.append_data(out_img) writer.close()
Example #24
Source File: test_obj_detection.py From ACAM_Demo with MIT License | 5 votes |
def test_tracking_local_video(): main_folder_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) # obj_detection_graph = os.path.join(main_folder_path, 'object_detection/weights/batched_zoo/faster_rcnn_nas_coco_2018_01_28/batched_graph/frozen_inference_graph.pb') obj_detection_graph = os.path.join(main_folder_path, 'object_detection/weights/batched_zoo/faster_rcnn_nas_lowproposals_coco_2018_01_28/batched_graph/frozen_inference_graph.pb') print("Loading object detection model at %s" % obj_detection_graph) Obj_Detector = obj.Object_Detector(obj_detection_graph) Tracker = obj.Tracker() #test_vid_path = "chase1Person1View3Point0.mp4" #test_vid_path = "VIRAT_S_000003_9_00.mp4" test_vid_path = "VIRAT_S_000101_1_00.mp4" print('Testing on %s' % test_vid_path) reader = imageio.get_reader(test_vid_path, 'ffmpeg') fps = reader.get_meta_data()['fps'] // 2 #out_vid_path = "chase1Person1View3Point0_out.mp4" #out_vid_path = "VIRAT_S_000003_9_00_out.mp4" out_vid_path = "VIRAT_S_000101_1_00_out.mp4" writer = imageio.get_writer(out_vid_path, fps=fps) print("Writing output video on %s" %out_vid_path) frame_cnt = 0 for test_img in reader: frame_cnt += 1 if frame_cnt % 2 == 0: continue print("frame_cnt: %i" %frame_cnt) expanded_img = np.expand_dims(test_img, axis=0) detection_list = Obj_Detector.detect_objects_in_np(expanded_img) detection_info = [info[0] for info in detection_list] Tracker.update_tracker(detection_info, test_img) #print(Tracker.active_actors) out_img = visualize_results_from_tracking(test_img, Tracker.active_actors, Tracker.inactive_actors, display=False) writer.append_data(out_img) writer.close()
Example #25
Source File: utils.py From self-attention-GAN-pytorch with MIT License | 5 votes |
def make_gif(image, iteration_number, save_path, model_name, max_frames_per_gif=100): # Make gif gif_frames = [] # Read old gif frames try: gif_frames_reader = imageio.get_reader(os.path.join(save_path, model_name + ".gif")) for frame in gif_frames_reader: gif_frames.append(frame[:, :, :3]) except: pass # Append new frame im = cv2.putText(np.concatenate((np.zeros((32, image.shape[1], image.shape[2])), image), axis=0), 'iter %s' % str(iteration_number), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, .5, (255, 255, 255), 1, cv2.LINE_AA).astype('uint8') gif_frames.append(im) # If frames exceeds, save as different file if len(gif_frames) > max_frames_per_gif: print("Splitting the GIF...") gif_frames_00 = gif_frames[:max_frames_per_gif] num_of_gifs_already_saved = len(glob.glob(os.path.join(save_path, model_name + "_*.gif"))) print("Saving", os.path.join(save_path, model_name + "_%05d.gif" % (num_of_gifs_already_saved))) imageio.mimsave(os.path.join(save_path, model_name + "_%05d.gif" % (num_of_gifs_already_saved)), gif_frames_00) gif_frames = gif_frames[max_frames_per_gif:] # Save gif # print("Saving", os.path.join(save_path, model_name + ".gif")) imageio.mimsave(os.path.join(save_path, model_name + ".gif"), gif_frames)
Example #26
Source File: extract_frames.py From sepconv with MIT License | 5 votes |
def extract_frames(video_path): def convert_frame(arg): return Image.fromarray(arg[:, :, :3], mode='RGB') video_reader = imageio.get_reader(video_path) fps = video_reader.get_meta_data().get('fps', None) frames = [convert_frame(x) for x in video_reader] return frames, fps
Example #27
Source File: utils.py From iclr2017mcnet with MIT License | 5 votes |
def load_s1m_data(f_name, data_path, trainlist, K, T): flip = np.random.binomial(1,.5,1)[0] vid_path = data_path + f_name img_size = [240,320] while True: try: vid = imageio.get_reader(vid_path,"ffmpeg") low = 1 high = vid.get_length()-K-T+1 if low == high: stidx = 0 else: stidx = np.random.randint(low=low, high=high) seq = np.zeros((img_size[0], img_size[1], K+T, 3), dtype="float32") for t in xrange(K+T): img = cv2.resize(vid.get_data(stidx+t), (img_size[1],img_size[0]))[:,:,::-1] seq[:,:,t] = transform(img) if flip == 1: seq = seq[:,::-1] diff = np.zeros((img_size[0], img_size[1], K-1, 1), dtype="float32") for t in xrange(1,K): prev = inverse_transform(seq[:,:,t-1])*255 prev = cv2.cvtColor(prev.astype("uint8"),cv2.COLOR_BGR2GRAY) next = inverse_transform(seq[:,:,t])*255 next = cv2.cvtColor(next.astype("uint8"),cv2.COLOR_BGR2GRAY) diff[:,:,t-1,0] = (next.astype("float32")-prev.astype("float32"))/255. break except Exception: # In case the current video is bad load a random one rep_idx = np.random.randint(low=0, high=len(trainlist)) f_name = trainlist[rep_idx] vid_path = data_path + f_name return seq, diff
Example #28
Source File: default_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _read_delayed(self) -> da.core.Array: with imageio.get_reader(self._file) as reader: # Store length as it is used a bunch image_length = reader.get_length() # Handle single image formats like png, jpeg, etc if image_length == 1: return da.from_array(self._get_data(self._file, 0)) # Handle many image formats like gif, mp4, etc elif image_length > 1: # Get a sample image sample = self._get_data(self._file, 0) # Create operating shape for the final dask array by prepending # image length to a tuple of ones that is the same length as # the sample shape operating_shape = (image_length,) + ((1,) * len(sample.shape)) # Create numpy array of empty arrays for delayed get data # functions lazy_arrays = np.ndarray(operating_shape, dtype=object) for indicies, _ in np.ndenumerate(lazy_arrays): lazy_arrays[indicies] = da.from_delayed( delayed(self._get_data)(self._file, indicies[0]), shape=sample.shape, dtype=sample.dtype, ) # Block them into a single dask array return da.block(lazy_arrays.tolist()) # Catch all other image types as unsupported # https://imageio.readthedocs.io/en/stable/userapi.html#imageio.core.format.Reader.get_length else: raise exceptions.UnsupportedFileFormatError(self._file)
Example #29
Source File: default_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _read_immediate(self) -> np.ndarray: try: with imageio.get_reader(self._file) as reader: # Store length as it is used a bunch image_length = reader.get_length() # Handle single image formats like png, jpeg, etc if image_length == 1: return reader.get_data(0) # Handle many image formats like gif, mp4, etc elif image_length > 1: frames = [] for frame in reader.iter_data(): frames.append(frame) return np.stack(frames) # Catch all other image types as unsupported # https://imageio.readthedocs.io/en/stable/userapi.html#imageio.core.format.Reader.get_length else: raise exceptions.UnsupportedFileFormatError(self._file) # Reraise unsupported file format except exceptions.UnsupportedFileFormatError: raise exceptions.UnsupportedFileFormatError(self._file)
Example #30
Source File: default_reader.py From aicsimageio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def metadata(self) -> Dict[str, Any]: if self._metadata is None: with imageio.get_reader(self._file) as reader: self._metadata = reader.get_meta_data() return self._metadata