Beispiel #1
0
def crop_oasis_images():
    # Get the list of files from the source data
    files = list_files(SOURCE, EXTENSION)

    i = 0

    # Iterate over each file path
    for f in files:

        # Load and crop the image at path f
        try:
            print i, f

            cropped_image = load_and_crop_image(f)

            # Assign a path for saving. We need to move it from source to dest,
            # so do some path replacement
            save_path = f.replace(SOURCE,
                                  DEST).replace(EXTENSION, SAVE_EXTENSION)

            i += 1

            # Save the new cropped image
            plt.imsave(save_path,
                       cropped_image,
                       cmap=plt.cm.grey,
                       format=SAVE_FORMAT)
        except Exception:
            print 'Problem cropping', f
Beispiel #2
0
def process(source_path, target_path, move):
    files = common.list_files(source_path)
    logger.info(f"{len(files)} pictures found")

    count = 1
    for file in files:
        picture = Picture(file)
        logger.info(f"{count}/{len(files)} - {picture.file_name}")
        process_picture(picture, target_path, move)
        count += 1
Beispiel #3
0
def demo_plot_cropped_image():
    # Get the list of files from the source data
    files = list_files(SOURCE, EXTENSION, result_cap=1)

    # Crop the first image
    cropped_image = load_and_crop_image(files[0])

    # Plot the image using matplotlib
    plt.imshow(cropped_image)
    plt.show()
Beispiel #4
0
def process(source_path, comparison_images_directory):
    files = common.list_files(source_path)
    logger.info(f"{len(files)} pictures found")

    known_faces = faces.load_known_faces(comparison_images_directory)

    count = 1
    for file in files:
        picture = Picture(file)
        logger.info(f"{count}/{len(files)} - {picture.file_name}")
        faces.recognize_faces(picture, known_faces)
        count += 1
Beispiel #5
0
def load_audio(path, seg, nw, n_mfcc):
    # seg = 4 # 2s分段
    # nw = 1024 # 帧长约23ms*4
    # n_mfcc = 32  # mfcc 维数

    folder_list = common.list_files(path)
    data_dict = {}
    for folder in folder_list:
        species = folder.split('\\')[-1]
        wav_list = common.find_ext_files(folder, '.wav')
        instance = dataObejct()
        for wav in wav_list:
            wavname = wav.split('\\')[-1]
            wav_data, fs = common.read_wav_file(wav)
            # 多通道仅取一通道
            if wav_data.ndim > 1:
                wav_data = wav_data[:, 0]
            wav_data = wav_data.T
            ref_value = 2**12 - 1
            wav_data = wav_data / ref_value  # wave幅值归一化

            filter_data = common.butter_worth_filter(wav_data,
                                                     cutoff=1000,
                                                     fs=fs,
                                                     btype='high',
                                                     N=8)

            seg_mfcc, frame_num = enframe_and_feature_extract(
                filter_data, seg, nw, fs, n_mfcc)

            # # 输出为[n_mfcc, n_sample]
            # mfcc_data = librosa.feature.mfcc(y=filter_data, sr=fs, n_mfcc=n_mfcc, n_fft=nw, hop_length=inc)

            instance.append(audio_name=wavname,
                            origin=filter_data,
                            frame=seg_mfcc,
                            frame_num=frame_num)
        data_dict[species] = instance
    return data_dict
Beispiel #6
0
from common import copy_files
from common import list_files_in_subdirectories_by_extension as list_files

# This Script will find all .txt files in folders of the data and store them in the external_raw folder

# Insure that the Source only points to the folder that has Disk files with the PAT records in them
SOURCE = '/Users/gunnarenserro/Desktop/Oasis_data/'
EXSTENSION = '.txt'
DEST = '../../data/external_raw/Pat_data'

files = list_files(SOURCE, EXSTENSION)
if len(files) % 2 == 1:
    raise ValueError('Does not have correct amount of files!')

