def run(overlay, src): subdirs = get_masks_to_process(src, STRAIGHTENED_MASKS_DIR) for dir in subdirs: masks = dir["files"] avg_filename = generate_avg_filename(masks) masks = [cv2.imread(m, cv2.IMREAD_GRAYSCALE) for m in masks] avg_mask = create_average_mask(masks) dest_dir = os.path.join(dir["path"], "..", AVERAGE_MASK_DIR) try: shutil.rmtree(dest_dir) except Exception as e: pass os.makedirs(dest_dir, exist_ok=True) cv2.imwrite(os.path.join(dest_dir, avg_filename), avg_mask) if overlay: avg_image = create_average_overlay(masks) dest_dir = os.path.join(dir["path"], "..", AVERAGE_OVERLAY_DIR) try: shutil.rmtree(dest_dir) except Exception as e: pass os.makedirs(dest_dir, exist_ok=True) cv2.imwrite(os.path.join(dest_dir, avg_filename), avg_image)
def run(dest, destdir, destsub, visualize, keep, src): if not src: click.secho("No source specified. Use --src", fg="red") return subdirs = get_masks_to_process(src, STRAIGHTENED_MASKS_DIR) regr = load("tip-mask-model.joblib") # detip masks with Pool(processes=cpu_count()) as pool: pool.starmap(tip_mask, [(dir["path"], regr, visualize) for dir in subdirs]) if dest: if not os.path.exists(dest): pathlib.Path(dest).mkdir(parents=True) subdirs = get_masks_to_process(src, DETIPPED_MASKS_DIR) # moving detipped masks with Pool(processes=cpu_count()) as pool: pool.starmap(copy_results, [(dir["path"], dest, destdir, destsub) for dir in subdirs])
def run(dest, dry, src): instances_dict = {} subdirs = get_masks_to_process(src, BINARY_MASKS_DIR) for dir in subdirs: for file in dir["files"]: try: instance = assemble_instance(file) instance_key = f"{instance['UID']}___{instance['Nut']}" if instance_key not in instances_dict.keys(): instances_dict[instance_key] = {f"{instance['type']}": instance} else: instances_dict[instance_key][instance["type"]] = instance if dry: print(instance) except Exception as e: print(e) if not dry: spit_out_csv(instances_dict, dest)
def run(dest, destdir, destsub, keep, smoothen, src): if not src: click.secho("No source specified. Use --src", fg="red") return subdirs = get_masks_to_process(src, BINARY_MASKS_DIR) # straighten masks with Pool(processes=cpu_count()) as pool: pool.starmap(straighten_binary_masks, [(dir["path"], ) for dir in subdirs]) if dest and not os.path.exists(dest): pathlib.Path(dest).mkdir(parents=True) # moving masks with Pool(processes=cpu_count()) as pool: pool.starmap(copy_results, [(dir["path"], dest) for dir in subdirs])
def run(collection, csv, dry, src, type, verbose): """ Curvature: mm (describing the number of mm the carrot has been pulled down to center) Biomass: mm^2 Max_width: mm Length: mm L/W ratio: mm / mm Shoulder top / bottom: mm^2 Tip angle top / bottom: absolute value of angle up/down from center line above tipangle top: mm2 above tipangle bottom: mm2 """ mask_type = type if mask_type is None and not csv: click.echo("No mask type specified...") click.echo("run 'python phenotype.py --help to see options'") return if src is None and csv is None: click.echo("No source specified...") click.echo("run 'python phenotype.py --help to see options'") return if src is not None and csv is not None: click.echo( "Both source and csv file specified. I can only do one thing at a time" ) click.echo("run 'python phenotype.py --help to see options'") return if csv is not None and not csv.endswith(".csv"): click.echo("That doesn't look like a csv file.") return tic = timeit.default_timer() if dry: collection = None else: collection = get_collection(collection) inserted = 0 updated = 0 if src is not None: type_map = { "binary": BINARY_MASKS_DIR, "straight": STRAIGHTENED_MASKS_DIR, "detipped": DETIPPED_MASKS_DIR, } subdirs = get_masks_to_process(src, type_map[mask_type]) for dir in subdirs: for file in dir["files"]: try: instance = assemble_instance(file) except Exception as e: print(e) instance = None click.secho(file, fg="red") if instance is not None: if dry: print(instance) else: try: action = insert_or_update_instance( collection, instance, verbose) except Exception as e: click.secho(f"failed to insert {file}", fg="red") if action == "inserted": inserted += 1 elif action == "updated": updated += 1 if csv is not None: instances = assemble_instance_from_csv(csv) for instance in instances: if dry: print(instance) else: action = insert_or_update_instance(collection, instance) if action == "inserted": inserted += 1 elif action == "updated": updated += 1 toc = timeit.default_timer() duration = toc - tic msg = "Inserted %s and updated %s in %.2f seconds." % (inserted, updated, duration) click.secho(msg, fg="green")