def copy(all_metadata, wb, tone, source, destinations): source_header = read_header(source) ensure_minimal_software_version(source_header, source, 709) for d in destinations: dest_header = read_header(d) ensure_minimal_software_version(dest_header, d, 709) if wb or all_metadata: click.echo(f"Temp: {source_header['setup'].fWBTemp}") click.echo(f"CC: {source_header['setup'].fWBCc}") dest_header["setup"].fWBTemp = source_header["setup"].fWBTemp dest_header["setup"].fWBCc = source_header["setup"].fWBCc dest_header["setup"].cmCalib = source_header["setup"].cmCalib dest_header["setup"].WBGain = source_header["setup"].WBGain if tone or all_metadata: tone_label = source_header["setup"].ToneLabel.decode("ascii") tone_points = list(source_header["setup"].fTone )[:source_header["setup"].TonePoints * 2] click.echo( f"Tone points: {tone_label} {' '.join([str(p) for p in tone_points])}" ) dest_header["setup"].ToneLabel = source_header["setup"].ToneLabel dest_header["setup"].TonePoints = source_header["setup"].TonePoints dest_header["setup"].fTone = source_header["setup"].fTone click.secho(f"Writing metadata to {d}.", fg="green") write_header(d, dest_header)
def main(): '''Convert cine file to frames and save them in a .npz file.''' for dataset in DATASET: if dataset == 'Globular': video_name = '11-12-06_GMAW CV High V Globular - 175 ipm WFS, 33V, 85-15CO2, 20 ipm travel' elif dataset == 'Spray': video_name = '11-12-06_GMAW CV Spray - 400 ipm WFS, 35V, 85-15CO2, 20 ipm travel' video_path = os.path.join('Data', 'Video', 'CINE', video_name + '.cine') total_frames = read_header(video_path)["cinefileheader"].ImageCount save_folder = os.path.join('Data', 'Image', 'Input', dataset.lower()) n_frames = total_frames data_rgb = [] data_gray = [] for i in progress(range(n_frames)): # read each frame from .cine file as an ndarray rgb_image, _ = display_frames(video_path, start_frame=i + 1) rgb_image[np.isnan(rgb_image)] = 0 gray_image = rgb2gray(rgb_image) # Convert from float to uint8 rgb_image = normalizeuint8(rgb_image) gray_image = normalizeuint8(gray_image) data_rgb.append(rgb_image) data_gray.append(gray_image) data_rgb = np.array(data_rgb) data_gray = np.array(data_gray) np.savez_compressed(save_folder + '_rgb', images=data_rgb) np.savez_compressed(save_folder + '_gray', images=data_gray)
def show(clips): for cine_file in clips: try: source_header = read_header(cine_file) ensure_minimal_software_version(source_header, cine_file, 709) show_metadata(source_header, cine_file) except Exception as e: click.echo(f"Could not read {cine_file}:") click.echo(e) click.echo()
def read_frames(cine_file, start_frame=1, count=None): header = read_header(cine_file) if header["bitmapinfoheader"].biCompression: bpp = 12 else: bpp = header["setup"].RealBPP raw_images = frame_reader(cine_file, header, start_frame=start_frame, count=count) return raw_images, header["setup"], bpp
def image_generator(cine_file: Union[str, bytes, PathLike], start_frame: int = None, start_frame_cine: int = None, count: int = None) -> Generator[np.ndarray, Any, None]: """ Get only a generator of raw images for specified cine file. Parameters ---------- cine_file : str or file-like object A string containing a path to a cine file start_frame : int First image in a pile of images in cine file. If 0 is given, it means the first frame of the saved images would be read in this function. Only start_frame or start_frame_cine should be specified. If both are specified, raise ValueError. start_frame_cine : int First image in a pile of images in cine file. This number corresponds to the frame number in Phantom Camera Control (PCC) application. Only start_frame or start_frame_cine should be specified. If both are specified, raise ValueError. count : int maximum number of frames to get. Returns ------- raw_image_generator : generator A generator for raw image """ header = read_header(cine_file) if start_frame and start_frame_cine: raise ValueError( "Do not specify both of start_frame and start_frame_cine") elif not start_frame and not start_frame_cine: fetch_head = 1 elif start_frame: fetch_head = start_frame elif start_frame_cine: first_image_number = header["cinefileheader"].FirstImageNo last_image_number = first_image_number + header[ "cinefileheader"].ImageCount - 1 fetch_head = start_frame_cine - first_image_number if fetch_head < 0: raise ValueError( f"Cannot read frame {start_frame_cine:d}. This cine has only from {first_image_number:d} to {last_image_number:d}." ) raw_image_generator = frame_reader(cine_file, header, start_frame=fetch_head, count=count) return raw_image_generator
def image_generator(cine_file, start_frame=False, start_frame_cine=False, count=None): """ Get only a generator of raw images for specified cine file. Parameters ---------- cine_file : str or file-like object A string containing a path to a cine file start_frame : int First image in a pile of images in cine file. If 0 is given, it means the first frame of the saved images would be readed in this function. Only start_frame or start_frame_cine should be specified. If both are specified, raise ValueError. start_frame_cine : int First image in a pile of images in cine file. This number corresponds to the frame number in Phantom Camera Control (PCC) application. Only start_frame or start_frame_cine should be specified. If both are specified, raise ValueError. count : int maximum number of frames to get. Returns ------- raw_image_generator : generator A generator for raw image """ header = read_header(cine_file) if type(start_frame) == int and type(start_frame_cine) == int: raise ValueError( "Do not specify both of start_frame and start_frame_cine") elif start_frame == False and start_frame_cine == False: fetch_head = 1 elif type(start_frame) == int: fetch_head = start_frame elif type(start_frame_cine) == int: numfirst = header["cinefileheader"].FirstImageNo numlast = numfirst + header["cinefileheader"].ImageCount - 1 fetch_head = start_frame_cine - numfirst if fetch_head < 0: strerr = "Cannot read frame %d. This cine has only from %d to %d." raise ValueError(strerr % (start_frame_cine, numfirst, numlast)) raw_image_generator = frame_reader(cine_file, header, start_frame=fetch_head, count=count) return raw_image_generator
def __init__(self, cine_file, header=None, start_index=0, count=None, post_processing=None): """ Create an object for reading frames from a cine file. It can either be used to get specific frames with __getitem__ or as an iterator. Parameters ---------- cine_file : str or file-like object A string containing a path to a cine file header : dict (optional) A dictionary contains header information of the cine file start_index : int First image in a pile of images in cine file. Only used with the iterator in the object. count : int Maximum number of frames to get if this object is used as an iterator. post_processing : function Function that takes one image parameter and returns a new processed image If provided this function will be applied to the raw image before returning. Returns ------- the created object """ self.cine_file = cine_file self.cine_file_stream = open(cine_file, "rb") self.post_processing = post_processing if header is None: header = read_header(cine_file) self.header = header self.start_index = start_index self.i = self.start_index self.full_size = self.header["cinefileheader"].ImageCount if not count: count = self.full_size - self.start_index self.end_index = self.start_index + count if self.end_index > self.full_size: raise ValueError( "end_index {} is larger than the maximum {}".format( self.end_index, self.full_size)) self.size = count
def set_(destinations, temp, cc, record_fps, playback_fps, timecode_fps, tone, first_frame_number): for d in destinations: dest_header = read_header(d) ensure_minimal_software_version(dest_header, d, 709) if temp: click.secho( "WARNING: This does not yet change the calibration matrix.", fg="red") dest_header["setup"].fWBTemp = temp if cc: click.secho( "WARNING: This does not yet change the calibration matrix.", fg="red") dest_header["setup"].fWBCc = cc if tone: tone_label, tone_points, tone = parse_tone(tone) dest_header["setup"].ToneLabel = bytes(tone_label, "ascii") dest_header["setup"].TonePoints = tone_points dest_header["setup"].fTone = tone if record_fps: dest_header["setup"].FrameRate = record_fps if playback_fps: dest_header["setup"].fPbRate = _parse_fps(playback_fps) if timecode_fps: dest_header["setup"].fTcRate = _parse_fps(timecode_fps) if first_frame_number is not None: dest_header["cinefileheader"].FirstImageNo = first_frame_number if any([ temp, cc, record_fps, playback_fps, timecode_fps, tone, first_frame_number is not None ]): click.secho(f"Writing metadata to {d}.", fg="green") write_header(d, dest_header)
def read_frames( cine_file: Union[str, bytes, PathLike], start_frame: int = None, start_frame_cine: int = None, count: int = None ) -> Tuple[Generator[np.ndarray, Any, None], SETUP, int]: """ Get a generator of raw images for specified cine file. Parameters ---------- cine_file : str or file-like object A string containing a path to a cine file start_frame : int First image in a pile of images in cine file. If 0 is given, it means the first frame of the saved images would be read in this function. Only start_frame or start_frame_cine should be specified. If both are specified, raise ValueError. start_frame_cine : int First image in a pile of images in cine file. This number corresponds to the frame number in Phantom Camera Control (PCC) application. Only start_frame or start_frame_cine should be specified. If both are specified, raise ValueError. count : int maximum number of frames to get. Returns ------- raw_image_generator : generator A generator for raw image setup : pycine.cine.tagSETUP class A class contains setup data of the cine file bpp : int Bit depth of the raw images """ header = read_header(cine_file) bpp = read_bpp(header) setup = header["setup"] raw_image_generator = image_generator(cine_file, start_frame, start_frame_cine, count) return raw_image_generator, setup, bpp