Skip to content

Commit 0b8a523

Browse files
committed
Add: copy_file - Start working on the output feature.
Modify: some output/logging changes.
2 parents 888561b + 6fe6bd4 commit 0b8a523

File tree

6 files changed

+54
-60
lines changed

6 files changed

+54
-60
lines changed

README.md

Lines changed: 2 additions & 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.
@@ -67,5 +60,6 @@ Arguments:
6760
- [X] Getting docker out of the loop (each user install his own DeepDetect)
6861
- [X] ResNet 50 integration
6962
- [ ] Output folder (copy and not rename)
63+
- [ ] NSFW tagging (Yahoo open_nsfw)
7064
- [ ] XMP metadata writing
7165
- [ ] GPU support

cmd/deepsort/classify.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ package main
22

33
import (
44
"bytes"
5-
"fmt"
65
"io/ioutil"
76
"net/http"
87
"os"
98
"path/filepath"
109
"sync"
1110

11+
"crypto/md5"
12+
"encoding/base64"
13+
"encoding/hex"
14+
1215
"github.com/CorentinB/DeepSort/pkg/logging"
1316
"github.com/labstack/gommon/color"
1417
)
1518

16-
func googleNetClassification(path string, arguments *Arguments, wg *sync.WaitGroup) {
19+
func googleNetClassification(path string, content []byte, arguments *Arguments, wg *sync.WaitGroup) {
1720
defer wg.Done()
1821
url := arguments.URL + "/predict"
19-
path, _ = filepath.Abs(path)
20-
var jsonStr = []byte(`{"service":"deepsort-resnet","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
22+
dataStr := base64.StdEncoding.EncodeToString(content)
23+
var jsonStr = []byte(`{"service":"deepsort-resnet","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + dataStr + `"]}`)
2124
// DEBUG
2225
//fmt.Println("Request: " + string(jsonStr))
2326
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
@@ -44,20 +47,18 @@ func googleNetClassification(path string, arguments *Arguments, wg *sync.WaitGro
4447
color.Green(parsedResponse), "[GoogleNet]")
4548
}
4649
if arguments.DryRun != true {
47-
if isValid(arguments.Output) == false {
48-
renameFile(path, arguments, parsedResponse)
49-
} else {
50-
fmt.Println("Output selected")
51-
}
50+
hashBytes := md5.Sum(content)
51+
hash := hex.EncodeToString(hashBytes[:])
52+
renameFile(path, hash, arguments, parsedResponse)
5253
}
5354
arguments.CountDone++
5455
}
5556

56-
func resNet50Classification(path string, arguments *Arguments, wg *sync.WaitGroup) {
57+
func resNet50Classification(path string, content []byte, arguments *Arguments, wg *sync.WaitGroup) {
5758
defer wg.Done()
5859
url := arguments.URL + "/predict"
59-
path, _ = filepath.Abs(path)
60-
var jsonStr = []byte(`{"service":"deepsort-resnet-50","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
60+
dataStr := base64.StdEncoding.EncodeToString(content)
61+
var jsonStr = []byte(`{"service":"deepsort-resnet-50","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + dataStr + `"]}`)
6162
// DEBUG
6263
//fmt.Println("Request: " + string(jsonStr))
6364
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
@@ -84,14 +85,9 @@ func resNet50Classification(path string, arguments *Arguments, wg *sync.WaitGrou
8485
color.Green(parsedResponse), "[ResNet-50]")
8586
}
8687
if arguments.DryRun != true {
87-
if isValid(arguments.Output) == false &&
88-
arguments.OutputChoice == false {
89-
renameFile(path, arguments, parsedResponse)
90-
} else if arguments.OutputChoice == true &&
91-
isValid(arguments.Output) == false {
92-
logging.Error("Wrong output folder.", "")
93-
os.Exit(1)
94-
}
88+
hashBytes := md5.Sum(content)
89+
hash := hex.EncodeToString(hashBytes[:])
90+
renameFile(path, hash, arguments, parsedResponse)
9591
}
9692
arguments.CountDone++
9793
}

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)