Example #1
0
def join_video_29(output, dirname, pattern, fps):
    """ 
        Note that the pattern is a Python regex:
        
        pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 
        
    """
    register_model_spec("""
--- model join_video_helper_29
config output
config dirname
config pattern
config factor

|files_from_dir dir=$dirname regexp=$pattern| -->file
file --> |imread_rgb| --> rgb
rgb --> |rewrite_timestamps interval=$factor| -> retimed
retimed --> |info|
# XXX this doesn't work
#retimed -> |fix_frame_rate fps=29.97| -->  fill
#fill --> |info|
retimed -> |mencoder quiet=1 file=$output timestamps=0|
    
    """)
    factor = 1.0 / fps
    pg('join_video_helper_29',
       dict(dirname=dirname, pattern=pattern, output=output, factor=factor))
Example #2
0
def join_video(output, dirname, pattern, fps):
    """ 
        Note that the pattern is a Python regex:
        
        pg-video-join -d anim-simple/ -p '.*.png' -o anim-simple-collate.mp4 --fps 1 
        
    """
    register_model_spec("""
--- model join_video_helper
config output
config dirname
config pattern
config fps

|files_from_dir dir=$dirname regexp=$pattern| -->file
#file --> |print|
file --> |imread_rgb| --> rgb
#file --> |imread| --> |torgb| --> rgb


rgb --> |mencoder quiet=1 file=$output timestamps=0 fps=$fps|
    
    """)

    pg('join_video_helper', dict(dirname=dirname, pattern=pattern, output=output,
                                 fps=fps))
Example #3
0
def join_video_29(output, dirname, pattern, fps):
    """ 
        Note that the pattern is a Python regex:
        
        pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 
        
    """
    register_model_spec("""
--- model join_video_helper_29
config output
config dirname
config pattern
config factor

|files_from_dir dir=$dirname regexp=$pattern| -->file
file --> |imread_rgb| --> rgb
rgb --> |rewrite_timestamps interval=$factor| -> retimed
retimed --> |info|
# XXX this doesn't work
#retimed -> |fix_frame_rate fps=29.97| -->  fill
#fill --> |info|
retimed -> |mencoder quiet=1 file=$output timestamps=0|
    
    """)
    factor = 1.0 / fps
    pg('join_video_helper_29', dict(dirname=dirname, pattern=pattern, output=output,
                                 factor=factor))
Example #4
0
def join_video(output, dirname, pattern, fps):
    """ 
        Note that the pattern is a Python regex:
        
        pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 
        
    """
    register_model_spec("""
--- model join_video_helper
config output
config dirname
config pattern
config fps

|files_from_dir dir=$dirname regexp=$pattern| -->file
#file --> |print|
file --> |imread_rgb| --> rgb
#file --> |imread| --> |torgb| --> rgb


rgb --> |mencoder quiet=1 file=$output timestamps=0 fps=$fps|
    
    """)

    pg('join_video_helper',
       dict(dirname=dirname, pattern=pattern, output=output, fps=fps))
Example #5
0
def video_crop(filename, output=None):
    if output is None:
        base, ext = os.path.splitext(filename)
        output = '%s-crop%s' % (base, ext)

    mask = get_motion_mask(filename)
    #print mask
    H, W = mask.shape
    useless_line = [np.min(mask[i, :]) for i in range(H)]
    useless_col = [np.min(mask[:, i]) for i in range(W)]

    useful_line = np.logical_not(useless_line)
    useful_col = np.logical_not(useless_col)
    #print useful_line
    #print useful_col

    crop_top = np.min(np.nonzero(useful_line)[0])
    crop_bottom = H - np.max(np.nonzero(useful_line)[0])
    crop_left = np.min(np.nonzero(useful_col)[0])
    crop_right = W - np.max(np.nonzero(useful_col)[0])

    print('Cropping left %s  right %s  top %s  bottom %s' %
          (crop_left, crop_right, crop_top, crop_bottom))

    register_model_spec("""
--- model crop_movie
config video 'video'
config output
config top = 0
config left = 0
config right = 0
config bottom = 0

|mplayer file=$video stats=1| --> rgb

rgb --> |crop left=$left top=$top bottom=$bottom right=$right| --> rgb2

rgb2 --> |mencoder quiet=1 file=$output timestamps=0|
    
    """)

    pg(
        'crop_movie',
        dict(top=crop_top,
             bottom=crop_bottom,
             left=crop_left,
             right=crop_right,
             video=filename,
             output=output))