copy_files(files[0::2], DEST)
Beispiel #7
0
def load_and_train(path, seg, nw, n_mfcc, save_path):
    """
    :param path: 音频存储路径
    :param nw: 帧长
    :param n_mfcc: mfcc特征维数
    :return: 
    hmm_models, hmm模型对象
    test_list,list[instance('frame':分帧数据 ,'origin':原始音频数据(滤波), 'frame_num':帧数), ]
    """
    hmm_models = hmms(n_iter=1000)

    list_folders = common.list_files(path)

    test_list = []

    for i in range(len(list_folders)):
        name = list_folders[i].split('\\')[-1]
        config_name = os.path.join(path, name + '.json')
        with open(config_name, encoding='UTF-8') as file:
            config = json.load(file)
        n_components = config['n_components']
        n_mixs = config['n_mixs']
        audio_num_for_train = config['audio num for train']
        # audio_num_for_train=10

        list_wavs = common.find_ext_files(list_folders[i], ext='.wav')
        print('%d = %s num:%d' % (i, list_folders[i], len(list_wavs)))

        list_wavs = shuffle_list(list_wavs)
        instance = dataObejct()
        instance.set_name(name)
        debug = 0
        for wavname in list_wavs:
            if debug >= audio_num_for_train:
                break
            debug += 1
            wav_data, fs = common.read_wav_file(wavname)
            # 多通道仅取一通道
            if wav_data.ndim > 1:
                wav_data = wav_data[:, 0]
            wav_data = wav_data.T
            ref_value = 2**12 - 1
            wav_data = wav_data / ref_value  # wave幅值归一化

            filter_data = common.butter_worth_filter(wav_data,
                                                     cutoff=1000,
                                                     fs=fs,
                                                     btype='high',
                                                     N=8)

            seg_mfcc, frame_num = enframe_and_feature_extract(
                filter_data, seg, nw, fs, n_mfcc)

            # # 输出为[n_mfcc, n_sample]
            # mfcc_data = librosa.feature.mfcc(y=filter_data, sr=fs, n_mfcc=n_mfcc, n_fft=nw, hop_length=inc)

            instance.append(audio_name=wavname,
                            origin=filter_data,
                            frame=seg_mfcc,
                            frame_num=frame_num)

        split_ = int(instance.get_num() / 2)
        shuffled_instance = instance.shuffle()
        train_samples = shuffled_instance[split_:]
        # 确定训练集最大帧数,留作样本平衡使用。
        train_samples.recompute_total()
        train_samples.set_name(name)
        frames = np.empty((0, n_mfcc))
        frames_num = []
        npy_name = name + '_' + str(n_mixs) + '_' + str(n_components) + '.npy'
        save_name = os.path.join(save_path, npy_name)
        if not os.path.exists(save_name):
            for j in range(len(train_samples.origin)):
                frame_data = train_samples.frame[j]
                frame_data = frame_data.reshape((-1, n_mfcc))
                frames = np.vstack((frames, frame_data))
                frames_num += train_samples.frame_num[j]
            if sum(frames_num) != frames.shape[0]:
                print('sum(frames_num) = ', sum(frames_num))
                print('total frames = ', frames.shape[0])
                raise ValueError('sum(frames_num) != frames.shape[0]')
            hmm_models.train_one(frames, frames_num, n_components, n_mixs,
                                 name)
        else:
            print('\t模型%s已存在,加载模型' % npy_name)
            hmm_models.load_one_model(save_name)
        test_samples = shuffled_instance[:split_]
        test_samples.set_name(name)
        test_list.append(test_samples)
    hmm_models.save_model(save_path)
    return hmm_models, test_list
Beispiel #8
0
def load_optical_data(path, seg, nw, n_mfcc):
    """
    :param path: 音频存储路径
    :param nw: 帧长
    :param n_mfcc: mfcc特征维数
    :return: 
    train_list,list[instance('frame':分帧数据 ,'origin':原始音频数据(滤波), 'frame_num':帧数), ]
    test_list,list[instance('frame':分帧数据 ,'origin':原始音频数据(滤波), 'frame_num':帧数), ]
    """

    list_folders = common.list_files(path)
    train_list = []
    test_list = []

    for i in range(len(list_folders)):
        name = list_folders[i].split('\\')[-1]
        list_wavs = common.find_ext_files(list_folders[i], ext='.wav')
        print('%d = %s num:%d' % (i, list_folders[i], len(list_wavs)))

        list_wavs = shuffle_list(list_wavs)
        instance = dataObejct()
        instance.set_name(name)
        debug = 0
        for wavname in list_wavs:
            if debug >= 200:
                break
            debug += 1
            wav_data, fs = common.read_wav_file(wavname)
            # 多通道仅取一通道
            if wav_data.ndim > 1:
                wav_data = wav_data[:, 0]
            wav_data = wav_data.T
            ref_value = 2**12 - 1
            wav_data = wav_data / ref_value  # wave幅值归一化

            filter_data = common.butter_worth_filter(wav_data,
                                                     cutoff=1000,
                                                     fs=fs,
                                                     btype='high',
                                                     N=8)

            seg_mfcc, frame_num = enframe_and_feature_extract(
                filter_data, seg, nw, fs, n_mfcc)

            # # 输出为[n_mfcc, n_sample]
            # mfcc_data = librosa.feature.mfcc(y=filter_data, sr=fs, n_mfcc=n_mfcc, n_fft=nw, hop_length=inc)

            instance.append(audio_name=wavname,
                            origin=filter_data,
                            frame=seg_mfcc,
                            frame_num=frame_num)

        split_ = int(instance.get_num() / 2)
        shuffled_instance = instance.shuffle()
        train_samples = shuffled_instance[split_:]
        # 确定训练集最大帧数,留作样本平衡使用。
        train_samples.recompute_total()
        train_samples.set_name(name)

        test_samples = shuffled_instance[:split_]
        test_samples.set_name(name)

        train_list.append(train_samples)
        test_list.append(test_samples)

    return train_list, test_list
Beispiel #9
0
SOURCES = [
    '../../data/external_raw/gifs_cropped_cor',
    '../../data/external_raw/gifs_cropped_sag',
    '../../data/external_raw/gifs_cropped_tra'
]
DESTS = [
    '../../data/external_raw/gifs_cropped_cor_scaled',
    '../../data/external_raw/gifs_cropped_sag_scaled',
    '../../data/external_raw/gifs_cropped_tra_scaled'
]
EXTENSION = ''
SAVE_FORMAT = 'PNG'

