Skip to content

Commit 6fe6bd4

Browse files
authored
Merge pull request #3 from terorie/master
Send images as Base64 to DeepDetect
2 parents 0c8f26d + d365b25 commit 6fe6bd4

File tree

6 files changed

+52
-48
lines changed

6 files changed

+52
-48
lines changed

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ Because sometimes, you have folders full of badly named pictures, and you want t
1414
You need DeepDetect installed, the easiest way is using docker:
1515
```
1616
docker pull beniz/deepdetect_cpu
17-
docker run -d -p 8080:8080 -v /path/to/images:/path/to/images beniz/deepdetect_cpu
18-
```
19-
20-
PLEASE NOTE THAT THE PATH IN THE HOST SHOULD BE THE SAME IN THE CONTAINER!
21-
22-
Example:
23-
```
24-
docker run -d -p 8080:8080 -v /home/corentin/Images:/home/corentin/Images beniz/deepdetect_cpu
17+
docker run -d -p 8080:8080 beniz/deepdetect_cpu
2518
```
2619

2720
Right now, the only supported installation of DeepDetect that works with DeepSort is the deepdetect_cpu container.

cmd/deepsort/classify.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import (
1010

1111
"github.com/CorentinB/DeepSort/pkg/logging"
1212
"github.com/labstack/gommon/color"
13+
"encoding/base64"
14+
"encoding/hex"
15+
"crypto/md5"
1316
)
1417

15-
func googleNetClassification(path string, arguments *Arguments, wg *sync.WaitGroup) {
18+
func googleNetClassification(path string, content []byte, arguments *Arguments, wg *sync.WaitGroup) {
1619
defer wg.Done()
1720
url := arguments.URL + "/predict"
18-
path, _ = filepath.Abs(path)
19-
var jsonStr = []byte(`{"service":"deepsort-resnet","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
21+
dataStr := base64.StdEncoding.EncodeToString(content)
22+
var jsonStr = []byte(`{"service":"deepsort-resnet","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + dataStr + `"]}`)
2023
// DEBUG
2124
//fmt.Println("Request: " + string(jsonStr))
2225
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
@@ -43,16 +46,18 @@ func googleNetClassification(path string, arguments *Arguments, wg *sync.WaitGro
4346
color.Green(parsedResponse), "[GoogleNet]")
4447
}
4548
if arguments.DryRun != true {
46-
renameFile(path, arguments, parsedResponse)
49+
hashBytes := md5.Sum(content)
50+
hash := hex.EncodeToString(hashBytes[:])
51+
renameFile(path, hash, arguments, parsedResponse)
4752
}
4853
arguments.CountDone++
4954
}
5055

51-
func resNet50Classification(path string, arguments *Arguments, wg *sync.WaitGroup) {
56+
func resNet50Classification(path string, content []byte, arguments *Arguments, wg *sync.WaitGroup) {
5257
defer wg.Done()
5358
url := arguments.URL + "/predict"
54-
path, _ = filepath.Abs(path)
55-
var jsonStr = []byte(`{"service":"deepsort-resnet-50","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
59+
dataStr := base64.StdEncoding.EncodeToString(content)
60+
var jsonStr = []byte(`{"service":"deepsort-resnet-50","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + dataStr + `"]}`)
5661
// DEBUG
5762
//fmt.Println("Request: " + string(jsonStr))
5863
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
@@ -79,7 +84,9 @@ func resNet50Classification(path string, arguments *Arguments, wg *sync.WaitGrou
7984
color.Green(parsedResponse), "[ResNet-50]")
8085
}
8186
if arguments.DryRun != true {
82-
renameFile(path, arguments, parsedResponse)
87+
hashBytes := md5.Sum(content)
88+
hash := hex.EncodeToString(hashBytes[:])
89+
renameFile(path, hash, arguments, parsedResponse)
8390
}
8491
arguments.CountDone++
8592
}

cmd/deepsort/hash_file.go

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

cmd/deepsort/process.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ func run(arguments *Arguments) {
3535
count++
3636
wg.Add(1)
3737
if arguments.Network == "resnet-50" {
38-
go resNet50Classification(path, arguments, &wg)
38+
go resNet50Classification(path, buf, arguments, &wg)
3939
} else {
40-
go googleNetClassification(path, arguments, &wg)
40+
go googleNetClassification(path, buf, arguments, &wg)
4141
}
4242
if count == arguments.Jobs {
4343
wg.Wait()
@@ -69,9 +69,9 @@ func runRecursively(arguments *Arguments) ([]string, error) {
6969
count++
7070
wg.Add(1)
7171
if arguments.Network == "resnet-50" {
72-
go resNet50Classification(file, arguments, &wg)
72+
go resNet50Classification(file, buf, arguments, &wg)
7373
} else {
74-
go googleNetClassification(file, arguments, &wg)
74+
go googleNetClassification(file, buf, arguments, &wg)
7575
}
7676
}
7777
if count == arguments.Jobs {

cmd/deepsort/read_file.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/base64"
6+
"github.com/CorentinB/DeepSort/pkg/logging"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
)
11+
12+
// Reads file and returns data string for DeepDetect
13+
func readFile(filePath string) string {
14+
f, err := os.Open(filePath)
15+
if err != nil {
16+
logging.Error("Can't open the file.", "["+filepath.Base(filePath)+"]")
17+
os.Exit(1)
18+
}
19+
defer f.Close()
20+
21+
var buf bytes.Buffer
22+
enc := base64.NewEncoder(base64.StdEncoding, &buf)
23+
24+
if _, err := io.Copy(enc, f); err != nil {
25+
logging.Error("Can't read the file.", "["+filepath.Base(filePath)+"]")
26+
os.Exit(1)
27+
}
28+
29+
enc.Close()
30+
return buf.String()
31+
}

cmd/deepsort/rename_file.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"github.com/CorentinB/DeepSort/pkg/logging"
88
)
99

10-
func renameFile(path string, arguments *Arguments, response string) {
10+
func renameFile(path string, hash string, arguments *Arguments, response string) {
1111
absPath, _ := filepath.Abs(path)
12-
hash := hashFileMD5(absPath)
1312
dirPath := filepath.Dir(absPath)
1413
extension := path[len(path)-4:]
1514
newName := response + "_" + hash + extension

0 commit comments

Comments
 (0)