def get_train_test_lists(main_path):
    '''get train and test list by npy file'''
    test_patient_list = np.load(os.path.join(main_path,
                                             'test_patient_list.npy'),
                                allow_pickle=True)
    train_patient_list = np.load(os.path.join(main_path,
                                              'train_patient_list.npy'),
                                 allow_pickle=True)

    test_list = []
    for p in test_patient_list:
        l = ff.find_all_target_files([p + '*'],
                                     os.path.join(main_path, 'raw_movie'))
        for ll in l:
            test_list.append(ll)

    train_list = []
    for p in train_patient_list:
        l = ff.find_all_target_files([p + '*'],
                                     os.path.join(main_path, 'raw_movie'))
        for ll in l:
            train_list.append(ll)

    # Set the groups in a dictionary.
    file_groups = {'train': train_list, 'test': test_list}

    return file_groups
    def get_frames_for_sample(sample):

        path = os.path.join(cg.local_dir, 'images',
                            sample['video_name_no_ext'])

        files = ff.find_all_target_files(['*.jpg'], path)

        images = sorted(files)

        return images
#!/usr/bin/env python
'''this script assigned classes to each movie, which are normal EF>=X and abnormal EF<=Y in synthetic global EF functions'''
'''It will return an excel file saving all files and their classes'''

import glob
import os
import os.path
import numpy as np
import settings
import pandas as pd
import function_list as ff
cg = settings.Experiment()

movie_folder = os.path.join(cg.nas_main_dir, 'movie')
movie_list = ff.find_all_target_files(['*.avi'], movie_folder)
print(movie_list.shape)

# save all video name + their classes into an excel file
result = []
for m in movie_list:
    parts = m.split(os.path.sep)
    full_name = parts[len(parts) - 1]

    ef = float(full_name.split('_')[-1].split('%')[0])
    patient_id = full_name.split('_')[-2]
    if full_name.split('_')[1] == 'tavr':
        patient_class = full_name.split('_')[0] + '_' + full_name.split(
            '_')[1] + '_1'
    else:
        patient_class = full_name.split('_')[0] + '_' + full_name.split('_')[1]
import os.path
import settings
import function_list as ff
import numpy as np
cg = settings.Experiment()

main_folder = os.path.join(cg.oct_main_dir)
#main_folder = "/Experiment/Documents/Video_Data/UCF101/"

a_list = ff.find_all_target_files(['*'], os.path.join(main_folder,
                                                      'sequences'))

# for a in a_list:
#     r = np.load(a,allow_pickle = True)
#     shape = r.shape
#     if shape[0] != 20 or shape[1] != 2048:
#         print(a,shape)

for a in a_list:
    r = np.load(a, allow_pickle=True)
    shape = r.shape
    a1 = r[0]
    a2 = r[9]
    aa = np.concatenate((a1, a2)).reshape(2, a1.shape[0])
    print(aa.shape)

    break
Exemplo n.º 5
0
def extract_timeframes(main_path, movie_path, excel_file):
    """After we have all of our videos split between train and test, and
    all nested within folders representing their classes, we need to
    make a data file that we can reference when training our RNN(s).
    This will let us keep track of image sequences and other parts
    of the training process.

    We'll first need to extract images from each of the videos. We'll
    need to record the following data in the file:

    [train|test] or batch, class, filename, nb frames

    Extracting can be done with ffmpeg:
    `ffmpeg -i video.mpg image-%04d.jpg`
    """
    data = []

    # create image folder
    image_folder = os.path.join(main_path, 'images')
    ff.make_folder([image_folder])

    # find all the movies
    excel_file = pd.read_excel(excel_file)
    movie_list = excel_file['video_name']

    # extract time frames from each movie:
    for i in range(0, excel_file.shape[0]):
        case = excel_file.iloc[i]
        print(i, case['video_name'])

        # set the file name for images
        file_name = case['video_name']
        file_name_sep = file_name.split('.')  # remove .avi
        file_name_no_ext = file_name_sep[0]
        if len(file_name_sep) > 1:
            for ii in range(1, len(file_name_sep) - 1):
                file_name_no_ext += '.'
                file_name_no_ext += file_name_sep[ii]
        save_folder = os.path.join(image_folder, file_name_no_ext)
        ff.make_folder([save_folder])

        src = os.path.join(movie_path, file_name)
        if os.path.isfile(src) == 0:
            ValueError('no movie file')

        if os.path.isfile(
                os.path.join(save_folder,
                             file_name_no_ext + '-0001.jpg')) == 0:
            cap = cv2.VideoCapture(src)
            count = 1
            frameRate = 1
            while (cap.isOpened()):
                frameId = cap.get(1)  # current frame number
                ret, frame = cap.read()

                if (ret != True):
                    break
                if (frameId % math.floor(frameRate) == 0):
                    if count < 10:
                        number = '000' + str(count)
                    if count >= 10:
                        number = '00' + str(count)

                dest = os.path.join(save_folder,
                                    file_name_no_ext + '-' + number + '.jpg')
                cv2.imwrite(dest, frame)
                count += 1
            cap.release()
            # call(["ffmpeg", "-i", src, dest]) % this will cause some error (not extract exact 20 frames) in some avis

        # Now get how many frames it is.
        nb_frames = len(ff.find_all_target_files(['*.jpg'], save_folder))
        data.append([case['video_name'], file_name_no_ext, nb_frames])
Exemplo n.º 6
0
#!/usr/bin/env python
'''this script transfers files from NAS to octomore'''

import glob
import os
import os.path
import numpy as np
import shutil
import settings
import function_list as ff

cg = settings.Experiment()

nas_movie_folder = os.path.join(cg.nas_main_dir, 'movie')
movie_list = ff.find_all_target_files(['*avi'], nas_movie_folder)
print(movie_list.shape)

# # # make folder in octomore
local_folder = os.path.join(cg.oct_main_dir, 'raw_movie')
# # ff.make_folder([local_folder])

# copy to octomore
for m in movie_list:
    if os.path.exists(os.path.join(local_folder, os.path.basename(m))):
        print(" find %s in the destination. Skipping." % (os.path.basename(m)))
        continue
    else:
        print(" copy %s ." % (os.path.basename(m)))
        shutil.copyfile(m, os.path.join(local_folder, os.path.basename(m)))

# check whether the transfer is completed
Exemplo n.º 7
0
#!/usr/bin/env python

import glob
import os
import os.path
import numpy as np
import settings
import function_list as ff
import pandas as pd
from sklearn.model_selection import train_test_split
cg = settings.Experiment()

# siemens 304225 is not in the infarction movie set
patient_list = ff.find_all_target_files([
    'ucsd_bivent/*', 'ucsd_lvad/*', 'ucsd_ccta/*', 'ucsd_toshiba/*',
    'ucsd_tavr_1/*', 'ucsd_pv/*', 'ucsd_siemens/2*'
], os.path.join(cg.nas_patient_dir))
print(patient_list.shape)

np.random.shuffle(patient_list)
a = np.array_split(patient_list, 10)

# split train and test by 7:3
train = []
for i in range(0, 7):
    aa = a[i]
    for j in aa:
        parts = j.split(os.path.sep)
        patient_name = parts[-2] + '_' + parts[-1]
        train.append(patient_name)