[Python] Why is my script not adding a file into the output folder?

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    This script should take a file from an input folder, put it through a pretrained model, and put the results in the output folder. I get no errors.

    import os
    import numpy as np
    import imageio.v2 as imageio
    from cellpose import models

    model_path = r"C:\Users\TomoCube\Desktop\New Testing\trained_model\models\cellpose_residual_on_style_on_concatenation_off_trained_model_2025_05_29_14_15_22.066364"
    input_folder = r"C:\Users\TomoCube\Desktop\Input Folder"
    output_folder = r"C:\Users\TomoCube\Desktop\Output Folder"
    os.makedirs(output_folder, exist_ok=True)

    # TEST: Write a dummy file to confirm save path works
    dummy_test_path = os.path.join(output_folder, "dummy_test.tif")
    imageio.imwrite(dummy_test_path, np.zeros((10, 10), dtype='uint16'))
    print(f"✅ Dummy test file saved: {dummy_test_path}")

    model = models.CellposeModel(pretrained_model=model_path, gpu=False)

    image_files = [
    f for f in os.listdir(input_folder)
    if f.lower().endswith(('.png', '.jpg', '.tif')) and '_masks' not in f
    ]
    image_files.sort()

    for filename in image_files:
    image_path = os.path.join(input_folder, filename)
    print(f"\n Processing: {filename}")

    img = imageio.imread(image_path)
    print(f" ➤ Image shape: {img.shape}, dtype: {img.dtype}")

    masks, flows, styles = model.eval(img, channels=[0, 0])
    print(f" ➤ masks.max(): {masks.max()}, dtype: {masks.dtype}, shape: {masks.shape}")

    output_filename = f"{os.path.splitext(filename)[0]}_mask.tif"
    output_path = os.path.join(output_folder, output_filename)

    try:
    imageio.imwrite(output_path, masks.astype('uint16'))
    print(f" ✅ Mask saved: {output_path}")
    except Exception as e:
    print(f" ❌ Failed to save mask for {filename}: {e}")

    print("\n✅ All done! Masks saved to:", output_folder)


    I tried updating all possible issues but I was getting nothing.

    Continue reading...

Compartilhe esta Página