def list_local_files_raw(*filters, local_dir="."): all_entries = scandir(local_dir) all_files = (e for e in all_entries if e.is_file() and all(filt(e) for filt in filters)) for entry in all_files: path = str(Path(local_dir, entry.name)) yield RawFileInfo(local_dir, entry.name, path, entry.stat().st_size)
def deep_scandir(path, deep=False, cls=None, filter=None, traverse_filter=None, exceptions=None): filter = filter or (lambda x: True) traverse_filter = traverse_filter or (lambda x: True) for item in scandir(path, return_value=iter(()), exceptions=exceptions): item = os.path.join(get_path(path), get_path(item)) item = item if cls is None else cls(item) traverse_result = item.is_dir() and traverse_filter(item) if deep and traverse_result: new_deep = deep if not isinstance(deep, bool) and isinstance(deep, int): new_deep = deep - 1 # yield from deep_scandir(item.path, new_deep) # for subitem in deep_scandir(item.path, new_deep, cls): for subitem in deep_scandir(item.path, new_deep, cls, filter, traverse_filter, exceptions): yield subitem if item.is_dir() and not traverse_result: # Si es un directorio y no cumple el traverse_result, no merece probar filter continue if filter(item): yield item
def scandir(path='.'): if sys.version_info >= (3, 5): return os.scandir(path) try: import scandir except ImportError: return map(FakeDirEntry, os.listdir(path)) else: return scandir(path)
def list_local_files(*filters, local_dir="."): all_entries = scandir(local_dir) file_entries = (e for e in all_entries if e.is_file()) for entry in file_entries: stat = entry.stat() size = stat.st_size datetime = arrow.get(stat.st_mtime) path = str(Path(local_dir, entry.name)) info = SimpleFileInfo(local_dir, entry.name, path, size, datetime) if all(filt(info) for filt in filters): yield info
def walk(top, topdown=True, onerror=None, followlinks=False): """Like os.walk(), but faster, as it uses scandir() internally.""" # Determine which are files and which are directories dirs = [] nondirs = [] try: for entry in scandir(top): if entry.is_dir(): dirs.append(entry) else: nondirs.append(entry) except OSError as error: if onerror is not None: onerror(error) return # Yield before recursion if going top down if topdown: # Need to do some fancy footwork here as caller is allowed to modify # dir_names, and we really want them to modify dirs (list of DirEntry # objects) instead. Keep a mapping of entries keyed by name. dir_names = [] entries_by_name = {} for entry in dirs: dir_names.append(entry.name) entries_by_name[entry.name] = entry yield top, dir_names, [e.name for e in nondirs] dirs = [] for dir_name in dir_names: entry = entries_by_name.get(dir_name) if entry is None: # Only happens when caller creates a new directory and adds it # to dir_names entry = GenericDirEntry(top, dir_name) dirs.append(entry) # Recurse into sub-directories, following symbolic links if "followlinks" for entry in dirs: if followlinks or not entry.is_symlink(): new_path = join(top, entry.name) for x in walk(new_path, topdown, onerror, followlinks): yield x # Yield before recursion if going bottom up if not topdown: yield top, [e.name for e in dirs], [e.name for e in nondirs]
def getFileNameOfVersion(file_location, version_str): """ """ if not os.path.exists(file_location): logging.error(u"file location not exists:%s"%file_location) return None file_entrys = [] for entry in scandir(file_location): if entry.is_file(): file_entry.append(entry) if not file_entrys: logging.error(u"no files under file location:%s"%file_location) return None file_list = [entry.name for entry in file_entrys] for file_name in file_list: if version_str in file_name: return file_name logging.warn(u"no file have version:%s"%version_str) return None
import cv2 import os import scandir from scandir import scandir # This script generate grayscale images from the rgb images for the whole validation set input_directory = "/home/ubuntu/data/WIDER_val/images" output_directory = "/home/ubuntu/data/WIDER_val_grayscale/images" for DirName in os.listdir(input_directory): in_path = os.path.join(input_directory, DirName) out_path = os.path.join(output_directory, DirName) if not os.path.exists(out_path): os.makedirs(out_path) for image in scandir(in_path): img = os.path.join(in_path, image.name) gray_img = os.path.join(out_path, image.name) print img rgb = cv2.imread(img) gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) cv2.imwrite(gray_img, gray)
#print(args.wider_face) if args.wider_face: for DirName in os.listdir(AnnotPath): #print(DirName) annotations_path = os.path.join(AnnotPath, DirName) annotations_path = os.path.abspath(annotations_path) ImagesPath_new = os.path.join(ImagesPath, DirName) print(ImagesPath_new) ImagesPath_new = os.path.abspath(ImagesPath_new) images_path = ImagesPath_new[ImagesPath_new.find( args.seperator_images):] # split the path from this directory annot_path = annotations_path[annotations_path.find( args.seperator_annot):] # split the path from this directory for files in scandir(annotations_path): if files.is_file() and (files.name.endswith('.txt')): annot_relative_path = os.path.join(annot_path, files.name) images_relative_path = os.path.join( images_path, os.path.splitext(files.name)[0] + ".jpg") f.write( str(images_relative_path) + " " + str(annot_relative_path) + "\n") if TrainValFlag == "val": im = Image.open( os.path.join(ImagesPath_new, os.path.splitext(files.name)[0] + ".jpg")) width, height = im.size fts.write(
help='Path to all images to put in list', required=True) #parser.add_argument('-Output', help = 'Full path to output results file', required = True ) args = vars(parser.parse_args()) if args['ImagesPath'] is not None: images_path = args['ImagesPath'] #if args['Output'] is not None: # output_file = args['Output'] num_images = len(glob.glob1(images_path, "*.png")) + len( glob.glob1(images_path, "*.jpg")) + len( glob.glob1(images_path, "*.jpeg")) + len( glob.glob1(images_path, "*.pgm")) if num_images > 1: print("path: " + images_path + " contains " + str(num_images)) #file_name = os.path.join(images_path, os.path.basename(images_path) + ".txt") file_name = os.path.join(images_path, "images_list.txt") f = open(file_name, "w") for files in scandir(images_path): if files.is_file() and (files.name.endswith('.jpg') or files.name.endswith('.png') or files.name.endswith('.jpeg') or files.name.endswith('.pgm')): images_full_path = os.path.join(images_path, files.name) f.write(str(images_full_path) + "\n")
from scandir import scandir # This script generate grayscale images from the rgb images for the whole validation set input_directory = "/media/sdf/NIR-Database/images-rand" output_directory = "/media/sdf/NIR-Database/grayscale-images-rand" has_subdir = False if has_subdir == True: for DirName in os.listdir(input_directory): in_path = os.path.join(input_directory, DirName) out_path = os.path.join(output_directory, DirName) if not os.path.exists(out_path): os.makedirs(out_path) for image in scandir(in_path): img = os.path.join(in_path, image.name) gray_img = os.path.join(out_path, image.name) print img rgb = cv2.imread(img) gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) cv2.imwrite(gray_img, gray) else: for image in scandir(input_directory): img = os.path.join(input_directory, image.name) gray_img = os.path.join(output_directory, image.name) print img rgb = cv2.imread(img) gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) cv2.imwrite(gray_img, gray)
required=True) args = vars(parser.parse_args()) if args['detection_files'] is not None: detection_csv = args['detection_files'] if args['images'] is not None: images_path = args['images'] if args['output'] is not None: output_path = args['output'] #images_path = "/data/stars/user/aabubakr/pd_datasets/datasets/videos/images/vid08" #detection_csv= "/data/stars/user/aabubakr/pd_datasets/outputs/videos-ujjwal/vid08/thresh01/detection_csv" #output_path = "/data/stars/user/aabubakr/pd_datasets/outputs/videos-ujjwal/vid08/thresh01/thresh_05" for files in scandir(detection_csv): if files.is_file() and files.name.endswith('.csv'): #im_path = os.path.join(images_path, image_name + ".jpg" ) csv_path = os.path.join(detection_csv, files.name) im_path = os.path.join(images_path, os.path.splitext(files.name)[0] + ".jpg") out_path = os.path.join(output_path, os.path.splitext(files.name)[0] + ".jpg") all_bbox = ReadResultsFiles(csv_path) print(im_path) img = cv2.imread(im_path) for b in all_bbox: if b[4] >= 0.5: x_min = int(b[0])