Skip to content

Commit fe0f4a9

Browse files
committed
Modify: the whole project, now the docker process isnt handled by DeepSort directly, you have to install and start DeepDetect on your own. Now deal with arguments such as --url and --input.
1 parent 9e39a41 commit fe0f4a9

File tree

9 files changed

+107
-84
lines changed

9 files changed

+107
-84
lines changed

cmd/deepsort/arguments.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/akamensky/argparse"
9+
)
10+
11+
func argumentParsing(args []string, argument *Arguments) {
12+
// Create new parser object
13+
parser := argparse.NewParser("deepsort", "AI powered image tagger backed by DeepDetect")
14+
// Create string flag
15+
URL := parser.String("u", "url", &argparse.Options{Required: true, Help: "URL of your DeepDetect instance (i.e: http://localhost:8080)"})
16+
input := parser.String("i", "input", &argparse.Options{Required: true, Help: "Your input folder."})
17+
// Parse input
18+
err := parser.Parse(os.Args)
19+
if err != nil {
20+
// In case of error print error and print usage
21+
// This can also be done by passing -h or --help flags
22+
fmt.Print(parser.Usage(err))
23+
}
24+
// Handle the input flag
25+
inputFolder, _ := filepath.Abs(*input)
26+
// Finally print the collected string
27+
argument.Input = inputFolder
28+
argument.URL = *URL
29+
}

cmd/deepsort/classify.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"bytes"
55
"io/ioutil"
66
"net/http"
7+
"os"
78
"path/filepath"
89

10+
"github.com/CorentinB/DeepSort/pkg/logging"
911
"github.com/labstack/gommon/color"
1012
)
1113

