def parse(self, file_path, **kwargs): """ Decodes a file path into a list of MarkerData. kwargs is not used. :param file_path: The file path to parse. :type file_path: str :param kwargs: There are no custom keyword arguments used. :return: List of MarkerData """ version = determine_format_version(file_path) if version == const.UV_TRACK_FORMAT_VERSION_1: mkr_data_list = parse_v1(file_path) elif version == const.UV_TRACK_FORMAT_VERSION_2: mkr_data_list = parse_v2(file_path) else: msg = 'Could not determine format version for UV Track file.' raise interface.ParserError(msg) return mkr_data_list
def parse_v1(file_path): """ Parse the UV file format or 3DEqualizer .txt format. :param file_path: :return: """ f = open(file_path, 'r') lines = f.readlines() f.close() if len(lines) == 0: raise OSError('No contents in the file: %s' % file_path) mkr_data_list = [] num_points = int(lines[0]) if num_points < 1: raise interface.ParserError('No points exist.') idx = 1 # Skip the first line for i in xrange(num_points): mkr_name = lines[idx] mkr_name = mkr_name.strip() # Create marker mkr_data = interface.MarkerData() mkr_data.set_name(mkr_name) idx += 1 num_frames = int(lines[idx]) if num_frames <= 0: msg = 'point has no data: %r' LOG.warning(msg, mkr_name) continue # Frame data parsing frames = [] j = num_frames while j > 0: idx += 1 line = lines[idx] if len(line) == 0: # Have we reached the end of the file? break j = j - 1 split = line.split() if len(split) != 4: # We should not get here msg = 'File invalid, there must be 4 numbers in line: %r' raise interface.ParserError(msg % line) frame = int(split[0]) mkr_u = float(split[1]) mkr_v = float(split[2]) mkr_weight = 1.0 mkr_weight = float(split[3]) mkr_data.weight.set_value(frame, mkr_weight) mkr_data.x.set_value(frame, mkr_u) mkr_data.y.set_value(frame, mkr_v) frames.append(frame) # Fill in occluded point frames all_frames = list(range(min(frames), max(frames) + 1)) for frame in all_frames: mkr_enable = int(frame in frames) mkr_data.enable.set_value(frame, mkr_enable) if mkr_enable is False: mkr_data.weight.set_value(frame, 0.0) mkr_data_list.append(mkr_data) idx += 1 return mkr_data_list
def parse(self, file_path, **kwargs): """ Parse the file path as a 3DEqualizer .txt file. :param file_path: File path to parse. :type file_path: str :param kwargs: expected to contain 'image_width' and 'image_height'. :return: List of MarkerData. """ # If the image width/height is not given we raise an error immediately. image_width = kwargs.get('image_width') image_height = kwargs.get('image_height') if isinstance(image_width, (int, float)): ValueError('image_width must be float or int.') if isinstance(image_height, (int, float)): ValueError('image_height must be float or int.') if image_width is None: image_width = 1.0 if image_height is None: image_height = 1.0 inv_image_width = 1.0 / image_width inv_image_height = 1.0 / image_height f = open(file_path, 'r') lines = f.readlines() f.close() if len(lines) == 0: raise OSError('No contents in the file: %s' % file_path) mkr_data_list = [] line = lines[0] line = line.strip() num_points = int(line) if num_points < 1: raise interface.ParserError('No points exist.') idx = 1 # Skip the first line for i in xrange(num_points): line = lines[idx] mkr_name = line.strip() # Create marker mkr_data = interface.MarkerData() mkr_data.set_name(mkr_name) # Get point color idx += 1 line = lines[idx] line = line.strip() mkr_color = int(line) mkr_data.set_color(mkr_color) idx += 1 line = lines[idx] line = line.strip() num_frames = int(line) if num_frames <= 0: idx += 1 msg = 'point has no data: %r' LOG.warning(msg, mkr_name) continue # Frame data parsing frames = [] j = num_frames while j > 0: idx += 1 line = lines[idx] line = line.strip() if len(line) == 0: # Have we reached the end of the file? break j = j - 1 split = line.split() if len(split) != 3: # We should not get here msg = 'File invalid, there must be 3 numbers in line: %r' raise interface.ParserError(msg % line) frame = int(split[0]) mkr_u = float(split[1]) * inv_image_width mkr_v = float(split[2]) * inv_image_height mkr_weight = 1.0 mkr_data.weight.set_value(frame, mkr_weight) mkr_data.x.set_value(frame, mkr_u) mkr_data.y.set_value(frame, mkr_v) frames.append(frame) # Fill in occluded point frames all_frames = list(range(min(frames), max(frames) + 1)) for frame in all_frames: mkr_enable = int(frame in frames) mkr_data.enable.set_value(frame, mkr_enable) if mkr_enable is False: mkr_data.weight.set_value(frame, 0.0) mkr_data_list.append(mkr_data) idx += 1 return mkr_data_list
def parse(self, file_path, **kwargs): if not isinstance(file_path, basestring): raise TypeError('file_path is not a string: %r' % file_path) if not os.path.isfile(file_path): raise OSError('File path does not exist: %s' % file_path) mkr_data_list = [] f = open(file_path, 'r') text = f.read() f.close() idx = text.find('imageSequence') if idx == -1: msg = "Could not get 'imageSequence' index from: %r" raise interface.ParserError(msg % text) start_idx = text.find('{', idx + 1) if start_idx == -1: msg = 'Could not get the starting index from: %r' raise interface.ParserError(msg % text) end_idx = text.find('}', start_idx + 1) if end_idx == -1: msg = 'Could not get the ending index from: %r' raise interface.ParserError(msg % text) imgseq = text[start_idx + 1:end_idx] imgseq = imgseq.strip() splt = imgseq.split() x_res = int(splt[0]) y_res = int(splt[1]) # Get path imgseq_path = re.search(r'.*f\(\s\"(.*)\"\s\).*', imgseq) if imgseq_path is None: msg = 'Could not get the image sequence path from: %r' raise interface.ParserError(msg % imgseq) imgseq_path = imgseq_path.groups() # Get frame range range_regex = re.search(r'.*b\(\s(\d*)\s(\d*)\s(\d*)\s\)', imgseq) if range_regex is None: msg = 'Could not get the frame range from: %r' raise interface.ParserError(msg % imgseq) range_grps = range_regex.groups() start_frame = int(range_grps[0]) end_frame = int(range_grps[1]) by_frame = int(range_grps[2]) frames = xrange(start_frame, end_frame, by_frame) idx = end_idx while True: idx = text.find('pointTrack', idx + 1) if idx == -1: break start_idx = text.find('{', idx + 1) if start_idx == -1: break end_idx = text.find('}', start_idx + 1) if end_idx == -1: break # Get point track name point_track_header = text[idx:start_idx] track_regex = re.search(r'pointTrack\s*\"(.*)\".*', point_track_header) if track_regex is None: continue track_grps = track_regex.groups() if len(track_grps) == 0: continue mkr_name = track_grps[0] # create marker mkr_data = interface.MarkerData() mkr_data.set_name(mkr_name) mkr_data.weight.set_value(start_frame, 1.0) for frame in frames: mkr_data.enable.set_value(frame, 0) point_track = text[start_idx + 1:end_idx] for line in point_track.splitlines(): splt = line.split() if len(splt) == 0: continue frame = int(splt[0]) # NOTE: In MatchMover, top-left is (0,0), but we want # bottom-left to be (0,0). x = float(splt[1]) / x_res y = ((float(splt[2]) / y_res) * -1) + 1.0 enable_value = int(frame in frames) mkr_data.enable.set_value(frame, enable_value) mkr_data.x.set_value(frame, x) mkr_data.y.set_value(frame, y) mkr_data_list.append(mkr_data) return mkr_data_list