def decode_lfc(self): # decode lfp file sections = self.read_buffer(self.file) # analyze JSON data self._json_dict = self.read_json(sections) # decompose JSON data h, w = [ safe_get(self._json_dict, 'image', 'width'), safe_get(self._json_dict, 'image', 'height') ] # filter LFP metadata settings self.cfg.lfpimg = self.filter_json(self._json_dict) # compose bayer image from lfp file sec_idx = self.get_idx(sections, int(h * w * self.cfg.lfpimg['bit'] / 8)) # perform color filter array management and obtain rgb image cfa_obj = CfaProcessor(img_buf=list(sections[sec_idx]), shape=(h, w), cfg=self.cfg, sta=self.sta) cfa_obj.main() self._rgb_img = cfa_obj.rgb_img del cfa_obj return True
def main(self): # auto calibration can only be used if calibration source path is either directory or tar archive if isdir(self._path) or self._path.endswith('.tar'): # read JSON file from selected *.lfp image self._lfp_json = self.cfg.load_json( self.cfg.params[self.cfg.lfp_path]) # extract calibration reference data frames = safe_get(self._lfp_json, 'frames') self._georef = safe_get(frames[0], 'frame', 'geometryCorrectionRef') if frames else '' # extract serial number to support search self._serial = safe_get(self._lfp_json, 'camera', 'serialNumber') # print status if not self._serial and isdir(self._path): self.sta.status_msg( 'No serial number found in JSON file. Provide calibration file instead of folder', self._opt_prnt) self.sta.interrupt = True # when path is directory if isdir(self._path): # look for geo data in calibration folders self._search_cal_dirs() # look for geo data in calibration tar-files (skip if already found in folders with file_found==True) self._search_cal_file() elif self._path.endswith('.tar'): # look for geo data in provided calibration tar-file self._search_tar_file(self._path) if self._file_found: # print status self.sta.status_msg('Found white image file ' + self._cal_fn, self._opt_prnt) else: # print status and interrupt process self.sta.status_msg( 'White image file not found. Revise calibration path settings', self._opt_prnt) self.sta.interrupt = True # load if file found and rotation option is set or calibration data is missing or re-calibration is required cond = self.cfg.params[self.cfg.opt_rota] or not exists( self.cfg.params[self.cfg.cal_meta]) or self.cfg.params[ self.cfg.opt_cali] if self._file_found and cond: # convert raw data to image array and get metadata self._raw2img() return True
def filter_json(json_dict): ''' filter LFP metadata settings ''' # variable init settings = {} channels = ['b', 'r', 'gb', 'gr'] # filter camera serial and model serial = safe_get(json_dict, "camera", "serialNumber") cam_model = serial if serial else safe_get(json_dict, "camera", "model") # set decode paramaters considering camera model if cam_model.startswith(('A', 'F')): # 1st generation Lytro # read bit packing settings['bit'] = safe_get(json_dict, "image", "rawDetails", "pixelPacking", "bitsPerPixel") if not settings['bit'] == 12: raise AssertionError('Unrecognized bit packing format') # get Bayer pattern, Automatic White Balance (AWB) gains and Color Correction Matrix (CCM) settings['bay'] = 'BGGR' settings['awb'] = [ safe_get(json_dict, 'image', 'color', 'whiteBalanceGain', key) for key in channels ] settings['ccm'] = safe_get(json_dict, 'image', 'color', 'ccmRgbToSrgbArray') settings['gam'] = safe_get(json_dict, 'image', 'color', 'gamma') elif cam_model.startswith(('B', 'I')): # 2nd generation Lytro # read bit packing settings['bit'] = safe_get(json_dict, "image", "pixelPacking", "bitsPerPixel") if not settings['bit'] == 10: raise AssertionError('Unrecognized bit packing format') # get Bayer pattern, Automatic White Balance (AWB) gains and Color Correction Matrix (CCM) settings['bay'] = 'GRBG' settings['awb'] = [ safe_get(json_dict, 'algorithms', 'awb', 'computed', 'gain', key) for key in channels ] settings['ccm'] = safe_get(json_dict, 'image', 'color', 'ccm') settings['gam'] = safe_get(json_dict, 'master', 'picture', 'frameArray', 0, 'frame', 'metadata', 'image', 'color', 'gamma') else: raise LfpTypeError('Camera type not recognized') return settings
def valid_cam_type(self): ''' check if Lytro file format is supported ''' # search for 2nd generation keys (filter camera serial and model ) serial = safe_get(self._json_dict, "camera", "serialNumber") cam_model = serial if serial else safe_get(self._json_dict, "camera", "model") # search for 1st generations keys (file names) #files = safe_get(self._json_dict, "files") if cam_model is None: # and files is None: self.sta.status_msg('File type not recognized') self.sta.error = True return False return True
def _raw2img(self): ''' decode raw data to obtain bayer image and settings data ''' # skip if calibrated json file already exists, otherwise perform centroid calibration if self._raw_data: # decode raw data obj = LfpDecoder(self._raw_data, self.cfg, self.sta) obj.decode_raw() self._wht_bay = obj.bay_img del obj # balance Bayer channels in white image try: wht_json = json.loads(self._wht_json.read()) frame_arr = safe_get(wht_json, 'master', 'picture', 'frameArray')[0] self.cfg.lfpimg['ccm_wht'] = safe_get(frame_arr, 'frame', 'metadata', 'image', 'color', 'ccmRgbToSrgbArray') awb = safe_get(frame_arr, 'frame', 'metadata', 'devices', 'sensor', 'normalizedResponses')[0] gains = [ 1. / awb['b'], 1. / awb['r'], 1. / awb['gr'], 1. / awb['gb'] ] self.cfg.lfpimg['awb_wht'] = gains except ValueError: gains = [ 1 / 0.74476742744445801, 1 / 0.76306647062301636, 1, 1 ] # apply white balance gains to calibration file cfa_obj = CfaProcessor(bay_img=self._wht_bay, cfg=self.cfg, sta=self.sta) cfa_obj.set_gains(gains) self._wht_bay = cfa_obj.apply_awb() del cfa_obj return True
def decode_lfc(self): # decode lfp file sections = self.read_buffer(self.file) # retrieve JSON data self._json_dict = self.read_json(sections) # JSON file export dp = os.path.splitext(self._lfp_path)[0] self.cfg.save_json(os.path.join(dp, os.path.basename(dp) + '.json'), json_dict=self.json_dict) # validate camera format support if not self.valid_cam_type: return False # decompose JSON data self._shape = [ safe_get(self._json_dict, 'image', 'width'), safe_get(self._json_dict, 'image', 'height') ] # filter LFP metadata settings self.cfg.lfpimg = self.filter_lfp_json(self._json_dict) # compose bayer image from lfp file sec_idx = self.get_idx( sections, int(self._shape[0] * self._shape[1] * self.cfg.lfpimg['bit'] / 8))[0] self._img_buf = list(sections[sec_idx]) self.comp_bayer() return True
def main(self): # check interrupt status if self.sta.interrupt: return False # auto calibration can only be used if calibration source path is either directory or tar archive if isdir(self._path) or self._path.lower().endswith('.tar'): # read JSON file from selected *.lfp image self._lfp_json = self.cfg.load_json( self.cfg.params[self.cfg.lfp_path]) # extract serial number to support search self._serial = safe_get(self._lfp_json, 'camera', 'serialNumber') self._cam_model = self._serial if self._serial else safe_get( self._lfp_json, 'camera', 'model') # extract calibration reference data if self._cam_model.startswith(('A', 'F')): frames = safe_get(self._lfp_json, 'picture', 'derivationArray') #'frameArray') # self._georef = frames[ 0] #safe_get(frames[0], 'frame', 'imageRef') if frames else '' # elif self._cam_model.startswith(('B', 'I')): frames = safe_get(self._lfp_json, 'frames') self._georef = safe_get( frames[0], 'frame', 'geometryCorrectionRef') if frames else '' # print status if not self._serial and isdir(self._path): self.sta.status_msg( 'No serial number found in JSON file. Provide calibration file instead of folder', self._opt_prnt) self.sta.error = True # when path is directory if isdir(self._path): # look for geo data in calibration folders self._search_cal_dirs() # look for geo data in calibration tar-files (skip if already found in folders with file_found==True) self._search_cal_file() elif self._path.lower().endswith('.tar'): # look for geo data in provided calibration tar-file self._search_tar_file(self._path) if not self._file_found: # print status and interrupt process self.sta.status_msg( 'White image file not found. Revise calibration path settings', self._opt_prnt) self.sta.error = True # load and keep white image if found and options are set or meta data is missing cond = self.cfg.params[self.cfg.opt_cali] or \ self.cfg.params[self.cfg.opt_vign] or \ not self.cfg.cond_meta_file() if self._file_found and cond: # convert raw data to image array and get metadata self._raw2img() return True