File size: 1,129 Bytes
9e14838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Utility functions used in preprocessing steps

import glob
import os
from torchvision.transforms import Resize, ToPILImage, ToTensor
import networkx as nx

# Returns all the files paths with a specific extension inside a requested root directory
def get_paths(rootdir, ext="png"):
    paths = []
    for path in glob.glob(f'{rootdir}/*/**/*.'+ext, recursive=True):
        paths.append(path)
    return paths

# Cluster the images generating a graph of connected components
def _generate_connected_components(similarities, similarity_threshold=0.80):
    graph = nx.Graph()
    for i in range(len(similarities)):
        for j in range(len(similarities)):
            if i != j and similarities[i, j] > similarity_threshold:
                graph.add_edge(i, j)

    components_list = []
    for component in nx.connected_components(graph):
        components_list.append(list(component))
    graph.clear()
    graph = None

    return components_list

# Method used to preprocess the image before features extraction in clustering step
def preprocess_images(img, shape=[128, 128]):
    img = Resize(shape)(img)
    return img