Skip to content

Commit d365b25

Browse files
committed
Optimize reading of files
1 parent 9983e47 commit d365b25

File tree

4 files changed

+18
-38
lines changed

4 files changed

+18
-38
lines changed

cmd/deepsort/classify.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ 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-
dataStr := readFile(path)
21+
dataStr := base64.StdEncoding.EncodeToString(content)
1922
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))
@@ -43,15 +46,17 @@ 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-
dataStr := readFile(path)
59+
dataStr := base64.StdEncoding.EncodeToString(content)
5560
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))
@@ -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/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)