Example #6
0
def video_crop(filename, output=None):
    if output is None:
        base, ext = os.path.splitext(filename)
        output = '%s-crop%s' % (base, ext)

    mask = get_motion_mask(filename)
    #print mask
    H, W = mask.shape
    useless_line = [np.min(mask[i, :]) for i in range(H)]
    useless_col = [np.min(mask[:, i]) for i in range(W)]

    useful_line = np.logical_not(useless_line)
    useful_col = np.logical_not(useless_col)
    #print useful_line
    #print useful_col

    crop_top = np.min(np.nonzero(useful_line)[0])
    crop_bottom = H - np.max(np.nonzero(useful_line)[0])
    crop_left = np.min(np.nonzero(useful_col)[0])
    crop_right = W - np.max(np.nonzero(useful_col)[0])

    print('Cropping left %s  right %s  top %s  bottom %s' %
          (crop_left, crop_right, crop_top, crop_bottom))

    register_model_spec("""
--- model crop_movie
config video 'video'
config output
config top = 0
config left = 0
config right = 0
config bottom = 0

|mplayer file=$video stats=1| --> rgb

rgb --> |crop left=$left top=$top bottom=$bottom right=$right| --> rgb2

rgb2 --> |mencoder quiet=1 file=$output timestamps=0|
    
    """)

    pg('crop_movie', dict(top=crop_top, bottom=crop_bottom,
                          left=crop_left, right=crop_right,
                          video=filename, output=output))
Example #7
0
def get_motion_mask(filename):
    register_model_spec("""
--- model movie_stats
config video 'video'
output rgb_min
output rgb_max

|mplayer file=$video stats=1| --> rgb

rgb --> |minimum_over_time| --> |output name=rgb_min|
rgb --> |maximum_over_time| --> |output name=rgb_max|
    
    """)
    model = pg('movie_stats', dict(video=filename))
    rgb_min = model.get_output('rgb_min')
    rgb_max = model.get_output('rgb_max')

    no_motion = np.min(rgb_min == rgb_max, axis=2)

    def percentage(x):
        return (100.0 * np.sum(x.flat) / x.size)

    print('Immobile: %.1f%% of the image.' % percentage(no_motion))

    bgcolor = rgb_min[0, 0, :]

    print('bgcolor: %s' % bgcolor)

    whiter = rgb_min[:, :, 0] == bgcolor[0]
    whiteg = rgb_min[:, :, 1] == bgcolor[1]
    whiteb = rgb_min[:, :, 2] == bgcolor[2]

    white = np.logical_and(np.logical_and(whiter, whiteg), whiteb)

    print('White: %.1f%% of the image.' % percentage(white))

    mask = np.logical_and(white, no_motion)

    print('Both: %.1f%% of the image.' % percentage(mask))

    return mask
Example #8
0
def join_video(output, dirname, pattern, fps):

    register_model_spec("""
--- model join_video_helper
config output
config dirname
config pattern
config fps

|files_from_dir dir=$dirname regexp=$pattern| -->file
#file --> |print|
file --> |imread_rgb| --> rgb
#file --> |imread| --> |torgb| --> rgb


rgb --> |mencoder quiet=1 file=$output timestamps=0 fps=$fps|
    
    """)

    pg('join_video_helper', dict(dirname=dirname, pattern=pattern, output=output,
                                 fps=fps))
