Skip to content

Commit

Permalink
Add: copy_file - Start working on the output feature.
Browse files Browse the repository at this point in the history
Modify: some output/logging changes.
  • Loading branch information
CorentinB committed Aug 26, 2018
2 parents 888561b + 6fe6bd4 commit 0b8a523
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 60 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ Because sometimes, you have folders full of badly named pictures, and you want t
You need DeepDetect installed, the easiest way is using docker:
```
docker pull beniz/deepdetect_cpu
docker run -d -p 8080:8080 -v /path/to/images:/path/to/images beniz/deepdetect_cpu
```

PLEASE NOTE THAT THE PATH IN THE HOST SHOULD BE THE SAME IN THE CONTAINER!

Example:
```
docker run -d -p 8080:8080 -v /home/corentin/Images:/home/corentin/Images beniz/deepdetect_cpu
docker run -d -p 8080:8080 beniz/deepdetect_cpu
```

Right now, the only supported installation of DeepDetect that works with DeepSort is the deepdetect_cpu container.
Expand Down Expand Up @@ -67,5 +60,6 @@ Arguments:
- [X] Getting docker out of the loop (each user install his own DeepDetect)
- [X] ResNet 50 integration
- [ ] Output folder (copy and not rename)
- [ ] NSFW tagging (Yahoo open_nsfw)
- [ ] XMP metadata writing
- [ ] GPU support
36 changes: 16 additions & 20 deletions cmd/deepsort/classify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ package main

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sync"

"crypto/md5"
"encoding/base64"
"encoding/hex"

"github.com/CorentinB/DeepSort/pkg/logging"
"github.com/labstack/gommon/color"
)

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

func resNet50Classification(path string, arguments *Arguments, wg *sync.WaitGroup) {
func resNet50Classification(path string, content []byte, arguments *Arguments, wg *sync.WaitGroup) {
defer wg.Done()
url := arguments.URL + "/predict"
path, _ = filepath.Abs(path)
var jsonStr = []byte(`{"service":"deepsort-resnet-50","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + path + `"]}`)
dataStr := base64.StdEncoding.EncodeToString(content)
var jsonStr = []byte(`{"service":"deepsort-resnet-50","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + dataStr + `"]}`)
// DEBUG
//fmt.Println("Request: " + string(jsonStr))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
Expand All @@ -84,14 +85,9 @@ func resNet50Classification(path string, arguments *Arguments, wg *sync.WaitGrou
color.Green(parsedResponse), "[ResNet-50]")
}
if arguments.DryRun != true {
if isValid(arguments.Output) == false &&
arguments.OutputChoice == false {
renameFile(path, arguments, parsedResponse)
} else if arguments.OutputChoice == true &&
isValid(arguments.Output) == false {
logging.Error("Wrong output folder.", "")
os.Exit(1)
}
hashBytes := md5.Sum(content)
hash := hex.EncodeToString(hashBytes[:])
renameFile(path, hash, arguments, parsedResponse)
}
arguments.CountDone++
}
26 changes: 0 additions & 26 deletions cmd/deepsort/hash_file.go

This file was deleted.

8 changes: 4 additions & 4 deletions cmd/deepsort/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func run(arguments *Arguments) {
count++
wg.Add(1)
if arguments.Network == "resnet-50" {
go resNet50Classification(path, arguments, &wg)
go resNet50Classification(path, buf, arguments, &wg)
} else {
go googleNetClassification(path, arguments, &wg)
go googleNetClassification(path, buf, arguments, &wg)
}
if count == arguments.Jobs {
wg.Wait()
Expand Down Expand Up @@ -69,9 +69,9 @@ func runRecursively(arguments *Arguments) ([]string, error) {
count++
wg.Add(1)
if arguments.Network == "resnet-50" {
go resNet50Classification(file, arguments, &wg)
go resNet50Classification(file, buf, arguments, &wg)
} else {
go googleNetClassification(file, arguments, &wg)
go googleNetClassification(file, buf, arguments, &wg)
}
}
if count == arguments.Jobs {
Expand Down
31 changes: 31 additions & 0 deletions cmd/deepsort/read_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"bytes"
"encoding/base64"
"github.com/CorentinB/DeepSort/pkg/logging"
"io"
"os"
"path/filepath"
)

// Reads file and returns data string for DeepDetect
func readFile(filePath string) string {
f, err := os.Open(filePath)
if err != nil {
logging.Error("Can't open the file.", "["+filepath.Base(filePath)+"]")
os.Exit(1)
}
defer f.Close()

var buf bytes.Buffer
enc := base64.NewEncoder(base64.StdEncoding, &buf)

if _, err := io.Copy(enc, f); err != nil {
logging.Error("Can't read the file.", "["+filepath.Base(filePath)+"]")
os.Exit(1)
}

enc.Close()
return buf.String()
}
3 changes: 1 addition & 2 deletions cmd/deepsort/rename_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"github.com/CorentinB/DeepSort/pkg/logging"
)

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

0 comments on commit 0b8a523

Please sign in to comment.