File size: 1,318 Bytes
a8d7e7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)