|
35 | 35 | import json
|
36 | 36 | from os.path import exists, isdir
|
37 | 37 | from os import mkdir
|
| 38 | +import numpy as np |
38 | 39 |
|
39 | 40 |
|
40 | 41 | class HandleFileNames:
|
@@ -375,6 +376,97 @@ def read_filenames(fname, path=None):
|
375 | 376 |
|
376 | 377 | return handle_files
|
377 | 378 |
|
| 379 | + @staticmethod |
| 380 | + def get_name_base_no_number(fname): |
| 381 | + """Take out everything after ., and take out numbers |
| 382 | + @param fname - the file name |
| 383 | + @return the file name, stripped""" |
| 384 | + name_pieces1 = fname.split(".")[0] |
| 385 | + name_pieces2 = name_pieces1.split("_")[0] |
| 386 | + name = "rbg" |
| 387 | + for s in name_pieces2: |
| 388 | + if |
| 389 | + name_pieces_prev = fnames_images[im_prev] |
| 390 | + |
| 391 | +def make_blueberry_dataset(path_src, path_dest, img_type="jpg", n_each_folder=2, pair_spacing=0): |
| 392 | + """Assumes there's a folder with multiple folders with multiple images in each folder |
| 393 | + Grab n_each_folder evenly spaced (time wise) for each director, either single or pairs |
| 394 | + @param path_src - directory that has the directories |
| 395 | + @param path_dest - directory to put the images in (will put images from each in a sub folder) |
| 396 | + @param img_type - one of jpg, png, etc |
| 397 | + @param n_each_folder - how many frames to grab from each sub folder |
| 398 | + @param pair_spacing - grab pairs of images, spaced n apart in time (if zero, only grabs one image)""" |
| 399 | + |
| 400 | + from os import system |
| 401 | + |
| 402 | + if not exists(path_src): |
| 403 | + print(f"No directory {path_src} exists, bailing") |
| 404 | + return |
| 405 | + |
| 406 | + if not exists(path_dest): |
| 407 | + mkdir(path_dest) |
| 408 | + |
| 409 | + search_path = f"{path_src}/*" |
| 410 | + fnames = glob(search_path) |
| 411 | + if fnames is None: |
| 412 | + raise ValueError(f"No sub directories in directory {search_path}") |
| 413 | + |
| 414 | + # Look in path_src for the sub directories |
| 415 | + fnames.sort() |
| 416 | + # Handles the case where the files are in the given directory |
| 417 | + fnames.append(".") |
| 418 | + for n in fnames: |
| 419 | + if not isdir(n): |
| 420 | + continue |
| 421 | + |
| 422 | + # List of images |
| 423 | + search_dir_path = f"{path_src}/{n}/*.{img_type}" |
| 424 | + fnames_images = glob(search_dir_path) |
| 425 | + path_dest_subdir = f"{path_dest}/" |
| 426 | + b_use_sub_folder_names = False |
| 427 | + if fnames_images is None: |
| 428 | + search_dir_path = f"{path_src}/{n}/color/*.{img_type}" |
| 429 | + fnames_images = glob(search_dir_path) |
| 430 | + path_dest_subdir = f"{path_dest}/{n}/" |
| 431 | + b_use_sub_folder_names = True |
| 432 | + |
| 433 | + if fnames_images is None: |
| 434 | + print("Subdir {n} has no images of type {img_type}") |
| 435 | + continue; |
| 436 | + |
| 437 | + if not exists(path_dest_subdir): |
| 438 | + if b_use_sub_folder_names: |
| 439 | + mkdir(path_dest_subdir) |
| 440 | + |
| 441 | + # Copy the images over first |
| 442 | + fnames_images.sort() |
| 443 | + im_step = len(fnames_images) // n_each_folder |
| 444 | + im_keep = np.linspace(im_step // 2, len(fnames_images), n_each_folder) |
| 445 | + for im in im_keep: |
| 446 | + im_i = int(im) |
| 447 | + im_prev = max(0, im_i - pair_spacing // 2) |
| 448 | + im_next = min(im_prev + pair_spacing, len(fnames_images) - 1) |
| 449 | + |
| 450 | + name_pieces_prev = fnames_images[im_prev].split("_") |
| 451 | + name_prev = "" |
| 452 | + for s in name_pieces_prev: |
| 453 | + name_pieces_prev = fnames_images[im_prev] |
| 454 | + sys_cmd_str = f"cp {search_dir_path}/{fnames_images[im_prev]} {path_dest_subdir}{fnames_images[im_prev]}" |
| 455 | + system(sys_cmd_str) |
| 456 | + |
| 457 | + if im_next != im_prev: |
| 458 | + sys_cmd_str = f"cp {search_dir_path}/{fnames_images[im_next]} {path_dest_subdir}{fnames_images[im_next]}" |
| 459 | + system(sys_cmd_str) |
| 460 | + |
| 461 | + if b_use_sub_folder_names: |
| 462 | + all_files.add |
| 463 | + |
| 464 | + |
| 465 | + |
| 466 | + all_files = HandleFileNames(path_dest, img_type=img_type) |
| 467 | + |
| 468 | + |
| 469 | + |
378 | 470 |
|
379 | 471 | if __name__ == '__main__':
|
380 | 472 | # Example bb
|
|
0 commit comments