Example #9
0
20100615/20100615_234934
20100615/20100615_235829
20100615/20100616_000059
20100616/20100616_175627
20100616/20100616_182001
""".split()

register_model_spec('''
--- model er1convert
config logdir 
config outdir 

|npyread file="${logdir}/angular_velocity.npy"| --> angular_velocity
|npyread file="${logdir}/linear_velocity.npy"| --> linear_velocity

|npyread file="${logdir}/odometry.npy"| --> odometry
|npyread file="${logdir}/video0.npy"| --> video0
|npyread file="${logdir}/video1.npy"| --> video1

odometry, angular_velocity, linear_velocity --> \
    |hdfwrite file="${outdir}/small_data.h5"|
video0 --> |mencoder file="${outdir}/video0.avi"|
video1 --> |mencoder file="${outdir}/video1.avi"|

''')


register_model_spec('''
--- model er1convert_simpler
config logdir 
config outdir 
Example #10
0
from procgraph import register_model_spec

# XXX: make models/ directory

register_model_spec("""
--- model variance 
''' Computes the element-wise variance. '''
input x 'Any numpy array'
output var_x 'Variance of ``x``.'
config wait = 100 "Number of samples to wait before declaring the value valid."

|input name=x| --> x --> |expectation| --> |wait n=$wait| --> Ex

   x, Ex --> |sync| --> |-| --> error 
   
   error -> |square| --> |expectation| --> |output name=var_x|
    
""")

register_model_spec("""
--- model soft_variance 
''' Computes the element-wise "soft" variance (exp. of error abs. value) '''
config wait = 100 "Number of samples to wait before declaring the value valid."
input x 'Any numpy array'
output var_x 'Soft variance of ``x``.'

|input name=x| --> x --> |expectation| --> |wait n=$wait| --> Ex

   x, Ex --> |sync| --> |-| --> error 
   
   error -> |abs| --> |expectation| --> |output name=var_x|
        frames = self.state.frames
        k = self.state.next_frame

        assert k < len(frames)
        
        timestamp, filename = frames[k] 
        
        self.set_output(0, value=filename, timestamp=timestamp)        

        if k + 1 >= len(frames):
            self.state.next_frame = None
        else:
            self.state.next_frame = k + 1
            

# TODO: make separate file for this
register_model_spec("""
--- model RawseedsCam
'''This model reads the images of a Rawseed camera log.'''
config dir    'Directory containing the images.' 
config fps_limit = 100 'Limit the frames per second (default is disabled).'
output images

import procgraph_pil

|RawseedsCamFiles dir=$dir| --> |fps_data_limit fps=$fps_limit| --> filenames

    filenames --> |imread| --> |output name=images|
 
""")
Example #12
0
import numpy as np

register_model_spec('''
--- model hdf2bag_conversion
config hdf 
config bag 
config id_robot
config id_actuators
config id_sensors
config id_episode
config id_environment
config commands_spec

import procgraph_hdf
import procgraph_ros

|log:hdfread file=$hdf|

#log.y --> |info|
#log.u --> |print|

log.y[y], log.u[u] -> |r:raw2boot| -> observations -> |bagwrite file=$bag| 

r.id_robot = $id_robot
r.id_actuators = $id_actuators
r.id_sensors = $id_sensors
r.id_episode = $id_episode
r.id_environment = $id_environment
r.commands_spec = $commands_spec
''')

Example #13
0
            else:
                yield 'y_goal', timestamp, goal
                

    iterator = read_data()
    pg('read_reconstructed_data',
       config=dict(iterator=iterator, out_base=out_base))



register_model_spec("""
--- model read_reconstructed_data
config out_base
config iterator 

|x:reconstructed_data iterator=$iterator| 

x.y, x.y_goal, x.servo_state -> |sync| -> |video_servo_multi_vis out_base=$out_base|

#x.commands, x.servo_state --> |info|
    
""")



class ReadData(IteratorGenerator):
    Block.alias('reconstructed_data')
    Block.output('y_goal')
    Block.output('y')
    Block.output('commands')
    Block.output('servo_state')
    Block.config('iterator')
