def create_video(frames, name, train_step_number, last_index, episode_reward_sum): if len(frames): tmp4_fname = os.path.join( model_base_filedir, "ATARI_step%010d_%s_%06d_%03d.mp4" % (train_step_number, name, last_index, episode_reward_sum)) vwrite(tmp4_fname, np.array(frames, dtype=np.uint8))
def play_bson_silhouette(days_folder, start, end, room): 'G:\HouseData-Sphere\4954\backups' day = '{}-{:02}-{:02}'.format(start.year, start.month, start.day) bson_path = os.path.join(days_folder, day, 'sphere', 'VID.bson') if os.path.exists(bson_path): use_bzip = False else: use_bzip = True bson_path += '.bz2' bson_data = BsonFile(bson_path) room_uid = read_room_uid(os.path.dirname(bson_path), room, use_bzip) json_data = [] for entry in tqdm(bson_data.data): entry_time = entry['bt'] entry_room = entry['uid'] if entry_room == room_uid and entry_time >= start and entry_time <= end: json_data.append(entry) silhouettes = [ bf['e'][0]['v'] for bf in json_data if bf['e'][0]['n'] == 'silhouette' ] sample = b64_image_to_numpy(silhouettes[0]) video = np.zeros((len(silhouettes), sample.shape[0], sample.shape[1]), dtype=np.uint8) for i, silhouette in enumerate(tqdm(silhouettes)): video[i, ...] = b64_image_to_numpy(silhouette) video_name = 'bson_{}.mp4'.format(start).replace(':', '.') vwrite(video_name, video) print('Frames from {} to {} were saved to {}'.format( start, end, video_name))
def extract_data(self): from skvideo.io import vwrite from os import walk for (session_dir, _j, scenes) in list(walk(self.sessions_path)): for scene in scenes: scene_dict = self.x.extract_signs_from( osp.join(session_dir, scene)) for key, value in scene_dict.items(): vwrite(osp.join(self.videos_path, key + '.mov'), value)
def gif(filename, image_list): fname, _ = os.path.splitext(filename) filename = fname + '.mp4' np_images = [] for m in image_list: np_images.append(m.permute(1, 2, 0).numpy() * 255) vwrite(filename, np_images, verbosity=0) return
def write_file(colored_frames, fps, output_type, output_wihout_ext, video_number): name = "{}_{}{}".format(output_wihout_ext, video_number, output_type) \ if video_number else "{}{}".format(output_wihout_ext, output_type) fps_string = "{:.2f}".format(fps) io.vwrite(name, colored_frames, inputdict={"-framerate": fps_string}, outputdict={"-r": fps_string}) # WRITE VIDEO print("\nwritten to {}".format(name))
def read_depth_video(videoname, framesdir, shape=(480, 640, 1), dtype=np.uint16): vid_data = read_frame_data( **default_read_z16_args(framesdir, shape=shape, dtype=dtype)) vid_data = grey_to_redblue_codec(np.array(vid_data, dtype=np.long)) skio.vwrite(join(VIDDIR, videoname), vid_data)
def extract(self): from skvideo.io import vwrite from os import walk for (session_dir, _j, scenes) in list(walk(self.sessions_path)): for scene in scenes: scene_dict = self.x.extract_signs_from( osp.join(session_dir, scene)) for key, value in scene_dict.items(): vwrite(osp.join(self.videos_path, key), value) a = osp.join(self.my_path, 'extracted') open(a, 'w').close()
def _to_supervisely(dataset, output): try: import supervisely_lib as sly from skvideo.io import vwrite except ModuleNotFoundError: raise ModuleNotInstalledException("supervisely") schema_dict = dataset.schema.dict_ for key, schem in schema_dict.items(): if isinstance(schem, Image): project_type = "images" extension = "jpeg" break elif isinstance(schem, Video): project_type = "videos" extension = "mp4" break else: raise Exception mode = sly.OpenMode.CREATE if project_type == "images": _project = sly.Project elif project_type == "videos": _project = sly.VideoProject else: raise Exception pr = _project(output, mode) meta = pr.meta meta._project_type = project_type # probably here we can create multiple datasets out_ds = pr.create_dataset(output) try: fn_key = "filename" dataset[fn_key] except KeyError: fn_key = None zeroes = len(str(len(dataset))) for idx, view in enumerate(dataset): obj = view[key].compute() if fn_key: fn = view[fn_key].compute() else: fn = f"{idx:0{zeroes}}" fn = "{}.{}".format(fn, extension) # strangely supervisely prevents from using this method on videos try: out_ds.add_item_np(fn, obj) except RuntimeError: # fix with in-memory file path = "{}/{}".format(out_ds.item_dir, fn) vwrite(path, obj) out_ds._item_to_ann[fn] = fn + ".json" out_ds.set_ann(fn, out_ds._get_empty_annotaion(path)) pr.set_meta(meta) return pr
def frames_tensor_to_video(frames_tensor, video_write_path): """ Writes a tensor of frames of size [n_frames x n_channels x height x width] to a video file at video_write_path. Args: frames_tensor (PyTorch Variable / tensor or numpy array): tensor of frames video_write_path (str): path to write the video """ assert type(video_write_path) == str, 'Video write path must be a string.' frames_tensor = convert_tensor(frames_tensor) vid.vwrite(video_write_path, frames_tensor)
def video(self): BIG_MAP_TIME = np.zeros((self.TIME_DURATION, self.X, self.Y)) for i in range(self.TIME_DURATION): A = tuple(map(tuple, self.DATA[0][i].T)) P = tuple(map(tuple, self.DATA[1][i].T)) BIG_MAP_TIME[i][A] = 100 BIG_MAP_TIME[i][P] = 200 # export to video self.VIDEO = BIG_MAP_TIME FILENAME = os.getcwd( ) + os.path.sep + 'OUT' + os.path.sep + 'MAP_LYFE.mp4' skio.vwrite(FILENAME, self.VIDEO)
def save_summary(src_video, label, out_video): logging.info(f'loading {src_video}') video = vread(src_video) summary = video[np.where(label)] logging.info(f'writing {out_video}') vwrite(out_video, summary, outputdict={ '-vcodec': 'libx264', '-crf': '20', '-pix_fmt': 'yuv420p' })
def save_images_to_gif(images, save_path): ''' Input: images: Batch_Size x 3 x H x W [Torch.Tensor] ''' os.makedirs(os.path.dirname(save_path), exist_ok=True) images = images.clamp(min=0, max=1.0) imgs = images.detach().cpu().numpy().transpose((0, 2, 3, 1)) * 255 imgs = imgs.astype('uint8') imageio.imwrite(save_path.replace('.gif', '.png'), imgs[0]) imageio.mimsave(save_path, imgs) vwrite(save_path.replace('.gif', '.mp4'), imgs, outputdict={'-r': '30'})
def arr2vid(arr, path, fps = '20', speed_mult = '2'): assert(len(arr.shape) == 4) # Compress output vid? makes writing to a video signifigantly faster compress = False speed = 'setpts=' + speed_mult + '*PTS' if compress: outdict = {'-r': fps, '-vcodec' : 'libx265', '-crf' : '30', '-filter:v' : speed} else: outdict = {'-r': fps, '-filter:v' : speed} skv.vwrite(path, arr, outputdict = outdict)
def images2avi(images, vidfile): # save video skv.vwrite(vidfile, images, inputdict={ '-vcodec': 'rawvideo', '-pix_fmt': 'rgb24' }, outputdict={ '-vcodec': 'rawvideo', '-pix_fmt': 'rgb24' })
def predict_video(num_frame, device, video_idx): # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') device = torch.device(device) model_parameters = torch.load('../model/new_video_vangogh' + str(video_idx) + '.pth') path = '../../2019_NCL_Brand_Essence_Good_to_be_Free.mp4' model = transferNet.TransferNet() model.load_state_dict(model_parameters) model.to(device) cap = cv2.VideoCapture(path) # fourcc = cv2.VideoWriter_fourcc(*args.fourcc) # out_writer = cv2.VideoWriter("../outputImage/outputvideo_new.avi", # fourcc, 24.0, (640, 480)) mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] frame_lst = [] count = 0 while (True): torch.cuda.empty_cache() ret, frame = cap.read() count = count + 1 print(count) if ret and count <= num_frame: current_frame = torch.Tensor(frame.transpose(2, 0, 1)) / 255. normalized = normalize(current_frame, mean, std) torch.cuda.empty_cache() new_frame = normalized.reshape((1, *normalized.shape)).to(device) out = model(new_frame) new_out = recover_image(out[1].data.cpu().numpy()) # out_writer.write(new_out) # new_out = new_out.reshape((1, *new_out.shape)) frame_lst.append(new_out) else: break cap.release() # out_writer.release() cv2.destroyAllWindows() out_video = np.concatenate(frame_lst) io.vwrite("../outputImage/new_video_vangogh" + str(video_idx) + ".mp4", out_video, outputdict={'-pix_fmt': 'yuv420p'})
def required_video(video_info): try: video_path = video_info[0] print(video_path) start_time = video_info[1] end_time = video_info[2] capture = cv2.VideoCapture(video_path) fps = capture.get(cv2.CAP_PROP_FPS) video = vread(video_path) video = video[math.floor(start_time * fps):math.floor(end_time * fps)] vwrite(videos_required_path + video_path.split('/')[-1], video) except: f = open(log_file, 'a') f.write(video_path + '\n') f.close()
def create_video(self): ''' composes video from .fformat files, requires ffmpeg ''' fileList = glob.glob(os.path.join(self.directory, '*' + self.format)) total_movie = [] fileList.sort(key=lambda x: os.path.basename(x)) print("Merging up the files. This might take a moment...") for filename in fileList: print(filename) img = ski.imread(filename) for i in range(self.framerate): total_movie.append(img) total_movie = np.array(total_movie) skv.vwrite(os.path.join(self.directory, 'movie.mkv'), total_movie) self.do_cleanup(fileList)
def play_clip(frames): # torch.Tensor -> Numpy np_frames = np.transpose(frames.numpy(), (0, 2, 3, 1)) * 255 np_frames = np_frames.astype(np.uint8) # write frames to temp file temp_file = tempfile.NamedTemporaryFile(suffix='.mp4') sk.vwrite(temp_file.name, np_frames) # play video clip cmd = 'ffplay -i ' + temp_file.name + ' -loop 0' os.system(cmd) # close & delete temp file temp_file.close() return
def dataset_to_mp4(s, m, mp4_path): """Converts the given series to an mp4 video. If the mask is given, adds an outline around each neuron. # Arguments s: imaging series as a (time x height x width) numpy array. m: neuron masks as a (no. neurons x height x width) numpy array. mp4_path: path where the mp4 file should be saved. # Returns Nothing """ from skvideo.io import vwrite logger = logging.getLogger(funcname()) logger.info('Preparing video %s.' % mp4_path) s = s.astype(np.float32) s = (s - np.min(s)) / (np.max(s) - np.min(s)) * 255 # If mask is given make a color video with neurons outlined. if m is not None: video = np.zeros(s.shape + (3, ), dtype=np.uint8) video[:, :, :, 0] = s video[:, :, :, 1] = s video[:, :, :, 2] = s outlines = np.zeros(s.shape[1:] + (3, ), ) for i in range(m.shape[0]): reg = one(list(zip(*np.where(m[i] == 1)))) outlines += reg.mask(dims=m.shape[1:], fill=None, stroke='white', background='black') yy, xx, _ = np.where(outlines != 0) video[:, yy, xx, :] = [102, 255, 255] else: video = s.astype(np.uint8) vwrite(mp4_path, video) logger.info('Saved video %s.' % mp4_path)
def write_images(): root = '../UCF-14/' classes = open('../dataset/classInd_14.txt', 'r') var1 = {} for line in classes: words = line.split(" ") var1[words[1].split("\n")[0]] = words[0] for path, subdirs, files in os.walk(root): for filename in files: print filename if ".DS_Store" not in filename: folder = 'images' + '/' + filename.split('.')[0] + '/' if not os.path.isdir(folder): os.mkdir(folder) else: shutil.rmtree(folder) os.mkdir(folder) try: cnt = 0 full_path = path + '/' + filename cap = vreader(full_path) fcnt = 1 for frame in cap: vid_name = filename.split('.')[0] img_path = folder + vid_name + '_{}.jpg'.format(cnt + 1) img_name = vid_name + '_{}'.format(cnt + 1) if fcnt % 5 == 0: vwrite(img_path, frame) cnt = cnt + 1 fcnt += 1 if cnt: with open("count.txt", "w") as txt: text = str(cnt) + " " + img_name.split( '.')[0] + "\n" txt.write(text) except (RuntimeError, TypeError, NameError): print "Some Error happened"
def save_images(self, webpage, visuals, image_path, aspect_ratio=1.0): image_dir = webpage.get_image_dir() short_path = ntpath.basename(image_path[0]) name = os.path.splitext(short_path)[0] webpage.add_header(name) ims = [] txts = [] links = [] if self.no_img: for label, im in visuals.items(): self.save_to_mat(webpage, name, label, im) else: for label, im in visuals.items(): self.save_to_mat(webpage, name, label, im) # save a matfile if self.video_mode: vid_name = '%s_%s.mp4' % (name, label) vid_path = os.path.join(image_dir, vid_name) # video_numpy = util.rescale_vid(video_numpy, 2) vwrite(vid_path, im, inputdict=self.ffmpeg_params) ims.append(vid_name) links.append(vid_path) else: image_name = '%s_%s.png' % (name, label) save_path = os.path.join(image_dir, image_name) h, w, _ = im.shape if aspect_ratio > 1.0: # im = imresize(im, (h, int(w * aspect_ratio)), interp='bicubic') im = np.array( Image.fromarray(im).resize( (h, int(w * aspect_ratio)), Image.BICUBIC)) if aspect_ratio < 1.0: # im = imresize(im, (int(h / aspect_ratio), w), interp='bicubic') im = np.array( Image.fromarray(im).resize( (int(h / aspect_ratio), w), Image.BICUBIC)) util.save_image(im, save_path) ims.append(image_name) links.append(image_name) txts.append(label) webpage.add_images(ims, txts, links, width=self.win_size)
def make_video_frames(datadir, outdir): # train video data for i in range(10000): if not os.path.exists(os.path.join(outdir, 'video%s' % i)): os.makedirs(os.path.join(outdir, 'video%s' % i)) filename = ('video%s.mp4' % i) videopath = os.path.join(datadir, filename) cap = sk.vreader(videopath) metadata = sk.ffprobe(videopath) # print json.dumps(metadata["video"], indent=4) """ fps : @r_frame_rate length : @duration frames : @nb_frames """ length = float( json.dumps(metadata["video"]["@duration"]).replace('"', '')) frames = float( json.dumps(metadata["video"]["@nb_frames"]).replace('"', '')) fps = int(frames / length) print('%sth video' % i) print('length : %d / frames : %d / fps : %d' % (length, frames, fps)) cent = np.linspace(0, length, 7)[1:-1] for x in range(len(cent)): cent[x] = int(cent[x]) frames = cent * fps idx = 0 filenum = 0 for frame in cap: if idx in frames: frame = cv2.resize(frame, dsize=(224, 224)) sk.vwrite(outdir + '/video%s/frame%s.png' % (i, filenum), frame) filenum += 1 idx += 1 if i % 1000 == 0: print('%sth video processed...' % i)
def main(): sess = tf.Session() in_image = tf.placeholder(tf.float32, [None, 16, None, None, 4]) gt_image = tf.placeholder(tf.float32, [None, 16, None, None, 3]) out_image = [] out_image += network(in_image), out_image += network(in_image, 3, prefix='1'), out_image += network(in_image, 2, prefix='2'), out_image += network(in_image, 3, 16, prefix='3'), from skvideo.io import vread, vwrite import numpy as np vid = np.load('./0_data/raw/test_data/gt_input/001_00_0001.npy') vid = np.expand_dims(np.float32(np.minimum((vid[:16, :256, :256, :] / vid.mean() / 5), 1.0)), axis=0) sess.run(tf.global_variables_initializer()) for i, out in enumerate(out_image): print out.shape output = sess.run(out, feed_dict={in_image: vid}) output = (np.minimum(output, 1.0) * 255).astype('uint8') print output[0].shape vwrite(str(i) + '.mp4', output[0])
def np_array_to_mp4(array, file_name): """ Parameters ---------- array : np.ndarray Video data. Dimensions = (t, y, x, color) file_name : str Name of mp4 file to write the data to Examples -------- Example 1:: # black and white import numpy as np array = np.random.random(size=(50, 480, 680, 1)) * 255 array = array.astype(np.uint8) file_name="outputvideo_BW.mp4" np_array_to_mp4(array,file_name=file_name) Example 2:: # color import numpy as np array = np.random.random(size=(50, 480, 680, 3)) * 255 array = array.astype(np.uint8) file_name="outputvideo_color.mp4" np_array_to_mp4(array,file_name=file_name) References ---------- * http://www.scikit-video.org/stable/io.html """ import skvideo.io as _skio print("work in progress") if not file_name.endswith('.mp4'): file_name += '.mp4' _skio.vwrite(file_name, array)
def main(): filename = 'videos/walking.MOV' video = io.vread(filename, as_grey=True) depths = [] for i in range(70, 340): filename = 'imgs/{}_cam-depth_array_.png'.format(i) d = cv2.imread(filename, -1) d = cv2.GaussianBlur(d, (9, 7), 0) depths.append(d) imgs = [] offset = 5 failure = 0 for i, frame in enumerate(video): print(i) frame = frame[:, :, 0] if i < offset: res = np.zeros(frame.shape) res2 = np.zeros(frame.shape) res3 = np.zeros(frame.shape) else: try: detector = Detector(video[i - offset], frame, depths[i - offset], depths[i]) res = detector.detect() kp1, kp2, matches = detector.match() res2 = detector.detect_2D(kp1, kp2, matches) res3 = detector.detect_3D(kp1, kp2, matches) except: failure += 1 res = np.zeros(frame.shape) res2 = np.zeros(frame.shape) res3 = np.zeros(frame.shape) top = np.hstack((frame, res)) bottom = np.hstack((res2, res3)) img = np.vstack((top, bottom)) imgs.append(img) print("failure: ", failure) io.vwrite('walking_detected.MOV', imgs)
def generate_video(base_dir, step_number, frames, reward, name='', results=[]): #, resize=True): print('converting to np array uint8') frames_for_video = np.array(frames).astype(np.uint8) if len(frames_for_video[0].shape) == 2: name += 'gray' else: name += 'color' mp4_fname = os.path.join( base_dir, "ATARI_step%010d_r%04d_%s.mp4" % (step_number, int(reward), name)) for i in range(0, len(frames_for_video), 500): end = min(i + 500, len(frames_for_video)) gname = mp4_fname.replace('.mp4', '%05d.mp4' % i) print("WRITING video", gname, len(frames_for_video)) vwrite(mp4_fname, frames_for_video[i:end])
def read_mesh_video(videoname, framesdir, shape=(480, 640), dtypergb=np.uint8, dtypedepth=np.uint16): rgb_data = read_frame_data(framesdir, framenofunction=rgb_std_naming_frameno, shape=shape + (3, ), dtype=dtypergb, framesregex="\d+\.rgb") * 0.5 depth_data = read_frame_data(framesdir, framenofunction=z16_std_naming_frameno, shape=shape + (1, ), dtype=dtypedepth, framesregex="\d+\.z16") * 0.5 depth_data = grey_to_redblue_codec(np.array(depth_data, dtype=np.long)) vid_data = depth_data + rgb_data skio.vwrite(join(VIDDIR, videoname), vid_data)
def convert_videos(self, square_size, save_folder, reduce_frames=True): x_paths = self.get_paths() failed = [] for row in x_paths.itertuples(): filepath = row.filepath obf_id = row.Index # load video = skv.vread(filepath, as_grey=False) self.height = video.shape[1] self.width = video.shape[2] # fill video if neccessary if video.shape[0] < self.num_frames: video = self._fill_video(video) # reduce if reduce_frames: frames = np.arange(0, video.shape[0], 2) try: video = video[frames, :, :] #.squeeze() except IndexError: if verbose: print("FAILED TO REDUCE:", filepath) print("id:\t", obf_id) failed.append(obf_id) #reshape video = np.asarray([imresize(x, (224, 224, 3), interp='bilinear') for x in video]) #save save_path = os.path.join(save_folder, obf_id) skv.vwrite(save_path, video) return failed
def plot_trajectory_on_video(vid_path, out_path, ball_trajectory, radius=10): """ Mark each detected ball with a red circle in a video :param vid_path: path to the input video :param out_path: path where to save the video :param ball_trajectory: array of ball detections (as outputted by detect_ball function) :param radius: radius of the circle marking the ball """ cap = cv2.VideoCapture(vid_path) arr = [] i = 0 count = 0 while True: ret, img = cap.read() if ret == False: break ori_img = img.copy() out_img = img.copy() if count < len(ball_trajectory) and ball_trajectory[count, 2] == i: scale = int(255 / (count + 1)) for j in range(count + 1): weight = count - j # print(j, weight*scale) center = tuple(ball_trajectory[j, :2].astype(int)) img = cv2.circle(ori_img, center, int(radius), [255, weight * scale, weight * scale], thickness=2) # print(j, weight, anti_weight) count += 1 #plt.figure(figsize = (10,10)) #plt.imshow(img[30:, 20:]) #plt.show() i += 1 # append multiple times to have slow motion like video for _ in range(5): arr.append(img[30:, 20:]) io.vwrite(out_path, arr)
def test_from_supervisely_video(): import supervisely_lib as sly from skvideo.io import vwrite data_path = "./data/test_supervisely/video_from" if os.path.exists(data_path): shutil.rmtree(data_path) project_name = "minuscule_videos/" project_path = os.path.join(data_path, project_name) project = sly.VideoProject(project_path, sly.OpenMode.CREATE) project.meta._project_type = "videos" item_name = "item.mp4" np.random.seed(0) for name in ["foofoo", "bar"]: ds = project.create_dataset(name) item_path = os.path.join(ds.item_dir, item_name) vwrite(item_path, (np.random.rand(len(name), 2, 2, 3) * 255).astype("uint8")) ds._item_to_ann[item_name] = item_name + ".json" ds.set_ann(item_name, ds._get_empty_annotaion(item_path)) project.set_meta(project.meta) trans = hub.Dataset.from_supervisely(os.path.join(data_path, project_name))