Skip to content

Commit d85475e

Browse files
committed
Add: -R / --recursive argument, now doesnt do recursive classification by default.
1 parent 45ad1e2 commit d85475e

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

cmd/deepsort/arguments.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func argumentParsing(args []string, argument *Arguments) {
1515
URL := parser.String("u", "url", &argparse.Options{Required: true, Help: "URL of your DeepDetect instance (i.e: http://localhost:8080)"})
1616
input := parser.String("i", "input", &argparse.Options{Required: true, Help: "Your input folder."})
1717
dryRun := parser.Flag("d", "dry-run", &argparse.Options{Required: false, Help: "Just classify images and return results, do not apply."})
18+
recursive := parser.Flag("R", "recursive", &argparse.Options{Required: false, Help: "Process files recursively."})
1819
// Parse input
1920
err := parser.Parse(os.Args)
2021
if err != nil {
@@ -27,7 +28,7 @@ func argumentParsing(args []string, argument *Arguments) {
2728
inputFolder, _ := filepath.Abs(*input)
2829
// Finally save the collected flags
2930
argument.DryRun = *dryRun
30-
fmt.Println(argument.DryRun)
31+
argument.Recursive = *recursive
3132
argument.Input = inputFolder
3233
argument.URL = *URL
3334
}

cmd/deepsort/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ import (
99
)
1010

1111
type Arguments struct {
12-
Input string
13-
URL string
14-
DryRun bool
12+
Input string
13+
URL string
14+
DryRun bool
15+
Recursive bool
1516
}
1617

1718
func main() {
1819
start := time.Now()
1920
arguments := new(Arguments)
2021
argumentParsing(os.Args, arguments)
2122
startGoogleNet(arguments)
22-
logging.Success("Starting image classification..", "[GoogleNet]")
23-
runRecursively(arguments)
23+
if arguments.Recursive == true {
24+
logging.Success("Starting image classification recursively..", "[GoogleNet]")
25+
runRecursively(arguments)
26+
} else {
27+
logging.Success("Starting image classification..", "[GoogleNet]")
28+
run(arguments)
29+
}
2430
color.Println(color.Cyan("Done in ") + color.Yellow(time.Since(start)) + color.Cyan("!"))
2531
}

cmd/deepsort/process_recursively.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,44 @@ package main
22

33
import (
44
"io/ioutil"
5+
"log"
56
"os"
67
"path/filepath"
78

89
"github.com/CorentinB/DeepSort/pkg/logging"
910
filetype "gopkg.in/h2non/filetype.v1"
1011
)
1112

12-
func runRecursively(arguments *Arguments) ([]string, error) {
13-
searchDir := arguments.Input
13+
func run(arguments *Arguments) {
14+
f, err := os.Open(arguments.Input)
15+
if err != nil {
16+
log.Fatal(err)
17+
}
18+
files, err := f.Readdir(-1)
19+
f.Close()
20+
if err != nil {
21+
logging.Error("Unable to process this directory.", "["+arguments.Input+"]")
22+
os.Exit(1)
23+
}
24+
25+
for _, file := range files {
26+
path := arguments.Input + "/" + file.Name()
27+
buf, _ := ioutil.ReadFile(path)
28+
if filetype.IsImage(buf) {
29+
googleNetClassification(path, arguments)
30+
}
31+
}
32+
}
1433

34+
func runRecursively(arguments *Arguments) ([]string, error) {
1535
fileList := make([]string, 0)
16-
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
36+
e := filepath.Walk(arguments.Input, func(path string, f os.FileInfo, err error) error {
1737
fileList = append(fileList, path)
1838
return err
1939
})
2040

2141
if e != nil {
22-
logging.Error("Unable to process this directory.", "["+searchDir+"]")
42+
logging.Error("Unable to process this directory.", "["+arguments.Input+"]")
2343
os.Exit(1)
2444
}
2545

0 commit comments

Comments
 (0)