Spaces:
Sleeping
Sleeping
import os | |
import logging | |
from cleanvision.imagelab import Imagelab | |
# Set up logging | |
logging.basicConfig( | |
level=logging.INFO, | |
format="%(asctime)s - %(levelname)s - %(message)s", | |
) | |
def delete_images_with_issues(directory): | |
# Initialize Imagelab with the directory of images | |
imagelab = Imagelab(directory) | |
# Run the inspection to identify images with issues | |
issues = imagelab.find_issues() | |
issue_columns = imagelab.issues.filter(like="issue") | |
# Use where to replace rows that don't have any True value with NaN | |
filtered_df = imagelab.issues.where(issue_columns.any(axis=1)) | |
# Drop the rows with NaN values (i.e., rows where no issue column was True) | |
filtered_df = filtered_df.dropna() | |
# Display the filtered DataFrame | |
filtered_df.index.to_list() | |
# Iterate over the issues and delete the corresponding images | |
for issue in filtered_df.index.to_list(): | |
image_path = issue | |
try: | |
os.remove(image_path) | |
logging.info(f"Deleted: {image_path}") | |
except Exception as e: | |
logging.error(f"Error deleting {image_path}: {e}") | |
if __name__ == "__main__": | |
# path = "/Users/andrewmayes/Project/document-type-detection/data/non_object" | |
path = "/home/user/app/images/" | |
delete_images_with_issues(path) | |