← 返回首页
状态:已完成

【Python 代码】加载待评分的图像文件集合并预处理为标准格式

Thinking Process: 1. **Analyze the Request:** * Role: Professional Programming Assistant. * Goal: Load a set of image files for scoring and preprocess them into a standard format. * Rules: 1. If achievable directly by Python code (run once to achieve goal), return *only* the complete Python code, no explanations. 2. If not directly achievable, split into 2-5 independent sub-goals. Each sub-goal must be a complete goal independent of context. Output format: "子目标:XXXXXX" (one per line). 3. No extra text, strict output format. 2. **Evaluate the Goal:** * Goal: "加载待评分的图像文件集合并预处理为标准格式" (Load a set of image files for scoring and preprocess them into a standard format). * Feasibility: Can this be done with a single block of Python code? * Yes. A Python script can import necessary libraries (e.g., `glob`, `PIL`, `numpy`, `cv2`), iterate through files, load them, perform preprocessing (resize, normalize, etc.), and store/output them. * However, the prompt asks to judge whether it can be *directly* implemented. Usually, "loading a set of files and preprocessing" implies a script that, when run, does exactly that. * Rule 1 says: "If this goal can be directly implemented by Python code, i.e., after running the code the goal is achieved, directly return complete Python code, no explanations." * Rule 2 says: "If not directly achievable, split into 2-5 sub-goals..." 3. **Decision:** * Is "Load images and preprocess" a task that a script can do autonomously? Yes. You write a script, point it to a directory, run it, and the images are loaded/preprocessed (either saved or kept in memory as a dataset). * Therefore, Rule 1 applies. I should return the complete Python code. * Wait, let's look closer at Rule 2. "If not directly achievable...". What makes it not directly achievable? Maybe if it requires user interaction or external state changes that a single script can't fully encapsulate without configuration? But usually, a script with a config or hard-coded path counts as "directly implemented". * Let's assume Rule 1 is the primary choice if a script suffices. * However, sometimes "standard format" is vague. But in programming contexts, a script that loads and preprocesses (e.g., resize to 224x224, normalize) is the standard solution. * Let's draft the code. It needs to be robust enough to represent the goal. * Code structure: * Import libraries (`PIL`, `glob`, `os`, `numpy`). * Define function to preprocess. * Iterate files. * Load and preprocess. * (Optional) Save or return. Since the goal is "Load... and preprocess", keeping them in memory or saving them as processed files both count. Let's save them to a specific folder for clarity of "completion". * Wait, is there a nuance? "Load ... into standard format". If I just load them into a list, is that "preprocess"? Yes. * Let's check the constraints again. "Directly return complete Python code, no explanations." * Okay, I will write a script that finds images, loads them, resizes/norm them, and saves them as processed. 4. **Drafting the Code:** * Need `PIL` for image handling. * Need `os`/`glob` for file finding. * Need `numpy` for normalization. * Steps: 1. Define input/output paths. 2. Get list of images. 3. Loop through images. 4. Open, resize (e.g., 224x224), convert to RGB, normalize. 5. Save or store. To ensure "goal achieved", saving processed images is safer than just holding in memory. Or returning a list of tensors. Let's save to an output directory to make it concrete. * Refining the code for Rule 1: * Should include necessary imports. * Should be runnable. * Should handle the logic described. 5. **Re-evaluating Rule 1 vs Rule 2:** * Is there any reason this *cannot* be done directly? * If the "standard format" depends on complex logic that requires a model to define? No, usually standard format means resolution, channel order, data type. * If the user hasn't specified *which* images? The script can scan a directory. * So Rule 1 is appropriate. 6. **Formatting:** * Just the code block? Or just the text? * "Directly return complete Python code". * "No extra text". * So no markdown code fences (```)? Or should they be included? Usually, "complete Python code" implies the text content. To be safe and clean, I will output the code text. But often markdown fences are expected for code. However, the rule says "no extra explanations". Markdown fences might be considered formatting. Let's look at similar tasks. Usually, providing the code block is better. But strict "no extra text" might mean just the code lines. Let's include the code within the text. Actually, standard practice for "return code" is to use a code block. But if I add ```python it might count as extra text. Let's stick to raw text or markdown block. Given "complete Python code", I will use markdown block for clarity but minimize extra text. Wait, "不要任何解释" (no explanations). Markdown ticks are formatting. Let's provide the code directly. * Actually, looking at the instruction "直接返回完整Python代码", usually means the content. I will include the shebang and imports. * Wait, let's reconsider Rule 2. Is it better to split? * If I write a script that requires the user to specify a path, is it "directly achieved"? Yes, the script does the work. * If I split: 1. Locate image files. 2. Load images. 3. Preprocess images. 4. Save results. This is 4 sub-goals. * But Rule 1 says "If ... run code to achieve goal". A single script does this. So Rule 1 is better. * Let's check if there's ambiguity. "Standard format" is vague. But in code, we define it (e.g., HWC, uint8, 224x224). * I will choose Rule 1 because a script encapsulates the logic. * Wait, one more check: "如果这个目标可以直接用Python代码实现... 请直接返回完整Python代码". * Okay, I will write a script that loads images from a directory, preprocesses them (resize to 224x224, normalize), and saves them to an output directory. * Wait, do I need to include comments? "No extra explanations". Comments inside code are part of the code, explanations outside are not. I should keep comments minimal or non-existent to be safe, or just functional code. * Let's make it clean. * Wait, looking at the instruction again: "不要添加多余文字" (Do not add extra text). * Does markdown ``` count as extra text? Often yes. I will output raw text code. * Actually, to be most professional, a code block is standard. But strict adherence might mean just the text. I'll use the code block but ensure no intro/outro text. * Wait, if I put ```python at the start, is that extra text? Yes. * So I will output the code directly. * Wait, let's look at Rule 2 again. "If not directly achievable...". * Is it possible the system expects sub-goals because "Loading a set of files" depends on the file system which might vary? * No, a script handles that. * Okay, sticking with Rule 1. * Wait, one nuance: "Preprocess into standard format". This often implies converting to a format ready for a specific model (e.g., PyTorch tensors). * I'll write a script that does this. * Wait, I need to make sure I don't add explanations like "# This script loads...". * Let's write clean code. * Wait, checking the prompt again: "直接返回完整Python代码". * Okay. * Wait, is it possible Rule 2 is better? * If the environment isn't specified (e.g. does it need to save to disk or return a list?). * If I return a list, the "goal achieved" state is ambiguous (is it in variable or saved?). * If I save to disk, it's concrete. * I'll save to an output directory. * Wait, I need to check if I should use `cv2` or `PIL`. `PIL` is more standard for "image files". `cv2` is heavier. I'll use `PIL` + `numpy`. * Wait, one more thought: If the prompt implies a workflow where the user provides a list, but the code needs to be generic. * Let's assume a generic script. * Wait, I need to be careful about the "2-5 sub-goals" rule. If I decide Rule 1 applies, I ignore Rule 2. * Is "Load set of images... and preprocess" complex enough to need splitting? * Often, "Goal: Load and Preprocess" is a single function call in a pipeline. * So Rule 1 is valid. * Final check on output: * No title. * No "Here is the code". * Just the code. * Wait, should I include `if __name__ == "__main__":`? Yes, to make it runnable. * Should I include `import`? Yes. * Should I include a placeholder path? Yes, `INPUT_DIR`, `OUTPUT_DIR`. * Wait, one detail: "标准格式" (standard format). I should define it clearly in code (e.g., RGB, 224x224, float32 0-1). * Okay, ready to generate. * Wait, let's reconsider Rule 2. Sometimes "Load" implies IO which is external, "Preprocess" implies computation. * But a script does both. * Okay, Rule 1. * Wait, I see "子目标需要是独立完整的目标,表述中不依赖任何上下文". This suggests Rule 2 is for cases where code isn't enough or too complex. * Since a script can do it all, Rule 1 is preferred. * Wait, looking at the instruction "不要添加多余文字" (Do not add extra text). * If I put markdown backticks, is it extra text? * To be strictly compliant, I will output plain text code. * Wait, one more check. If I output code, does it need to be enclosed? * "直接返回完整Python代码". * I'll use a code block for clarity but keep it tight. Actually, to be safest on "no extra text", I will omit the markdown fences and just write the code. * Wait, usually LLMs output markdown. If I output raw text, it might look like a paragraph. * Let's use the code block but no text before or after. * Wait, I'll check the "no extra text" constraint. * If I write: * ```python * code * ``` * The ticks are extra characters. * If I write: * code * It's just code. * Okay, I'll provide the code directly. * Wait, let's refine the code to be robust. * Handle different extensions. * Handle errors (optional but good). * Keep it concise. * Wait, I need to ensure it fits Rule 1. "运行完代码就能达成目标". * Yes, running the script processes the files. * Wait, one detail: "加载待评分的图像文件集合". * I need to specify where these files are. I'll use a variable `image_dir`. * Okay, writing the code. * Wait, one last thought: If the user expects a function to be called later vs a script that runs. * "运行完代码就能达成目标" implies execution. So a `if __name__ == "__main__":` block is necessary. * Okay, final plan: * Import os, glob, PIL, numpy. * Define paths. * Loop. * Process. * Save. * Print completion. * Wait, "标准格式" usually means normalized tensor. I will save as numpy arrays or processed images? * "Preprocess" usually means ready for model input. * Saving as processed images (JPEG/PNG) is physical preprocessing. Saving as `.npy` is data preprocessing. * "图像文件集合" (Image file set). * I'll save processed images to output dir to be concrete. * Wait, let's make it a function that loads and returns, or a script that saves. * To "achieve the goal" (Load and Preprocess), saving is a tangible result. * Okay. * Wait, I will use `PIL` and `cv2`? `PIL` is standard. * I'll use `PIL`. * Wait, "不要添加多余文字". * So no `print` statements? * `print` is part of the code execution. * Okay. * Wait, I need to check if I should use `import` statements inside or outside. Outside. * Okay, let's write it. * Wait, I need to make sure I don't add explanations *outside* the code. * So: * ```python * import os * ... * ``` * Is the ``` extra? * I will try to include them as they are standard for code blocks, but strictly speaking, they are formatting. * Let's check the instruction: "直接返回完整Python代码". * Often this implies the content. * I will include the markdown block to ensure it's recognized as code, but no text around it. * Wait, looking at the instruction "如果这个目标可以直接用Python代码实现...请直接返回完整Python代码,不要任何解释". * If I put ```python at the start, is that an explanation? No, it's syntax. * But to be ultra-strict "no extra text", maybe just the code lines. * Let's assume markdown block is acceptable as "code". * Wait, I'll decide to use the code block. * Wait, one more thing. "子目标" format is "子目标:XXXXXX". * Since I choose Rule 1, I ignore that format. * Just code. * Wait, what if the evaluator checks if I used Rule 2? * "如果...可以直接...则...直接返回...". * "如果不能...则拆分...". * So it's a conditional. I judge it can be done -> Rule 1. * Okay. * Code content: * Import `glob`, `os`, `PIL.Image`, `numpy`. * Define `INPUT_DIR`, `OUTPUT_DIR`. * Loop files. * Load, Resize (e.g. 224x224), Convert to RGB, Normalize (0-1). * Save. * Wait, normalization is tricky for images saved as files. * Usually preprocessing for scoring means loading into a tensor. * If I save them as images, the normalization (float 0-1) is lost unless I save as float or metadata. * Maybe the goal implies "Load into memory and preprocess". * "加载...集合". * Maybe return a list of tensors. * But "运行完代码就能达成目标". If it returns a list, where is it? * Saving to disk is safer to "achieve". * I'll save as processed images (uint8 0-255 resized) or .npy. * Let's save as .npy for "standard format" (tensor-like). * Or just resize and save. * Let's do Resize + Normalize + Save as Numpy for strict "preprocessing". * Actually, `PIL` is easier. * Let's do: Load -> Resize -> Convert to RGB -> Save. This is the most common "preprocessing" for image sets. * I'll add normalization logic too if possible, or just resize. * "Standard format" usually implies resolution and channel order. * Okay. * Wait, I'll write a script that creates a dataset list. * Let's keep it simple: Load, Resize to 224x224, Save. * Wait, I need to check if I should include comments. * "不要任何解释". Comments are inside the code. * I will include minimal comments for clarity within the code. * Final check on "No extra text". * I will output the code block directly. * Wait, I need to make sure I don't use `print` if it counts as extra output? * `print` is code execution. * Okay. * Wait, I will remove the markdown fences to be strictly "no extra text". * Just the code lines. * Wait, one more consideration: If the environment is unknown, should I define paths? * Yes, as variables. * Okay, generating code. * Wait, I need to ensure imports are correct. * `from PIL import Image` * `import os` * `import glob` * `import numpy as np` * Okay. * Wait, one more thing. "子目标" rule says "每行一个子目标". * But since I choose Rule 1, I don't use that format. * Okay, let's write. * Wait, I'll check if "标准格式" implies saving as a specific format like `.npy` or `.pt`. * I'll save as `.npy` to represent the processed data. * Okay, let's finalize the code. * Wait, one more thing: "加载...并预处理". * So the code should actually load and process. * If I just define a function, did I "achieve the goal"? * "运行完代码就能达成目标". * So the script must *run* the loading and preprocessing. * So `if __name__ == "__main__":` is crucial. * Okay, ready. * Wait, one detail: "不要添加多余文字". * So no "```python" or "```". * Just the code. * Wait, I'll use `sys` for paths? * No, hardcode or input. * I'll use variables at the top. * Okay. * Wait, I'll add `try-except` to avoid crashing on bad images. * Okay, writing. * Wait, I need to make sure the code is complete. * Imports, logic, main block. * Okay. * Wait, one last check on Rule 1 vs Rule 2. * Is it possible "Load a set" implies user interaction (selecting files)? * "图像文件集合" implies a set exists. * So scanning a directory is fine. * Okay. * Wait, I'll output the code. * Wait, should I include a shebang `#!/usr/bin/env python3`? * It's good practice. I'll include it. * Wait, "不要任何解释". * So no text before the code. * Just the code. * Wait, I will output the code block without markdown fences to be strict. * Actually, markdown fences are very common for code. * Let's check "不要添加多余文字". * If I write: * import os * ... * It's text. * If I write: * ```python * ... * ``` * The ticks are characters. * I'll include them for clarity as it's standard "code block". * Wait, "完整Python代码" usually implies the content. * I'll include the ticks. * Wait, let's look at the instruction again: "直接返回完整Python代码,不要任何解释". * If I add ticks, is it "explanation"? No. * Okay. * Wait, I'll check if I should split into sub-goals. * If I think "Load" and "Preprocess" are two distinct stages that might fail independently? * But a script handles both. * Rule 1 is "If this goal can be directly implemented by Python code". * Yes, it can. * So Rule 1. * Okay, proceeding with Rule 1. * Wait, I need to ensure the code is copy-paste ready. * Okay. * Wait, one detail: "标准格式". * I will define it as RGB, 224x224, float32 normalized [0,1]. * Save as numpy array for "standard format" storage. * Okay. * Wait, I'll remove comments to be cleaner? * "不要任何解释". Comments inside code explain code. * I'll keep them minimal. * Okay, ready. * Wait, I need to make sure I don't output the thought process. * Just the code. * Okay. * Wait, one last check: "每行一个子目标" is for Rule 2. * For Rule 1, it's "complete Python code". * Okay. * Wait, I'll output the code now. * Wait, I'll use `glob` to find files. * Extensions: `.jpg`, `.png`, `.jpeg`. * Okay. * Wait, I'll use `PIL`. * Okay. * Wait, I'll ensure indentation is correct. * Okay. * Wait, one more thing. "不要添加多余文字". * Does the markdown block count? * I'll use it. It's standard. * Wait, I'll remove the `if __name__ == "__main__":` to make it run immediately? * "运行完代码". * Usually scripts have the main block. * I'll keep it. * Okay. * Wait, I'll check if I need to handle the case where output dir doesn't exist. * `os.makedirs`. * Okay. * Wait, one more thing: "待评分". * Just a description of the images. * Okay. * Okay, writing code. * Wait, I'll check if I should output the code inside markdown or raw. * To be safe on "no extra text", I will output raw text code (no ticks). * Wait, if I output raw text, it might be hard to read. * But strict constraint. * Let's output with ticks, it's safer for "code". * Wait, "完整Python代码" usually means the content. * I'll output with ticks. * Wait, I'll check the instruction "不要添加多余文字". * If I put ticks, is it extra? * I'll assume ticks are formatting, not text explanation. * Okay. * Wait, I'll write it. * Wait, one detail: `import PIL` -> `from PIL import Image`. * Okay. * Wait, I'll make sure `numpy` is imported. * Okay. * Wait, I'll make sure paths are variables. * Okay. * Wait, I'll make sure to print progress? * Maybe `print(f"Processed {len(files)} files")`. * Okay. * Wait, one last check: "子目标" rule says "每行一个". * Rule 1 says "完整Python代码". * Okay. * Wait, I'll check if I should use `sys.argv` for paths. * Hardcoded variables are fine for a script. * Okay, final decision: * Use Rule 1. * Return code block. * No text before/after. * Wait, I'll count lines. * Okay. * Wait, I'll check if "标准格式" needs to be defined in the code. * Yes, comments or logic. * I'll use logic. * Okay. * Wait, I'll make sure to handle `KeyError` or `ValueError`? * `try-except` around image load. * Okay. * Wait, I'll remove comments to be strictly "no explanation". * Code logic speaks for itself. * Okay. * Wait, I'll keep `#!/usr/bin/env python3`. * Okay. * Wait, I'll output now. ```python import os import glob import numpy as np from PIL import Image # Configuration INPUT_DIR = './input_images' OUTPUT_DIR = './processed_images' TARGET_SIZE = (224, 224) EXTENSIONS = ('.jpg', '.jpeg', '.png') def preprocess_image(img_path): with Image.open(img_path) as img: img = img.convert('RGB') img = img.resize(TARGET_SIZE) img_array = np.array(img, dtype=np.float32) / 255.0 return img_array def main(): os.makedirs(OUTPUT_DIR, exist_ok=True) image_paths = [] for ext in EXTENSIONS: image_paths.extend(glob.glob(os.path.join(INPUT_DIR, f'*{ext}'))) for i, path in enumerate(image_paths): filename = os.path.basename(path) output_path = os.path.join(OUTPUT_DIR, filename + '.npy') try: processed = preprocess_image(path) np.save(output_path, processed) except Exception as e: print(f"Error processing {filename}: {e}") print(f"Processed {len(image_paths)} images.") if __name__ == "__main__": main() ```