for SOURCE, DEST in zip(SOURCES, DESTS):
    files = list_files(SOURCE, EXTENSION, result_cap=1000)

    heights = []
    widths = []

    for f in files:
        img = io.imread(f)
        heights.append(len(img))
        widths.append(len(img[0]))

        widths = sorted(widths)
        heights = sorted(heights)

    print 'Width: mean: ', mean(widths), 'std', std(widths)
    print 'Height: mean: ', mean(heights), 'std', std(heights)
Beispiel #10
0
def process(path_to_clean, reference_path, delete_duplicates):
    files_to_clean = common.list_files(path_to_clean)
    nb_files_to_clean = len(files_to_clean)
    logger.info(f"{nb_files_to_clean} files to check for duplicates")
    if delete_duplicates is True:
        logger.info("/!\ Duplicates found will be deleted")

    if reference_path is not None:
        reference_files = common.list_files(reference_path)
    else:
        reference_files = []

    nb_reference_files = len(reference_files)
    logger.info(f"{nb_reference_files} reference files to compare")
    total_nb_files = nb_files_to_clean + nb_reference_files

    duplicates = {}
    p_reference_files = {}
    with Bar('Processing', max=total_nb_files) as bar:
        if reference_path is not None:
            for i in range(nb_reference_files):
                f = Picture(reference_files[i])
                p_reference_files[f.md5sum] = f
                bar.next()

        for i in range(nb_files_to_clean):
            p_file_to_clean = Picture(files_to_clean[i])
            is_duplicate(p_reference_files, duplicates, p_file_to_clean)
            bar.next()

    nb_duplicates = len(duplicates)
    if nb_duplicates == 0:
        logger.info("No duplicates found")
    elif nb_duplicates == 1:
        logger.info(f"One picture with duplicates found")
    else:
        logger.info(f"{nb_duplicates} pictures with duplicates found")

    if nb_duplicates > 0:
        with open('report.html', 'a') as the_file:
            the_file.write('''
                <html>
                    <head>
                    <style>
                    table, th, td {
                      border: 1px solid black;
                      border-collapse: collapse;
                    }
                    </style>
                    </head>
                    <body>
                        <table border="1"><tr><th>Original</th><th>Duplicates</th></tr>\n'''
                           )
            for d in duplicates.values():
                #logger.info(f"Original file: {d.original.full_path}")
                the_file.write('<tr>\n')
                the_file.write(
                    f'<!--Original--><td><img title="{d.original.full_path}" alt="{d.original.full_path}" src="file://{d.original.full_path}" width="25%" height="25%"></td>'
                )
                the_file.write('\n<!--Duplicates--><td>')
                for v in d.duplicates:
                    #logger.info(f"  - {v.full_path}")
                    the_file.write(
                        f'<img alt="{v.full_path}" src="file://{v.full_path}" width="25%" height="25%">\n'
                    )
                    if delete_duplicates is True:
                        logger.info(
                            f"Deleting {v.full_path} (original: {d.original.full_path})"
                        )
                        os.remove(v.full_path)
            the_file.write('</td>\n</tr>\n')
            the_file.write('</table></body></html>')
Beispiel #11
0
    parser.add_argument("--input", help="Input file or folder.")
    parser.add_argument("--output", help="Output folder.")
    parser.add_argument("--fps", type=int, help="")
    parser.add_argument("--to-video", help="")
    parser.add_argument("--tool", default="vlc", help="")

    args = parser.parse_args()

    shutil.rmtree(args.output, ignore_errors=True)
    Path(args.output).mkdir(parents=True, exist_ok=True)

    sample_fps = args.fps
    if args.to_video:
        shutil.rmtree(args.to_video, ignore_errors=True)
        Path(args.to_video).mkdir(parents=True, exist_ok=True)
    for full_path, sub_path, file_name, file_ext in tqdm(list_files(
            args.input)):
        if file_ext in set(['.avi', '.mp4']):
            print(f'To images: {full_path}')

            video_duration, video_fps = get_video_info(full_path)

            step = math.ceil(video_fps / sample_fps)
            if args.tool == "ffmpeg":
                for s in tqdm(range(int(video_duration))):
                    for t in range(sample_fps):
                        st = s + t / sample_fps
                        command = f'ffmpeg -ss {st} -i {full_path} -qmin 1 -q:v 1 -vframes 1 {args.output}/{file_name}_{st:08.2f}.jpg'
                        result = run_shell_command(command)
            elif args.tool == "vlc":
                command = f"vlc {full_path} --intf=dummy --rate=5 --video-filter=scene --vout=dummy --scene-format=jpg \
                    --scene-ratio={step} --scene-path={args.output}  --scene-prefix={file_name}_ vlc://quit"
Beispiel #12
0
def demo_bounds():
    files = list_files(SOURCE, EXTENSION, result_cap=100)
    i = 0
    image = io.imread(files[i])
    plt.figure(i)
    show_plot_with_bounds_for_image(image)