def fmf_ucmp(video_in, video_out, timestamps_in='None', fps=None, width=320, height=240): """Converts any file format to FlyMovieFormat. Parameters ---------- video_in : string Path to compressed video. video_out : string Path to save uncompressed, .fmf video. timestamps_in : string or None (default = None) Path to .gz file containing timestamps for video. fps : int or None (default = None) Frame rate to save uncompressed video. width : int (optional, default=640) Width of video, in pixels. height : int (optional, default=480) Height of video, in pixels. """ ts_file_given = os.path.isfile(timestamps_in) if ts_file_given: timestamps = np.loadtxt(timestamps_in) command = [ 'ffmpeg', '-i', video_in, '-f', 'image2pipe', '-pix_fmt', 'gray8', '-vf', 'scale=320:240', '-an', '-vcodec', 'rawvideo', '-' ] pipe = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=10**8) vid = FMF.FlyMovieSaver(video_out) try: count = 0 while True: raw_img = pipe.stdout.read(width * height) img = np.fromstring(raw_img, dtype=np.uint8) img = img.reshape((height, width)) if ts_file_given: vid.add_frame(img, timestamps[count]) else: vid.add_frame(img) count += 1 except: pipe.stdout.close() pipe.wait() del pipe vid.close()
def AddImageToFmf(self, image, imagetopic): if (imagetopic not in self.fmf_dict): self.fmf_dict[imagetopic] = FlyMovieFormat.FlyMovieSaver( self.fullpathFmf_dict[imagetopic], version=3, format=image.encoding.upper( ), # BUG: This isn't quite right, as the ROS encodings don't all match those in FlyMovieFormat.py bits_per_pixel=int(8 * image.step / image.width)) # Cast the pixels to the proper type, then add the frame to the .fmf pixels = np.array(image.data, 'c').view(np.uint8).reshape( (image.height, image.width)) self.fmf_dict[imagetopic].add_frame(pixels, image.header.stamp.to_sec())
) # BUG: This isn't quite right, as the ROS encodings don't all match those in FlyMovieFormat.py # Save the image file. cv.SaveImage(fullpathnameextImage, image) print('Wrote %s' % fullpathnameextImage) iImage += 1 else: print( 'Only image encoding==mono8 is supported. This one has %s' % msg.encoding) if (fullpathnameextFmf is not None): if (fmf is None): fmf = FlyMovieFormat.FlyMovieSaver(fullpathnameextFmf, version=3, format=format, bits_per_pixel=bpp) # Add the frame to the .fmf if (pixels is not None): fmf.add_frame(pixels, msg.header.stamp.to_sec()) # Write the filename,timestamp info the the .csv file. fidCsv.write('%s.%s, %0.9f\n' % (nameImage, extImage, msg.header.stamp.to_sec())) # If requested, convert the image files to an .mov file, then delete the images. if (fullpathnameextMov is not None) and (extImage is not None): # Run avconv. #cmdCreateVideoFile = 'avconv -y -r 60 -i %s/%%08d.%s -same_quant -r 60 %s && rm -rf %s && echo Finished.' % (fullpathImages, extImage, fullpathnameextMov, fullpathImages) cmdCreateVideoFile = 'avconv -y -r 60 -i %s/%%08d.%s -same_quant -r 60 %s && echo Finished.' % (
#many iterations, yuck. meh. while cuts: try: cut = cuts.pop(0) except IndexError: continue startt = endt = None for t in ts: if startt is None and t >= cut.start: startt = t if endt is None and startt is not None and t >= cut.end: endt = t break if endt is not None: print "cut", cut dest = fmf.FlyMovieSaver( cut.dest, format=fmffile.format, bits_per_pixel=fmffile.bits_per_pixel) for i in range(ts.index(startt),ts.index(endt)): frame, timestamp = fmffile.get_frame(i) dest.add_frame(frame,timestamp) dest.close() else: print "no cut found for",cut.dest
def clip_gen(clips_log, clip_ind=[]): # clips_log = r'Z:\April\JAABA\Experiments\CS\transition_clips\clips.mat' if 'WT' in clips_log: dirc_log = r'Z:\April\JAABA\Experiments\test\WT_dirc.mat' mat_contents = sio.loadmat(clips_log) dirc_contents = sio.loadmat(dirc_log) log = mat_contents['WT'] index = mat_contents['WT_index'] dirc = dirc_contents['WT_dirc'] elif 'Or47b' in clips_log: dirc_log = r'Z:\April\JAABA\Experiments\test\or47b_dirc.mat' mat_contents = sio.loadmat(clips_log) dirc_contents = sio.loadmat(dirc_log) log = mat_contents['Or47b'] index = mat_contents['Or47b_index'] dirc = dirc_contents['or47b_dirc'] if not clip_ind: clip_no = range(len(log)) elif clip_ind: clip_no = clip_ind video_no = len(dirc[0]) for j in clip_no: if 'WT' in clips_log: fname_2 = 'Z:\April\JAABA\Experiments\CS\\transition_clips\WT\\' + ''.join( log[j, 1]) + '.fmf' elif 'Or47b' in clips_log: fname_2 = 'Z:\April\JAABA\Experiments\CS\\transition_clips\or47b\\' + ''.join( log[j, 1]) + '.fmf' fmf_saver = FMF.FlyMovieSaver( fname_2) # save each video based on clip name b = log[j, 0] # get the log of this clip segment_no = index[j] timestamp_count = 0 for i in range(video_no): a = dirc[0, i][ 0] # a = dirc[row 0, video_no][get string of video name] if 'WT' in clips_log: fname = 'Z:\April\JAABA\Experiments\CS\WT\\' + a + '\\video.fmf' elif 'Or47b' in clips_log: fname = 'Z:\April\JAABA\Experiments\CS\or47b\\' + a + '\\video.fmf' fmf = FMF.FlyMovie(fname) c = b[i, :] segment_length = np.array(segment_no).tolist() segment_length = segment_length[0] for k in range( int(segment_length[i]) ): # for the num of segments in each video, -1 due to python index if int(segment_length[i]) == 0: e = 1 else: e = c[k] f = np.array(e).tolist() d = f[0] for frame_number in d: frame, timestamp = fmf.get_frame(int(frame_number) - 1) timestamp_count += 1 fmf_saver.add_frame(frame, timestamp_count) # add the spacer b/w segments width, height = 1024, 1024 fake_image = np.zeros((height, width), dtype=np.uint8) for p in range(10): timestamp_count += 1 fmf_saver.add_frame(fake_image, timestamp_count) # add the spacer b/w videos width, height = 1024, 1024 fake_image = 255 * np.ones((height, width), dtype=np.uint8) for p in range(10): timestamp_count += 1 fmf_saver.add_frame(fake_image, timestamp_count) fmf_saver.close()
import matplotlib.cm as cm import motmot.FlyMovieFormat.FlyMovieFormat as FMF import pandas as pd import numpy as np import benu.benu import benu.utils from PIL import Image, ImageStat #import roslib; roslib.load_manifest('flymad') #import flymad.madplot as madplot fname = sys.argv[1] fmf = FMF.FlyMovie(fname) newfmf = FMF.FlyMovieSaver(sys.argv[2] + 'newfmf.fmf') starting_timestamp = fmf.get_frame(0)[1] def CreateMovie(plotter, numberOfFrames, fps=10): for i in range(numberOfFrames): plotter(i) fname = '_tmp%05d.png'%i plt.savefig(fname) plt.clf()