示例#1
0
def learn(bin_lid):
    (h,w),LR_dir = stage(bin_lid)
    pids = []
    for LR in 'LR':
        pid = os.fork()
        if pid == 0: # subprocess
            # provision temp space
            tmp = mkdirs(scratch(bin_lid,'tmp_' + LR))
            # fetch eic
            eic = fetch_eic(bin_lid)
            # construct param file
            lightmap_dir = mkdirs(scratch(bin_lid,bin_lid + '_lightmap_' + LR))
            lightmap = os.path.join(lightmap_dir,bin_lid+'_lightmap_' + LR)
            if os.path.exists(lightmap):
                logging.info('lightmap exists: %s' % lightmap)
            else: # lightmap doesn't exist
                learn_tmp =  mkdirs(scratch(bin_lid,bin_lid + '_lightmap_tmp_' + LR))
                param = os.path.join(tmp,bin_lid + '_learn.txt')
                # produce param file
                logging.info('writing param file %s' % param)
                with open(param,'w') as fout:
                    print >> fout, 'imagedir %s' % LR_dir
                    print >> fout, 'metafile %s' % re.sub(r'/([^/]+)$',r'/ \1',eic)
                    print >> fout, 'tmpdir %s' % learn_tmp
                    print >> fout, 'save %s' % lightmap 
                    for k,v in DEFAULT_IC_CONFIG.items():
                        print >> fout, '%s %s' % (k,str(v))
                    print >> fout, 'binary_format'
                    print >> fout, 'num_to_process %d' % NUM_LEARN
                    print >> fout, 'num_rows %d' % h
                    print >> fout, 'num_cols %d' % (w/2)
                    print >> fout, 'top %d' % 0
                    if LR == 'L':
                        print >> fout, 'left %d' % 0
                    else:
                        print >> fout, 'left %d' % (w/2)
                    print >> fout, PATTERN
                    print >> fout, 'scallop_eic'
                    print >> fout, 'learn'
                # now learn
                learn = Process('"%s" "%s"' % (IC_EXEC, param))
                for line in learn.run():
                    logging.info(line['message'])
            os._exit(0)
        else:
            pids += [pid]
    # done
    for pid in pids:
        os.waitpid(pid,0)
        logging.info('joined learn process %s' % pid)
示例#2
0
文件: workers.py 项目: LouisK130/oii
class ProcessWorker(Job):
    """messages can be anything.
    queue_name - RabbitMQ queue basename
    amqp_host - RabbitMQ host"""
    def __init__(self,config):
        super(ProcessWorker,self).__init__(config.queue_name, config.amqp_host)
        self.config = config
        self.process = Process(self.get_script())
    def get_script(self):
        return ''
    def get_parameters(self,message):
        """Raise JobExit if the message is sufficient to determine an exit status
        (e.g., the job has already been completed by another worker). All validation
        and provisioning (e.g., creating output directories) must take place at this stage"""
        return {}
    def win_callback(self,params):
        return WIN
    def fail_callback(self,params):
        return FAIL
    def run_callback(self,message):
        try:
            params = self.get_parameters(message)
            try:
                for msg in self.process.run(params):
                    self.log(msg['message'])
                return self.win_callback(params)
            except RuntimeError:
                return self.fail_callback(params)
        except:
            raise
