Skip to content

Commit 1adb921

Browse files
authored
Merge pull request #4 from terorie/refactor
Refactor of the project + output feature
2 parents 0b8a523 + f226a95 commit 1adb921

22 files changed

+492
-498
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Executable builds
2+
/deepsort
3+
/cmd/deepsort/deepsort
4+
deepsort.exe
5+
6+
# OS files
7+
desktop.ini
8+
.DS_Store

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
language: go
22
go:
3-
- "1.10.2"
4-
5-
before_script: cd cmd/deepsort/
3+
- "1.10.2"

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Right now, the only supported installation of DeepDetect that works with DeepSor
2121

2222
Then, download the latest DeepSort release from https://github.com/CorentinB/DeepSort/releases
2323

24-
Unzip your release, rename it `deepsort` and make it executable with:
24+
Unzip your release, rename it `DeepSort` and make it executable with:
2525
```
26-
chmod +x deepsort
26+
chmod +x DeepSort
2727
```
2828

2929
# Usage
@@ -34,10 +34,10 @@ DeepSort support few different parameters, you're obliged to fill two of them:
3434

3535
For more informations, refeer to the helper:
3636
```
37-
./deepsort --help
37+
./DeepSort --help
3838
3939
[-u|--url] is required
40-
usage: deepsort [-h|--help] -u|--url "<value>" -i|--input "<value>"
40+
usage: DeepSort [-h|--help] -u|--url "<value>" -i|--input "<value>"
4141
[-n|--network (resnet-50|googlenet)] [-R|--recursive]
4242
[-j|--jobs <integer>] [-d|--dry-run]
4343

arguments.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/akamensky/argparse"
9+
)
10+
11+
var arguments = struct {
12+
Input string
13+
Output string
14+
URL string
15+
DryRun bool
16+
Recursive bool
17+
Jobs int
18+
Network string
19+
}{
20+
// Default arguments
21+
Jobs: 1,
22+
}
23+
24+
func argumentParsing(args []string) {
25+
// Create new parser object
26+
parser := argparse.NewParser("deepsort", "AI powered image tagger backed by DeepDetect")
27+
28+
// Create flags
29+
URL := parser.String("u", "url", &argparse.Options{
30+
Required: true,
31+
Help: "URL of your DeepDetect instance (i.e: http://localhost:8080)"})
32+
33+
input := parser.String("i", "input", &argparse.Options{
34+
Required: true,
35+
Help: "Your input folder."})
36+
37+
output := parser.String("o", "output", &argparse.Options{
38+
Required: false,
39+
Help: "Your output folder, if output is set, " +
40+
"original files will not be renamed, " +
41+
"but the renamed version will be copied in the output folder."})
42+
43+
network := parser.Selector("n", "network", []string{"resnet-50", "googlenet"}, &argparse.Options{
44+
Required: false,
45+
Help: "The pre-trained deep neural network you want to use, " +
46+
"can be resnet-50 or googlenet",
47+
Default: "resnet-50"})
48+
49+
recursive := parser.Flag("R", "recursive", &argparse.Options{
50+
Required: false,
51+
Help: "Process files recursively."})
52+
53+
jobs := parser.Int("j", "jobs", &argparse.Options{
54+
Required: false,
55+
Help: "Number of parallel jobs",
56+
Default: 1})
57+
58+
dryRun := parser.Flag("d", "dry-run", &argparse.Options{
59+
Required: false,
60+
Help: "Just classify images and return results, do not apply."})
61+
62+
// Parse input
63+
err := parser.Parse(args)
64+
if err != nil {
65+
// In case of error print error and print usage
66+
// This can also be done by passing -h or --help flags
67+
fmt.Print(parser.Usage(err))
68+
os.Exit(0)
69+
}
70+
71+
// Convert path parameters to absolute paths
72+
inputFolder, _ := filepath.Abs(*input)
73+
var outputFolder string
74+
if *output != "" {
75+
outputFolder, _ = filepath.Abs(*output)
76+
}
77+
78+
// Finally save the collected flags
79+
arguments.Network = *network
80+
arguments.Jobs = *jobs
81+
arguments.DryRun = *dryRun
82+
arguments.Recursive = *recursive
83+
arguments.Input = inputFolder
84+
arguments.Output = outputFolder
85+
arguments.URL = *URL
86+
}

classify.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"encoding/base64"
5+
"net/http"
6+
"strings"
7+
"io/ioutil"
8+
"github.com/savaki/jq"
9+
)
10+
11+
// Classify runs the image through the classification engine and
12+
// returns a slice of tags
13+
func (c *ClassificationService) Classify(image []byte) ([]string, error) {
14+
url := c.URL + "/predict"
15+
16+
// Send image over as base64
17+
dataStr := base64.StdEncoding.EncodeToString(image)
18+
19+
req, err := http.NewRequest("POST", url, strings.NewReader(`{
20+
"service": "` + c.ID+ `",
21+
"parameters": {
22+
"input": {
23+
"width":224,
24+
"height":224
25+
},
26+
"output": { "best":1 },
27+
"mllib": { "gpu":false }
28+
},
29+
"data":["` + dataStr + `"]
30+
}`))
31+
32+
resp, err := c.Conn.Do(req)
33+
if err != nil { return nil, err }
34+
defer resp.Body.Close()
35+
36+
body, _ := ioutil.ReadAll(resp.Body)
37+
38+
tagsJSON, _ := jq.Parse(".body.predictions.[0].classes.[0].cat")
39+
value, _ := tagsJSON.Apply(body)
40+
class := strings.Split(string(value), " ")
41+
class = class[1:]
42+
43+
return class, nil
44+
}

cmd/deepsort/arguments.go

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

cmd/deepsort/classify.go

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

cmd/deepsort/copy_file.go

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

0 commit comments

Comments
 (0)