Example #14
0
            raise BadInput('Expected arrays of 3 elements', self, 't')
       
        delta = t[2] - t[0]
        
        if not delta > 0:
            raise BadInput('Bad timestamp sequence % s' % t, self, 't')

        # if this is a sequence of bytes, let's promove them to floats
        if x[0].dtype == numpy.dtype('uint8'):
            diff = x[2].astype('float32') - x[0].astype('float32')
        else:
            diff = x[2] - x[0]
        time = t[1]
        x_dot = diff / numpy.float32(delta)
        self.set_output('x_dot', x_dot, timestamp=time)   

# TODO: move this to models/
register_model_spec("""
--- model derivative 
''' Computes the derivative of a quantity with 3 taps  (``x[t+1] - x[t-1]``).
    See also :ref:`block:derivative2`.                   '''
input x "quantity to derive"
output x_dot "approximate derivative"

|input name=x| --> |last_n_samples n=3| --> x,t

   x, t --> |forward_difference| --> |output name=x_dot|
    
""")

Example #15
0
        commands = [linear[0], linear[1], angular]
        self.set_output('commands', commands, timestamp=t[0])


# TODO: move this to models/ 
register_model_spec("""
--- model pose2commands
''' Computes the velocity commands from the odometry data. '''

input pose "Odometry as an array ``[x,y,theta]``."

output commands "Estimated commands as an array ``[vx,vy,omega]``."
output vx       "Linear velocity, forward (m/s)"
output vy       "Linear velocity, side (m/s)"
output omega    "Angular velocity (rad/s)"
 
|input name=pose| --> |last_n_samples n=2| --> |pose2vel_| --> commands 

    commands          -->          |output name=commands|
    commands --> |extract index=0| --> |output name=vx|
    commands --> |extract index=1| --> |output name=vy|
    commands --> |extract index=2| --> |output name=omega|
    
""")


register_model_spec("""
--- model commands_from_SE2
''' Computes the velocity commands from the odometry data. '''
Example #16
0
1   2   3   4   5   6   7   8   9 
 1*  2   3   4   5   6   7   8   9
  1   2   3   4   5   6   7   8   9
"""),
    ("Skip slave if other slave doesn't arrive.", """
* 1   2   3   4   9  
 1   *   2   3   4   9
   1   2   3   4   9
"""),
]

register_model_spec('''
--- model sync_test

|log_sim line=$master| -> master 
|log_sim line=$slave1| -> slave1
|log_sim line=$slave2| -> slave2

master, slave1, slave2 -> |sync2| -> |check_sync| --> |output name=value|

''')


class LogSim(Generator):
    Block.alias('log_sim')

    Block.output('stream')

    def init(self):
        line = self.config.line

        self.queue = []
Example #17
0
rgb --> |crop left=$left top=$top bottom=$bottom right=$right| --> rgb2

rgb2 --> |mencoder quiet=1 file=$output timestamps=0|
    
    """)

    pg('crop_movie', dict(top=crop_top, bottom=crop_bottom,
                          left=crop_left, right=crop_right,
                          video=filename, output=output))

register_model_spec("""
--- model movie_stats
config video 'video'
output rgb_min
output rgb_max

|mplayer file=$video stats=1| --> rgb

rgb --> |minimum_over_time| --> |output name=rgb_min|
rgb --> |maximum_over_time| --> |output name=rgb_max|
    
    """)
def get_motion_mask(filename):

    model = pg('movie_stats', dict(video=filename))
    rgb_min = model.get_output('rgb_min')
    rgb_max = model.get_output('rgb_max')

    no_motion = np.min(rgb_min == rgb_max, axis=2)

    def percentage(x):
        return (100.0 * np.sum(x.flat) / x.size)
Example #18
0
    pg(
        'crop_movie',
        dict(top=crop_top,
             bottom=crop_bottom,
             left=crop_left,
             right=crop_right,
             video=filename,
             output=output))


register_model_spec("""
--- model movie_stats
config video 'video'
output rgb_min
output rgb_max

|mplayer file=$video stats=1| --> rgb