示例#3
0
文件: batch.py 项目: LouisK130/oii
def correct(bin_lid):
    # provision temp space
    tmp = mkdirs(scratch(bin_lid,'tmp'))
    # find TIFFs
    tenmin = get_tenmin(bin_lid)
    # split TIFFs into L and R
    ((h,w),LR_dirs) = split(bin_lid)
    for LR,LR_dir in zip('LR',LR_dirs):
        # fetch eic
        eic = fetch_eic(bin_lid,suffix='_cfa_%s' % LR)
        # construct param file
        lightmap_dir = mkdirs(scratch(bin_lid,bin_lid + '_lightmap_' + LR))
        lightmap = os.path.join(lightmap_dir,bin_lid+'_lightmap_' + LR)
        assert os.path.exists(lightmap)
        learn_tmp =  mkdirs(scratch(bin_lid,bin_lid + '_lightmap_tmp_' + LR))
        outdir = mkdirs(scratch(bin_lid,bin_lid + '_cfa_illum_' + LR))
        rgbdir = mkdirs(scratch(bin_lid,bin_lid + '_rgb_illum_' + LR))
        param = os.path.join(tmp,bin_lid + '_learn.txt')
        # produce param file
        logging.info('writing param file %s' % param)
        with open(param,'w') as fout:
            print >> fout, 'imagedir %s' % LR_dir
            print >> fout, 'outdir %s' % outdir
            print >> fout, 'metafile %s' % re.sub(r'/([^/]+)$',r'/ \1',eic)
            print >> fout, 'tmpdir %s' % learn_tmp
            print >> fout, 'load %s' % lightmap 
            print >> fout, 'num_to_process -1'
            for k,v in DEFAULT_IC_CONFIG.items():
                print >> fout, '%s %s' % (k,str(v))
            print >> fout, 'binary_format'
            print >> fout, 'num_rows %d' % h
            print >> fout, 'num_cols %d' % (w/2)
            print >> fout, PATTERN
            print >> fout, 'scallop_eic'
            print >> fout, 'correct'
        # now correct
        logging.info('correcting %s' % bin_lid)
        correct = Process('"%s" "%s"' % (IC_EXEC, param))
        for line in correct.run():
            logging.info(line['message'])
        # now demosaic
        imgs = os.listdir(outdir)
        pids = []
        for n in range(NUM_PROCS):
            pid = os.fork()
            if pid == 0:
                for f in imgs[n::NUM_PROCS]:
                    png = os.path.join(rgbdir,re.sub(r'_[a-zA-Z_.]+$','_rgb_illum_%s.png' % LR,f))
                    p = os.path.join(outdir,f)
                    cfa = img_as_float(imread(p,plugin='freeimage'))
                    rgb = demosaic(cfa,PATTERN)
                    imsave(png,rgb)
                    logging.info('debayered %s' % png)
                    (h,w,_) = rgb.shape
                    aspect = 1. * w / h
                    thumb = os.path.join(rgbdir,re.sub(r'_[a-zA-Z_.]+$','_rgb_illum_%s_thumb.jpg' % LR,f))
                    imsave(thumb,resize(rgb,(480,int(480*aspect))))
                    logging.info('saved thumbnail %s' % thumb)
                os._exit(0)
            else:
                pids += [pid]
        for pid in pids:
            os.waitpid(pid,0)
            logging.info('joined debayering process %d' % pid)
示例#4
0
def rectify_list(inlist):
    rect = Process('"%s" -v -s -l "%s" -c "%s"' % (RECT_EXEC, inlist, CALIBRATION_DIR))
    for line in rect.run():
        logging.info(line['message'])
示例#5
0
def merge_one(cfa_L, cfa_R, cfa_LR):
    merge = Process('"%s" -l "%s" -r "%s" -o "%s" -v' % (MERGE_EXEC, cfa_L, cfa_R, cfa_LR))
    for line in merge.run():
        logging.info(line['message'])
