def ult2vid_wedge_ustools(ult_data,
                          dir_file,
                          filename_no_ext,
                          NumVectors,
                          PixPerVector,
                          ZeroOffset,
                          Angle,
                          FramesPerSec,
                          pixels_per_mm=2):
    print(filename_no_ext + ' - ultrasound video started')

    output_file_no_ext = dir_file + filename_no_ext + '_ultrasound_ustools'

    # sample from https://github.com/UltraSuite/ultrasuite-tools/blob/master/ultrasound_process.ipynb
    ult_data_wedge = transform_ultrasound(ult_data, \
        spline_interpolation_order=2, background_colour=255, \
        num_scanlines=NumVectors, size_scanline=PixPerVector, \
        angle=Angle, zero_offset=ZeroOffset, pixels_per_mm=pixels_per_mm)

    print(
        filename_no_ext +
        ' - ultrasound video to wedge conversion (ustools) finished',
        ult_data_wedge.shape)

    (n_frames, n_width, n_height) = ult_data_wedge.shape

    # compressed
    # fourcc = VideoWriter_fourcc(*'MP4V')
    fourcc = VideoWriter_fourcc(*'mp4v')

    # uncompressed 8-bit
    # fourcc = VideoWriter_fourcc(*'Y800')

    # video = VideoWriter(output_file_no_ext + '.avi', fourcc, float(FramesPerSec), (n_width, n_height), 0)
    video = VideoWriter(output_file_no_ext + '.mp4', fourcc,
                        float(FramesPerSec), (n_width, n_height), 0)

    for n in range(n_frames):
        # print('starting frame ', n)
        # print(filename_no_ext, 'frame ', n, ' ', 'minmax', np.min(ult_data_wedge[n]), np.max(ult_data_wedge[n]), end='\n')
        frame = np.uint8(ult_data_wedge[n]).reshape(n_width, n_height)
        frame = np.rot90(frame).reshape(n_height, n_width, 1)

        video.write(frame)
        print('frame ', n, ' done', end='\r')
        # print('frame ', n, ' done')

    video.release()

    print(filename_no_ext + ' - ultrasound video finished')
Exemplo n.º 2
0
def main(config_filename, speaker, file_id):
    config = Config(config_filename)
    basepath = os.path.join(config.tal_corpus, speaker, file_id)
    identifier = '-'.join([speaker, file_id])

    # read input data
    txt = myio.read_text_from_file(basepath + '.txt')[0]
    wav, wav_sr = myio.read_waveform(basepath + '.wav')
    ult, params = myio.read_ultrasound_tuple(basepath,
                                             shape='3d',
                                             cast=None,
                                             truncate=None)
    vid, meta = myio.read_video(basepath, shape='3d', cast=None)

    ult_fps = params['FramesPerSec']
    vid_fps = meta['fps']

    # trim streams to parallel data
    ult, vid, wav = utils.trim_to_parallel_streams(ult, vid, wav, params, meta,
                                                   wav_sr)

    # transform ultrasound to real world coordinates
    ultrasound_background_colour = 255
    if config.dark_mode:
        ultrasound_background_colour = 0

    ult = transform_ultrasound(ult, \
        spline_interpolation_order=2, background_colour=ultrasound_background_colour, \
        num_scanlines=params['scanlines'], size_scanline=params['echos'], \
        angle=params['Angle'], zero_offset=params['ZeroOffset'], pixels_per_mm=1.0)

    # bit of hacky way to trim the ultrasound
    # it works well without this box, but using this box makes it a bit nicer
    ult = ult[:, 180:-180, 120:-100]

    # downsample ultrasound and video to target fps
    ult, vid = utils.downsample(ult, vid, ult_fps, vid_fps, config.target_fps)

    # plot data frame by frame
    frame_directory = os.path.join(config.output_directory,
                                   identifier + '_frames')
    plotters.plot_video_frames(ult, vid, wav, txt, wav_sr, config.target_fps, config, \
        frame_directory)

    # make final video file from frames
    vmaker.make(frame_directory, config, wav, wav_sr, identifier)
Exemplo n.º 3
0
    def transform_ult(self):
        """
        Transform the ultrasound.
        :return:
        """
        if self.params[
                'ult_frame_resized'] and not self.params['ult_transformed']:
            print("ultrasound has been down-sampled. No transform applied.")

        elif not self.params['ult_frame_resized'] and not self.params[
                'ult_transformed']:
            self.ult_t = transform_ultrasound(
                self.ult,
                num_scanlines=self.params['num_scanlines'],
                size_scanline=self.params['size_scanline'],
                angle=self.params['angle'],
                zero_offset=self.params['zero_offset'],
                pixels_per_mm=3)
            self.params['ult_transformed'] = True
Exemplo n.º 4
0
def animate_utterance(prompt_file,
                      wave_file,
                      ult_file,
                      param_file,
                      output_video_filename="out.avi",
                      frame_rate=24,
                      background_colour=255,
                      aspect='equal'):
    """

    :param prompt_file:
    :param wave_file:
    :param ult_file:
    :param param_file:
    :param output_video_filename:
    :param frame_rate: the video frame rate. This will be different to the ultrasound framerate
    :param background_colour: black = 0 and white = 255
    :param aspect
    :return:
    """

    # temp file names
    temp_audio_file = "cropped_audio.wav"
    temp_video_file = "video_only.avi"

    # prompt file is used for a video caption
    video_caption = parse_prompt_file(prompt_file)[0]

    # read parameter file
    param_df = parse_parameter_file(param_file=param_file)

    # use offset parameter to crop audio
    crop_audio(audio_start_time=param_df['TimeInSecsOfFirstFrame'].value,
               input_audio_file=wave_file,
               output_audio_file=temp_audio_file)

    # read ultrasound, reshape it, reduce the frame rate for efficiency, and transform it
    ult = read_ultrasound_file(ult_file=ult_file)

    ult_3d = ult.reshape(-1, int(param_df['NumVectors'].value),
                         int(param_df['PixPerVector'].value))

    x, fps = reduce_frame_rate(ult_3d=ult_3d,
                               input_frame_rate=float(
                                   param_df['FramesPerSec'].value),
                               output_frame_rate=frame_rate)

    print("transforming raw ultrasound to world...")
    y = transform_ultrasound(x,
                             background_colour=background_colour,
                             num_scanlines=int(param_df['NumVectors'].value),
                             size_scanline=int(param_df['PixPerVector'].value),
                             angle=float(param_df['Angle'].value),
                             zero_offset=int(param_df['ZeroOffset'].value),
                             pixels_per_mm=3)

    # create video without audio
    create_video(y, fps, temp_video_file, title=video_caption, aspect=aspect)

    # append audio and video
    append_audio_and_video(temp_audio_file, temp_video_file,
                           output_video_filename)

    # remove temporary files
    os.remove(temp_audio_file)
    os.remove(temp_video_file)

    print("Creation of video", output_video_filename, "complete.")