@@ -23,6 +23,7 @@ import (
2323
2424var replaceSpace = strings .NewReplacer (" " , "_" )
2525var replaceComma = strings .NewReplacer ("," , "" )
26+ var replaceDoubleQuote = strings .NewReplacer ("\" " , "" )
2627
2728const charset = "abcdefghijklmnopqrstuvwxyz" +
2829 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
@@ -41,10 +42,12 @@ func hashFileMD5(filePath string, name string) string {
4142 h := md5 .New ()
4243 f , err := os .Open (filePath )
4344 if err != nil {
45+ stopDeepDetect (name )
4446 log .Fatal (err )
4547 }
4648 defer f .Close ()
4749 if _ , err := io .Copy (h , f ); err != nil {
50+ stopDeepDetect (name )
4851 log .Fatal (err )
4952 }
5053 return (hex .EncodeToString (h .Sum (nil )))
@@ -73,12 +76,13 @@ func parseResponse(rawResponse []byte) string {
7376 result := strings .Join (class , " " )
7477 result = replaceSpace .Replace (result )
7578 result = replaceComma .Replace (result )
76- result = result [: len (result )- 1 ]
79+ result = replaceDoubleQuote . Replace (result )
7780 return (result )
7881}
7982
8083func getClass (path string , name string ) {
8184 url := "http://localhost:8080/predict"
85+ path , _ = filepath .Abs (path )
8286 var jsonStr = []byte (`{"service":"imageserv","parameters":{"input":{"width":224,"height":224},"output":{"best":1},"mllib":{"gpu":false}},"data":["` + "/" + path + `"]}` )
8387 // DEBUG
8488 //fmt.Println("Request: " + string(jsonStr))
@@ -98,8 +102,8 @@ func getClass(path string, name string) {
98102 renameFile (path , name , parsedResponse )
99103}
100104
101- func runRecursively (name string ) ([]string , error ) {
102- searchDir := "Images"
105+ func runRecursively (path string , name string ) ([]string , error ) {
106+ searchDir := path
103107
104108 fileList := make ([]string , 0 )
105109 e := filepath .Walk (searchDir , func (path string , f os.FileInfo , err error ) error {
@@ -127,9 +131,10 @@ func startDeepDetect(path string) string {
127131 name := randString (11 , charset )
128132 path , _ = filepath .Abs (path )
129133 cmd := "docker"
130- args := []string {"run" , "-d" , "-p" , "8080:8080" , "-v" , path + ":/Images" , "--name" , name , "beniz/deepdetect_cpu" }
134+ args := []string {"run" , "-d" , "-p" , "8080:8080" , "-v" , path + ":" + path , "--name" , name , "beniz/deepdetect_cpu" }
131135 if err := exec .Command (cmd , args ... ).Run (); err != nil {
132136 fmt .Fprintln (os .Stderr , err )
137+ stopDeepDetect (name )
133138 os .Exit (1 )
134139 }
135140 color .Println (color .Yellow ("[" ) + color .Cyan ("CONTAINER: " + name ) + color .Yellow ("] " ) + color .Green ("Successfully started DeepDetect. " ))
@@ -141,6 +146,7 @@ func startDeepDetect(path string) string {
141146 client := & http.Client {}
142147 resp , err := client .Do (req )
143148 if err != nil {
149+ stopDeepDetect (name )
144150 panic (err )
145151 }
146152 defer resp .Body .Close ()
@@ -166,7 +172,7 @@ func main() {
166172 start := time .Now ()
167173 name := startDeepDetect (os .Args [1 ])
168174 color .Println (color .Yellow ("[" ) + color .Cyan ("CONTAINER: " + name ) + color .Yellow ("] " ) + color .Yellow ("Starting image classification.. " ))
169- runRecursively (name )
175+ runRecursively (os . Args [ 1 ], name )
170176 stopDeepDetect (name )
171177 color .Println (color .Yellow ("[" ) + color .Cyan ("CONTAINER: " + name ) + color .Yellow ("] " ) + color .Green ("Successfully stopped DeepDetect. " ))
172178 color .Println (color .Cyan ("Done in " ) + color .Yellow (time .Since (start )) + color .Cyan ("!" ))
0 commit comments