Skip to content

Commit a30a5c0

Browse files
committed
Restructuration of the whole project.
1 parent 2297e6e commit a30a5c0

File tree

10 files changed

+226
-179
lines changed

10 files changed

+226
-179
lines changed

cmd/deepsort/classify.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
7+
"path/filepath"
8+
9+
"github.com/labstack/gommon/color"
10+
)
11+
12+
func getClass(path string, name string) {
13+
url := "http://localhost:8080/predict"
14+
path, _ = filepath.Abs(path)
15+
var jsonStr = []byte(`{"service":"imageserv","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
16+
// DEBUG
17+
//fmt.Println("Request: " + string(jsonStr))
18+
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
19+
req.Close = true
20+
client := &http.Client{}
21+
resp, err := client.Do(req)
22+
if err != nil {
23+
stopDeepDetect(name)
24+
panic(err)
25+
}
26+
defer resp.Body.Close()
27+
28+
body, _ := ioutil.ReadAll(resp.Body)
29+
parsedResponse := parseResponse(body)
30+
color.Println(color.Yellow("[") + color.Cyan(filepath.Base(path)) + color.Yellow("]") + color.Yellow(" Response: ") + color.Green(parsedResponse))
31+
renameFile(path, name, parsedResponse)
32+
}

cmd/deepsort/deepsort

6.4 MB
Binary file not shown.

cmd/deepsort/docker.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"time"
11+
12+
"github.com/labstack/gommon/color"
13+
)
14+
15+
func startDeepDetect(path string) string {
16+
// Starting the DeepDetect docker image
17+
name := randString(11, charset)
18+
path, _ = filepath.Abs(path)
19+
cmd := "docker"
20+
args := []string{"run", "-d", "-p", "8080:8080", "-v", path + ":" + path, "--name", name, "beniz/deepdetect_cpu"}
21+
if err := exec.Command(cmd, args...).Run(); err != nil {
22+
fmt.Fprintln(os.Stderr, err)
23+
stopDeepDetect(name)
24+
os.Exit(1)
25+
}
26+
color.Println(color.Yellow("[") + color.Cyan("CONTAINER: "+name) + color.Yellow("] ") + color.Green("Successfully started DeepDetect. "))
27+
// Starting the image classification service
28+
time.Sleep(time.Second)
29+
url := "http://localhost:8080/services/imageserv"
30+
var jsonStr = []byte(`{"mllib":"caffe","description":"image classification service","type":"supervised","parameters":{"input":{"connector":"image"},"mllib":{"nclasses":1000}},"model":{"repository":"/opt/models/ggnet/"}}`)
31+
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
32+
client := &http.Client{}
33+
resp, err := client.Do(req)
34+
if err != nil {
35+
stopDeepDetect(name)
36+
panic(err)
37+
}
38+
defer resp.Body.Close()
39+
if resp.Status != "201 Created" {
40+
stopDeepDetect(name)
41+
os.Exit(1)
42+
}
43+
color.Println(color.Yellow("[") + color.Cyan("CONTAINER: "+name) + color.Yellow("] ") + color.Green("Successfully started the image classification service. "))
44+
return name
45+
}
46+
47+
func stopDeepDetect(name string) {
48+
// Stopping the DeepDetect docker image
49+
cmd := "docker"
50+
args := []string{"stop", name}
51+
if err := exec.Command(cmd, args...).Run(); err != nil {
52+
fmt.Fprintln(os.Stderr, err)
53+
os.Exit(1)
54+
}
55+
}

cmd/deepsort/hash_file.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"io"
7+
"log"
8+
"os"
9+
)
10+
11+
func hashFileMD5(filePath string, name string) string {
12+
h := md5.New()
13+
f, err := os.Open(filePath)
14+
if err != nil {
15+
stopDeepDetect(name)
16+
log.Fatal(err)
17+
}
18+
defer f.Close()
19+
if _, err := io.Copy(h, f); err != nil {
20+
stopDeepDetect(name)
21+
log.Fatal(err)
22+
}
23+
return (hex.EncodeToString(h.Sum(nil)))
24+
}

cmd/deepsort/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"time"
6+
7+
"github.com/labstack/gommon/color"
8+
)
9+
10+
func main() {
11+
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. "))
17+
color.Println(color.Cyan("Done in ") + color.Yellow(time.Since(start)) + color.Cyan("!"))
18+
}

cmd/deepsort/parse_response.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
6+
"github.com/savaki/jq"
7+
)
8+
9+
var replaceSpace = strings.NewReplacer(" ", "_")
10+
var replaceComma = strings.NewReplacer(",", "")
11+
var replaceDoubleQuote = strings.NewReplacer("\"", "")
12+
13+
func parseResponse(rawResponse []byte) string {
14+
op, _ := jq.Parse(".body.predictions.[0].classes.[0].cat")
15+
value, _ := op.Apply(rawResponse)
16+
class := strings.Split(string(value), " ")
17+
class = class[1:len(class)]
18+
result := strings.Join(class, " ")
19+
result = replaceSpace.Replace(result)
20+
result = replaceComma.Replace(result)
21+
result = replaceDoubleQuote.Replace(result)
22+
return (result)
23+
}
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+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
8+
filetype "gopkg.in/h2non/filetype.v1"
9+
)
10+
11+
func runRecursively(path string, name string) ([]string, error) {
12+
searchDir := path
13+
14+
fileList := make([]string, 0)
15+
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
16+
fileList = append(fileList, path)
17+
return err
18+
})
19+
20+
if e != nil {
21+
stopDeepDetect(name)
22+
panic(e)
23+
}
24+
25+
for _, file := range fileList {
26+
buf, _ := ioutil.ReadFile(file)
27+
if filetype.IsImage(buf) {
28+
getClass(file, name)
29+
}
30+
}
31+
32+
return fileList, nil
33+
}

cmd/deepsort/rand_string.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
const charset = "abcdefghijklmnopqrstuvwxyz" +
9+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
10+
11+
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
12+
13+
func randString(length int, charset string) string {
14+
b := make([]byte, length)
15+
for i := range b {
16+
b[i] = charset[seededRand.Intn(len(charset))]
17+
}
18+
return string(b)
19+
}

cmd/deepsort/rename_file.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
)
8+
9+
func renameFile(path string, name string, response string) {
10+
absPath, _ := filepath.Abs(path)
11+
hash := hashFileMD5(absPath, name)
12+
dirPath := filepath.Dir(absPath)
13+
extension := path[len(path)-4:]
14+
newName := response + "_" + hash + extension
15+
16+
err := os.Rename(absPath, dirPath+"/"+newName)
17+
if err != nil {
18+
fmt.Println(err)
19+
stopDeepDetect(name)
20+
return
21+
}
22+
}

0 commit comments

Comments
 (0)