Download orchestrator.py from binder-sa/OCR-pipeline-python: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/binder-sa/OCR-pipeline-python/resolve/main/orchestrator.py
- Command line
-
hf download hf://binder-sa/OCR-pipeline-python/orchestrator.py
-
curl -L -o orchestrator.py https://huggingface.co/binder-sa/OCR-pipeline-python/resolve/main/orchestrator.py
2.5 kB
| import os | |
| import shutil | |
| from ultralytics import YOLO | |
| # ======================================= | |
| # π‘π§ User-configurable settings | |
| # ======================================= | |
| # π Folder containing images to classify | |
| INPUT_FOLDER = r"C:\Users\ASUS\OneDrive - Binder\Desktop\test.orch" # β Modify this path | |
| # π Folder to save classified images | |
| OUTPUT_BASE = os.path.join(INPUT_FOLDER, "classified") # β Automatically inside input folder | |
| # π¦ Path to YOLOv8 classification model | |
| MODEL_PATH = r"C:\Users\ASUS\Downloads\best.pt" # β Modify this path | |
| # β Allowed classes only | |
| ALLOWED_CLASSES = {"CR1", "CR2", "CR3", "CR4", "CR5", "CR6", "CR7", "b1", "b2", "b3", "b4", "v1", "v2", "v3"} | |
| # ======================================= | |
| # π Load the model | |
| # ======================================= | |
| model = YOLO(MODEL_PATH) | |
| # ======================================= | |
| # π§ Classify a single image | |
| # ======================================= | |
| def classify_image_yolo(image_path): | |
| try: | |
| results = model(image_path, verbose=False)[0] | |
| class_id = int(results.probs.top1) | |
| class_name = model.names.get(class_id, "others") | |
| return class_name | |
| except Exception as e: | |
| print(f"β Error classifying image {image_path}: {e}") | |
| return "others" | |
| # ======================================= | |
| # π Classify and move images | |
| # ======================================= | |
| processed_files = set() | |
| for filename in os.listdir(INPUT_FOLDER): | |
| file_path = os.path.join(INPUT_FOLDER, filename) | |
| if not os.path.isfile(file_path): | |
| continue | |
| if filename in processed_files: | |
| continue | |
| class_name = classify_image_yolo(file_path) | |
| # β Check if class is allowed, otherwise assign to "others" | |
| if class_name not in ALLOWED_CLASSES: | |
| class_name = "others" | |
| # Create class folder if not exists | |
| output_folder = os.path.join(OUTPUT_BASE, class_name) | |
| os.makedirs(output_folder, exist_ok=True) | |
| destination_path = os.path.join(output_folder, filename) | |
| # Avoid duplicate images | |
| if not os.path.exists(destination_path): | |
| shutil.move(file_path, destination_path) | |
| print(f"β Moved image {filename} to [{class_name}]") | |
| processed_files.add(filename) | |
| else: | |
| print(f"β οΈ Image {filename} already exists in [{class_name}], skipped") | |
| print("π Finished classifying images using YOLOv8 Classification!") | |