示例#6
0
def correct(bin_lid,learn_lid=None):
    (h,w),LR_dir = stage(bin_lid)
    if learn_lid is None:
        learn_lid = bin_lid
    # provision temp space
    tmp = mkdirs(scratch(bin_lid,'tmp'))
    for LR in 'LR':
        # check for lightmap existence
        lightmap_dir = mkdirs(scratch(learn_lid,learn_lid + '_lightmap_' + LR))
        lightmap = os.path.join(lightmap_dir,learn_lid+'_lightmap_' + LR)
        if not os.path.exists(lightmap):
            logging.info('requested lightmap does not exist, skipping correct phase')
            return
        # check for existing output
        logging.info('checking for existing corrected images...')
        outdir = mkdirs(scratch(bin_lid,bin_lid + '_cfa_illum_' + LR))
        skip = []
        for fn in os.listdir(outdir):
            imagename = re.sub('_cfa_illum_' + LR + '.tif','',fn)
            skip += [remove_extension(imagename)]
        mergedir = mkdirs(scratch(bin_lid,bin_lid + '_cfa_illum_LR'))
        for fn in os.listdir(mergedir):
            imagename = re.sub('_cfa_illum_LR.tif','',fn)
            skip += [remove_extension(imagename)]
        logging.info('found %d existing corrected images ...' % len(skip))
        outdir = mkdirs(scratch(bin_lid,bin_lid + '_cfa_illum_' + LR))
        # fetch eic
        eic = fetch_eic(bin_lid,suffix='_'+LR,skip=skip)
        # now see if that file is empty
        if os.stat(eic)[6] == 0:
            logging.info('no images to correct, skipping %s/%s' % (bin_lid, LR))
            continue
        # construct param file
        correct_tmp = mkdirs(scratch(bin_lid,bin_lid + '_correct_tmp_' + LR))
        param = os.path.join(tmp,bin_lid + '_correct.txt')
        # produce param file
        logging.info('writing param file %s' % param)
        with open(param,'w') as fout:
            print >> fout, 'imagedir %s' % LR_dir
            print >> fout, 'outdir %s' % outdir
            print >> fout, 'metafile %s' % re.sub(r'/([^/]+)$',r'/ \1',eic)
            print >> fout, 'tmpdir %s' % correct_tmp
            print >> fout, 'load %s' % lightmap 
            print >> fout, 'num_to_process %d' % NUM_CORRECT
            for k,v in DEFAULT_IC_CONFIG.items():
                print >> fout, '%s %s' % (k,str(v))
            print >> fout, 'binary_format'
            print >> fout, 'num_rows %d' % h
            print >> fout, 'num_cols %d' % (w/2)
            print >> fout, 'top %d' % 0
            if LR == 'L':
                print >> fout, 'left %d' % 0
            else:
                print >> fout, 'left %d' % (w/2)
            print >> fout, PATTERN
            print >> fout, 'scallop_eic'
            print >> fout, 'correct'
        # now correct
        logging.info('correcting %s' % bin_lid)
        correct = Process('"%s" "%s"' % (IC_EXEC, param))
        for line in correct.run():
            logging.info(line['message'])
示例#7
0
文件: workers.py 项目: LouisK130/oii
 def __init__(self,config):
     super(ProcessWorker,self).__init__(config.queue_name, config.amqp_host)
     self.config = config
     self.process = Process(self.get_script())
示例#8
0
import sys
from oii.utils import Struct
import os
import re

if __name__=='__main__':
    exec_path = '/usr/local/bin/ImageStack'
    script = ' '.join([exec_path,
            '-load %(img_in)s',
            '-demosaic 1 0 1',
            '-crop 0 0 1360 1024',
            '-dup -normalize -scale 2.5 -clamp -save %(img_out)s_before.png -pop',
            '-dup',
            '-rectfilter %(filter_size)s',
            '-pull 1 -subtract',
            '-normalize -scale 2.5 -clamp',
            '-save %(img_out)s'
            ])
    ims = Process(script)
    in_dir='/Users/jfutrelle/Pictures/habcamv4/leg1_stereo'
    out_dir='/Users/jfutrelle/Pictures/habcamv4/leg1_stereo_out_v3'
    for f in os.listdir(in_dir):
        if re.match('.*\.tif$',f):
            img_in = os.path.join(in_dir,f)
            img_out = os.path.join(out_dir,f)
            for msg in ims.run(dict(img_in=img_in,filter_size=501,img_out=img_out)):
                print msg['message']