rgb --> |minimum_over_time| --> |output name=rgb_min|
rgb --> |maximum_over_time| --> |output name=rgb_max|
    
    """)


def get_motion_mask(filename):

    model = pg('movie_stats', dict(video=filename))
    rgb_min = model.get_output('rgb_min')
    rgb_max = model.get_output('rgb_max')

    no_motion = np.min(rgb_min == rgb_max, axis=2)
Example #19
0
from procgraph import register_model_spec

# XXX: make models directory

register_model_spec("""
--- model covariance
''' Computes the covariance matrix of the input. '''
input x 'Unidimensional numpy array.'
output cov_x 'Square matrix representing sample covariance.'
config wait=10 'Number of sample to have reliable expectation.'

|input name=x| --> x --> |expectation| --> |wait n=$wait| --> Ex

   x, Ex --> |sync| --> |-| --> x_normalized 
   
   x_normalized, x_normalized --> |outer| -->  |expectation| --> covariance 
   
   covariance --> |output name=cov_x|
    
""")

register_model_spec("""
--- model normalize 
''' Removes the mean from a signal. '''
input  x       'Unidimensional numpy array.'
output x_n     'Signal without the mean.'
config wait=10 'Number of sample to have reliable expectation.'

|input name=x| --> x --> |expectation| --> |wait n=$wait| --> Ex

   x, Ex --> |sync| --> |-| --> x_normalized --> |output name=x_n|
Example #20
0
from procgraph  import register_model_spec

# XXX: make models directory

register_model_spec("""
--- model covariance
''' Computes the covariance matrix of the input. '''
input x 'Unidimensional numpy array.'
output cov_x 'Square matrix representing sample covariance.'
config wait=10 'Number of sample to have reliable expectation.'

|input name=x| --> x --> |expectation| --> |wait n=$wait| --> Ex

   x, Ex --> |sync| --> |-| --> x_normalized 
   
   x_normalized, x_normalized --> |outer| -->  |expectation| --> covariance 
   
   covariance --> |output name=cov_x|
    
""")

register_model_spec("""
--- model normalize 
''' Removes the mean from a signal. '''
input  x       'Unidimensional numpy array.'
output x_n     'Signal without the mean.'
config wait=10 'Number of sample to have reliable expectation.'

|input name=x| --> x --> |expectation| --> |wait n=$wait| --> Ex

   x, Ex --> |sync| --> |-| --> x_normalized --> |output name=x_n|
Example #21
0
        commands = [linear[0], linear[1], angular]
        self.set_output('commands', commands, timestamp=t[0])


# TODO: move this to models/
register_model_spec("""
--- model pose2commands
''' Computes the velocity commands from the odometry data. '''

input pose "Odometry as an array ``[x,y,theta]``."

output commands "Estimated commands as an array ``[vx,vy,omega]``."
output vx       "Linear velocity, forward (m/s)"
output vy       "Linear velocity, side (m/s)"
output omega    "Angular velocity (rad/s)"
 
|input name=pose| --> |last_n_samples n=2| --> |pose2vel_| --> commands 

    commands          -->          |output name=commands|
    commands --> |extract index=0| --> |output name=vx|
    commands --> |extract index=1| --> |output name=vy|
    commands --> |extract index=2| --> |output name=omega|
    
""")

register_model_spec("""
--- model commands_from_SE2
''' Computes the velocity commands from the odometry data. '''

input pose "Odometry as an element of SE2."
Example #22
0
 1*  2   3   4   5   6   7   8   9
  1   2   3   4   5   6   7   8   9
"""),
("Skip slave if other slave doesn't arrive.",
"""
* 1   2   3   4   9  
 1   *   2   3   4   9
   1   2   3   4   9
"""),
]

register_model_spec('''
--- model sync_test

|log_sim line=$master| -> master 
|log_sim line=$slave1| -> slave1
|log_sim line=$slave2| -> slave2

master, slave1, slave2 -> |sync2| -> |check_sync| --> |output name=value|

''')


class LogSim(Generator):
    Block.alias('log_sim')

    Block.output('stream')

    def init(self):
        line = self.config.line

        self.queue = []