-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
75 lines (65 loc) 路 1.83 KB
/
main.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/agatan/bktree"
"github.com/cheggaaa/pb"
)
var images []string
func isValidImagePath(path string) bool {
return strings.HasSuffix(path, "jpg") || strings.HasSuffix(path, "jpeg") || strings.HasSuffix(path, "png")
}
func traverseDirectory(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("ERROR:", err)
}
if !info.IsDir() && isValidImagePath(path) {
images = append(images, path)
}
return nil
}
func processImages() bktree.BKTree {
var tree bktree.BKTree
bar := pb.StartNew(len(images))
for _, path := range images {
bar.Increment()
tree.Add(image{path, hashImage(path)})
}
bar.Finish()
println("Images hashed and BK-tree created")
return tree
}
func aggregateImages(imagesPath string) {
fmt.Printf("Starting to cluster your images from %s\n", imagesPath)
filepath.Walk(imagesPath, traverseDirectory)
fmt.Printf("Selected %d images\n", len(images))
}
func saveClusters(threshold int, cluster_path string, tree bktree.BKTree) {
println("Creating clusters")
clusters := createClusters(tree, images, threshold)
println("Clusters created")
createDirectories(clusters, cluster_path)
}
func main() {
imagesPtr := flag.String("imagesPath", "", "String with the path where all the images are located")
thresholdPtr := flag.Int("threshold", 10, "Threshold to set similar distances")
flag.Parse()
if _, err := os.Stat(*imagesPtr); os.IsNotExist(err) {
log.Fatal("Given image path does not exist!\n")
}
cluster_path := fmt.Sprintf("%s/clusters", *imagesPtr)
if _, err := os.Stat(cluster_path); os.IsNotExist(err) {
err := os.Mkdir(cluster_path, 0755)
if err != nil {
log.Fatal(err)
}
}
aggregateImages(*imagesPtr)
tree := processImages()
saveClusters(*thresholdPtr, cluster_path, tree)
fmt.Println("Done")
}