12-
func getClass(path string, name string) {
13-
url := "http://localhost:8080/predict"
14+
func getClass(path string, arguments *Arguments) {
15+
url := arguments.URL + "/predict"
1416
path, _ = filepath.Abs(path)
1517
var jsonStr = []byte(`{"service":"imageserv","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
1618
// DEBUG
@@ -20,13 +22,13 @@ func getClass(path string, name string) {
2022
client := &http.Client{}
2123
resp, err := client.Do(req)
2224
if err != nil {
23-
stopDeepDetect(name)
24-
panic(err)
25+
logging.Error("Unable to classify this file.", "["+filepath.Base(path)+"]")
26+
os.Exit(1)
2527
}
2628
defer resp.Body.Close()
2729

2830
body, _ := ioutil.ReadAll(resp.Body)
2931
parsedResponse := parseResponse(body)
3032
color.Println(color.Yellow("[") + color.Cyan(filepath.Base(path)) + color.Yellow("]") + color.Yellow(" Response: ") + color.Green(parsedResponse))
31-
renameFile(path, name, parsedResponse)
33+
renameFile(path, arguments, parsedResponse)
3234
}

cmd/deepsort/deepdetect.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"net/http"
6+
"os"
7+
8+
"github.com/CorentinB/DeepSort/pkg/logging"
9+
)
10+
11+
func startGoogleNet(arguments *Arguments) {
12+
// Starting the image classification service
13+
logging.Success("Starting the classification service..", "[GoogleNet]")
14+
url := arguments.URL + "/services/deepsort-googlenet"
15+
var jsonStr = []byte(`{"mllib":"caffe","description":"DeepSort-GoogleNet","type":"supervised","parameters":{"input":{"connector":"image"},"mllib":{"nclasses":1000}},"model":{"repository":"/opt/models/ggnet/"}}`)
16+
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
17+
client := &http.Client{}
18+
resp, err := client.Do(req)
19+
if err != nil {
20+
logging.Error("Error while starting the classification service, please check if DeepDetect is running.", "[GoogleNet]")
21+
os.Exit(1)
22+
}
23+
defer resp.Body.Close()
24+
if resp.Status != "201 Created" && resp.Status != "500 Internal Server Error" {
25+
logging.Error("Error while starting the classification service, please check if DeepDetect is running.", "[GoogleNet]")
26+
os.Exit(1)
27+
}
28+
if resp.Status == "500 Internal Server Error" {
29+
logging.Success("Looks like you already have the deepsort-googlenet service started, no need to create a new one.", "[GoogleNet]")
30+
} else {
31+
logging.Success("Successfully started the image classification service.", "[GoogleNet]")
32+
}
33+
}

cmd/deepsort/docker.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

cmd/deepsort/hash_file.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import (
44
"crypto/md5"
55
"encoding/hex"
66
"io"
7-
"log"
87
"os"
8+
"path/filepath"
9+
10+
"github.com/CorentinB/DeepSort/pkg/logging"
911
)
1012

11-
func hashFileMD5(filePath string, name string) string {
13+
func hashFileMD5(filePath string) string {
1214
h := md5.New()
1315
f, err := os.Open(filePath)
1416
if err != nil {
15-
stopDeepDetect(name)
16-
log.Fatal(err)
17+
logging.Error("Can't open the file.", "["+filepath.Base(filePath)+"]")
18+
os.Exit(1)
1719
}
1820
defer f.Close()
1921
if _, err := io.Copy(h, f); err != nil {
20-
stopDeepDetect(name)
21-
log.Fatal(err)
22+
logging.Error("Can't get checksum.", "["+filepath.Base(filePath)+"]")
23+
os.Exit(1)
2224
}
2325
return (hex.EncodeToString(h.Sum(nil)))
2426
}

cmd/deepsort/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ import (
44
"os"
55
"time"
66

7+
"github.com/CorentinB/DeepSort/pkg/logging"
78
"github.com/labstack/gommon/color"
89
)
910

11+
type Arguments struct {
12+
Input string
13+
URL string
14+
}
15+
1016
func main() {
1117
start := time.Now()
12-
name := startDeepDetect(os.Args[1])
13-
color.Println(color.Yellow("[") + color.Cyan("CONTAINER: "+name) + color.Yellow("] ") + color.Yellow("Starting image classification.. "))
14-
runRecursively(os.Args[1], name)
15-
stopDeepDetect(name)
16-
color.Println(color.Yellow("[") + color.Cyan("CONTAINER: "+name) + color.Yellow("] ") + color.Green("Successfully stopped DeepDetect. "))
18+
arguments := new(Arguments)
19+
argumentParsing(os.Args, arguments)
20+
startGoogleNet(arguments)
21+
logging.Success("Starting image classification..", "[GoogleNet]")
22+
runRecursively(arguments)
1723
color.Println(color.Cyan("Done in ") + color.Yellow(time.Since(start)) + color.Cyan("!"))
1824
}

cmd/deepsort/process_recursively.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"os"
66
"path/filepath"
77

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

11-
func runRecursively(path string, name string) ([]string, error) {
12-
searchDir := path
12+
func runRecursively(arguments *Arguments) ([]string, error) {
13+
searchDir := arguments.Input
1314

1415
fileList := make([]string, 0)
1516
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
@@ -18,14 +19,14 @@ func runRecursively(path string, name string) ([]string, error) {
1819
})
1920

2021
if e != nil {
21-
stopDeepDetect(name)
22-
panic(e)
22+
logging.Error("Unable to process this directory.", "["+searchDir+"]")
23+
os.Exit(1)
2324
}
2425

2526
for _, file := range fileList {
2627
buf, _ := ioutil.ReadFile(file)
2728
if filetype.IsImage(buf) {
28-
getClass(file, name)
29+
getClass(file, arguments)
2930
}
3031
}
3132

cmd/deepsort/rename_file.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65
"path/filepath"
6+
7+
"github.com/CorentinB/DeepSort/pkg/logging"
78
)
89

9-
func renameFile(path string, name string, response string) {
10+
func renameFile(path string, arguments *Arguments, response string) {
1011
absPath, _ := filepath.Abs(path)
11-
hash := hashFileMD5(absPath, name)
12+
hash := hashFileMD5(absPath)
1213
dirPath := filepath.Dir(absPath)
1314
extension := path[len(path)-4:]
1415
newName := response + "_" + hash + extension
1516

1617
err := os.Rename(absPath, dirPath+"/"+newName)
1718
if err != nil {
18-
fmt.Println(err)
19-
stopDeepDetect(name)
20-
return
19+
logging.Error("Unable to rename this file.", "["+filepath.Base(path)+"]")
20+
os.Exit(1)
2121
}
2222
}

pkg/logging/logging.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package logging
33
import "github.com/labstack/gommon/color"
44

55
// Error log error messages.
6-
func Error(str string) {
7-
color.Println(color.Red("[✖] ") + color.Yellow(str))
6+
func Error(str string, prefix string) {
7+
color.Println(color.Red("[✖] ") + color.Red(prefix+" ") + color.Yellow(str))
8+
}
9+
10+
// Success log success messages.
11+
func Success(str string, prefix string) {
12+
color.Println(color.Green("[✔] ") + color.Green(prefix+" ") + color.Yellow(str))
813
}

0 commit comments

Comments
 (0)