From b3b5b94dacf15010b544a91db4e0ef0b719af503 Mon Sep 17 00:00:00 2001 From: Ian Andwati Date: Fri, 16 Jun 2023 10:25:54 +0300 Subject: [PATCH] feat: added multithreading --- .gitignore | 3 ++- README.md | 1 + cli.py | 19 +++++++++++-------- sorter/image_sorter.py | 11 +++++++++++ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index f2c4413..26a416e 100644 --- a/.gitignore +++ b/.gitignore @@ -172,4 +172,5 @@ pyrightconfig.json rejects/ done/ -.vscode/ \ No newline at end of file +.vscode/ +image_sorter.log \ No newline at end of file diff --git a/README.md b/README.md index 40c5ac8..0d30845 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,4 @@ python cli.py --source --destination --rejects - Remove blurry images - Use multithreading to increase performance - Remove images with no meter readings +- Progress functionality diff --git a/cli.py b/cli.py index 28c7a1e..7b9ee43 100644 --- a/cli.py +++ b/cli.py @@ -2,6 +2,7 @@ import argparse from pathlib import Path import cv2 from sorter.image_sorter import process_image +from concurrent.futures import ThreadPoolExecutor # CLI argument parsing def parse_arguments(): @@ -35,14 +36,16 @@ def main(): image_paths = list(input_folder.glob("*.jpg")) + list(input_folder.glob("*.png")) # Process the images - for image_path in image_paths: - """ - try catch block to handle errors in processing individual images. - """ - try: - process_image(image_path, pass_folder, reject_folder) - except cv2.error as e: - print(f"Error processing image {image_path.name}: {str(e)}") + with ThreadPoolExecutor() as executor: + for image_path in image_paths: + """ + try catch block to handle errors in processing individual images. + """ + try: + #process_image(image_path, pass_folder, reject_folder) + executor.submit(process_image, image_path, pass_folder, reject_folder) + except cv2.error as e: + print(f"Error processing image {image_path.name}: {str(e)}") print("Sorting complete.") diff --git a/sorter/image_sorter.py b/sorter/image_sorter.py index 80bf09b..348c4fd 100644 --- a/sorter/image_sorter.py +++ b/sorter/image_sorter.py @@ -14,6 +14,17 @@ threshold_type = cv2.THRESH_BINARY logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" , datefmt="%Y-%m-%d %H:%M:%S") logger = logging.getLogger(__name__) +# Create a file handler and set the log file path +log_file = "./image_sorter.log" +file_handler = logging.FileHandler(log_file) + +# Configure the file handler to use a more verbose log format than the console output +formatter = logging.Formatter("[%(levelname)s] %(asctime)s %(message)s") +file_handler.setFormatter(formatter) + +# Add the file handler to the logger +logger.addHandler(file_handler) + def is_image_below_threshold(image_path, size_threshold): """Check if the image size is below the threshold.