def copy_inits(py_package, in_pattern, out_pattern): """ :param py_package: :param so_package: :return: """ [ os.remove(_f) for _f in listfiles(os.path.realpath(py_package), patterns=[".pyc"]) ] init_files_src = listfiles(py_package, patterns=["__init__.py"]) init_files_dst = [ parent(file).replace(in_pattern, out_pattern).replace( "/{PACKAGE}/".format(PACKAGE=os.environ["PACKAGE"]), "/{PACKAGE}/".format( PACKAGE=os.environ["PACKAGE"].replace("-", "_"))) for file in init_files_src ] commands = [ "cp {} {}".format(src, dst) for src, dst in zip(init_files_src, init_files_dst) ] [print(command) for command in commands] [os.system(command) for command in commands]
def main(args): # General config object from given config files. cfg.merge_from_file(args.config_file) # Initialize model. model = RTPanoNet( backbone=cfg.model.backbone, num_classes=cfg.model.panoptic.num_classes, things_num_classes=cfg.model.panoptic.num_thing_classes, pre_nms_thresh=cfg.model.panoptic.pre_nms_thresh, pre_nms_top_n=cfg.model.panoptic.pre_nms_top_n, nms_thresh=cfg.model.panoptic.nms_thresh, fpn_post_nms_top_n=cfg.model.panoptic.fpn_post_nms_top_n, instance_id_range=cfg.model.panoptic.instance_id_range) device = args.device model.to(device) model.load_state_dict(torch.load(args.pretrained_weight)) # Print out mode architecture for sanity checking. print(model) # Prepare for model inference. model.eval() normalize_transform = P.Normalize(mean=cfg.input.pixel_mean, std=cfg.input.pixel_std, to_bgr255=cfg.input.to_bgr255) transform = P.Compose([ P.ToTensor(), normalize_transform, ]) inputs = listfiles(args.root, [f".{args.ext}"]) for input in tqdm(inputs, desc="Processing files") : inference(model, input, transform, args.device)
def ext_modules(root, remove_c=True, filter_key=""): """ Clean up the modules by removing undesired extensions default is ["c", "o", "pyc", "pyx"] Return a list if extension to use in setup.py :param root: :param remove_c: default True :return list: """ from distutils.extension import Extension py_files = listfiles(root, patterns=[".py"], excludes=["/__init__.py", ".pyc"]) modules_dict = {} proot = parent(root) for file in py_files: file = file.replace(proot, "")[1:] parent_modules = ".".join(parent(file).replace( "/", ".").split(".")).replace("-", "_") key = "{parent_modules}.{leaf_module}".format( parent_modules=parent_modules, leaf_module=name(file)) if key.__contains__(filter_key): try: modules_dict[key].append(file) except: modules_dict[key] = [file] print(modules_dict) return [Extension(key, value) for key, value in list(modules_dict.items())]
def rename_prefixe_lib(root): """ Rename automatically the generated .so files to be consistent. :param root: :return: """ files = listfiles(root, patterns=[".so"]) files = [(file, "{}/{}.so".format(parent(file), name(name(file)))) for file in files] for (file_before, file_after) in files: command = "mv {} {}".format(file_before, file_after) print(command) os.system(command)
def get_packages(root): """ Get the list of packages availables in a root :param root: package root :return list: """ root = os.path.realpath(root) proot = parent(root) + "/" py_files = [file.rsplit(proot)[1] for file in listfiles(root)] packages = list( np.unique([parent(file).replace("/", ".") for file in py_files])) # return list(np.unique([parent(file).replace("/", ".").split(".{name_root}.".format(name_root=name(root)))[1] # for file in py_files])) return packages
def get_packages(root): """ Get the list of packages availables in a root :param root: package root :return list: """ root = os.path.realpath(root) proot = parent(root) + "/" py_files = [ file.rsplit(proot)[1] for file in listfiles(root, patterns=[".py"]) if ((os.stat(file).st_size > 0) & (not file.__contains__(".pyc"))) ] packages = list( np.unique([parent(file).replace("/", ".") for file in py_files])) # return list(np.unique([parent(file).replace("/", ".").split(".{name_root}.".format(name_root=name(root)))[1] # for file in py_files])) return packages
def copy_data(py_package, in_pattern, out_pattern): """ :param py_package: :param so_package: :return: """ data_files = listfiles(os.path.realpath(py_package), patterns=["__data__"]) output_dirs = [ parent(_f.replace(in_pattern, out_pattern)) for _f in data_files ] [os.makedirs(_output_dir, exist_ok=True) for _output_dir in output_dirs] commands = [ "cp {} {}".format(_f, _output_dir) for _f, _output_dir in zip(data_files, output_dirs) ] [print(command) for command in commands] [os.system(command) for command in commands]
def get_packages(self, root, namelib): """ Get the list of packages availables in a root :param root: package root :return list: """ root = os.path.realpath(root) files = listfiles(root, patterns=[".py"], excludes=[".pyc", "__pycache__"]) files = [f for f in files if os.stat(f).st_size > 0] py_files = [file.rsplit(self._parent_lib_root)[1] for file in files] modules = [ parent(file).replace("/", ".").split(f"{namelib}.") for file in py_files ] modules = [m for m in modules if len(m) == 2] return list(np.unique([m[1] for m in modules]))
def __init__(self, root): super(ImageGenerator, self).__init__() def add_text(string, img): font = cv2.FONT_HERSHEY_SIMPLEX # org org = (50, 50) # fontScale fontScale = 1 # Blue color in BGR color = (255, 255, 255) # Line thickness of 2 px thickness = 2 # Using cv2.putText() method return cv2.putText(img, string, org, font, fontScale, color, thickness, cv2.LINE_AA) # Load the files self.files = dict([(name(file), file) for file in listfiles(root)]) setmesh = NMesh(self.files['model']) # Set the color setmesh.set_color([0, 200, 200]) _img0 = setmesh.shot() setmesh.rotate(axis_rotation=0, theta=pi) _img1 = setmesh.shot() _img1 = cv2.flip(_img1, 0) img_master0 = np.zeros((800, 800, 3), np.uint8) img_master0[200:-200, 200:-200] = 255 - _img0 img_master1 = np.zeros((800, 800, 3), np.uint8) img_master1[200:-200, 200:-200] = 255 - _img1 for theta in np.arange(0, 2*pi, 2*pi / 360): deg = int((theta / pi) * 180) img0 = self.rotateImage(img_master0, deg) img0 = add_text(name(root), img0) img1 = self.rotateImage(img_master1, deg) img = np.zeros((800, 1600, 3), np.uint8) img[:, :800] = img0 img[:, 800:] = img1 self.setdefault('{root}/{deg}_None.None'.format(root=root, deg=str(deg).zfill(3)), img)
# keep looping until the 'q' key is pressed while True: # display the image and wait for a keypress cv2.imshow("image", image) key = cv2.waitKey() # if the 'r' key is pressed, reset the cropping region if key == ord("r"): image = clone.copy() # if the 'c' key is pressed, break from the loop elif key == 13: break # if there are two reference points, then crop the region of interest # from teh image and display it if len(refPt) == 2: roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]] cv2.imshow("ROI", roi) cv2.waitKey(0) # close all open windows cv2.destroyAllWindows() return refPt if __name__ == '__main__': files = listfiles('data', ['.jpg']) for file in tqdm(files, total=len(files)): process(file)
import sys import subprocess from gnutools.utils import listfiles, name import random argslist = list(sys.argv)[1:] root_manifest = argslist[argslist.index('--root-manifest') + 1] manifests = list( set([ name(manifest).replace('train_', '').replace('val_', '') for manifest in listfiles(root_manifest, ['.json']) ])) random.shuffle(manifests) for manifest in manifests: argslist_k = argslist + ['--manifest', name(manifest), '--epochs', '1'] print(argslist_k) p = subprocess.Popen([str(sys.executable)] + ['train.py'] + argslist_k) p.wait() if p.returncode != 0: raise subprocess.CalledProcessError(returncode=p.returncode, cmd=p.args)
def generate_rst(root, package): """ Scan a package and generate automatically a rst string to save as a rst file for documentation. :param root: package root :param package: package or module to generate the rst file :return string: """ root = os.path.realpath(root) package = os.path.realpath(package) proot = parent(root) + "/" modules = np.unique([ file.replace(proot, "").replace("/", ".").replace("py", "")[:-1] for file in listfiles(package, level=1, patterns=[".py"]) ]) module_name = package.replace(proot, "").replace("/", ".").replace(".py", "") output = "{}\n==============================================================\n\n".format( module_name) for module in modules: splits = module.split("__init__") if len(splits) == 2: path = "{}{}__init__.py".format(proot, splits[0].replace(".", "/")) else: path = "{}{}.py".format(proot, module.replace(".", "/")) with open(path, "r") as f: modules_dict = {} modules_dict[name(path)] = [] members_class = {} functions = [] lines = f.readlines() last = "" for line in lines: if ((line[:8] == " def ") | (line[:6] == "class ")): if line.__contains__("class "): name_class = line.split("class ")[1].replace( ":\n", "").split("(")[0].split("\n")[0] modules_dict[name_class] = module members_class[name_class] = [] last = "class" else: name_member_class = line.split(" def ")[1].split( "(")[0] if not name_member_class.__contains__("__"): if last == "class": members_class[name_class].append( name_member_class) last = "class" elif line[:4] == "def ": name_function = line.split("def ")[1].split("(")[0] modules_dict[name_function] = module functions.append(name_function) last = "function" for name_class, class_value in members_class.items(): output += ".. currentmodule:: {}\n\n".format(module_name) output += \ "{}\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n" \ ".. autoclass:: {}\n" \ " :members: {}\n" \ " :special-members:\n\n".format(name_class, name_class, ", ".join(class_value)) if len(functions) > 0: if not ext(module) == "__init__": output += ".. currentmodule:: {}\n\n".format(module_name) output += "{}\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n".format( ext(module)) else: output += ".. currentmodule:: {}\n\n".format(module_name) for function in functions: output += \ ".. autofunction:: {}\n".format(function) output += "\n" print( "{}\n============================================================================\n" .format(output)) return output
from gnutools.utils import listfiles import argparse import os if __name__ == "__main__": parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('dir_src') parser.add_argument('dir_dst') args = parser.parse_args() for file in listfiles(root=args.dir_src, patterns=["__init__.py"]): command = "cp {file_src} {file_dst}".format(file_src=file, file_dst=file.replace( "/" + args.dir_src, "/" + args.dir_dst)) os.system(command) print(command)