@ -1,4 +1,6 @@ |
|||||
{ |
{ |
||||
|
"serverIP": "127.0.0.1", |
||||
|
"serverPort": "3055", |
||||
"imgWidth": 300, |
"imgWidth": 300, |
||||
"imgHeigh": 300 |
"imgHeigh": 300 |
||||
} |
} |
@ -1,9 +1,18 @@ |
|||||
package main |
package main |
||||
|
|
||||
import "fmt" |
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"net/http" |
||||
|
) |
||||
|
|
||||
func check(err error) { |
func check(err error) { |
||||
if err != nil { |
if err != nil { |
||||
fmt.Println(err) |
fmt.Println(err) |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
func checkAndReturn(err error, w http.ResponseWriter, msg string) { |
||||
|
if err != nil { |
||||
|
fmt.Fprintln(w, msg) |
||||
|
} |
||||
|
} |
@ -0,0 +1,87 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"log" |
||||
|
"net/http" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/gorilla/mux" |
||||
|
) |
||||
|
|
||||
|
type ImageModel struct { |
||||
|
File []byte `json:"file"` |
||||
|
} |
||||
|
|
||||
|
type Route struct { |
||||
|
Name string |
||||
|
Method string |
||||
|
Pattern string |
||||
|
HandlerFunc http.HandlerFunc |
||||
|
} |
||||
|
type Routes []Route |
||||
|
|
||||
|
var routes = Routes{ |
||||
|
Route{ |
||||
|
"Index", |
||||
|
"GET", |
||||
|
"/", |
||||
|
Index, |
||||
|
}, |
||||
|
Route{ |
||||
|
"NewImage", |
||||
|
"POST", |
||||
|
"/image", |
||||
|
NewImage, |
||||
|
}, |
||||
|
} |
||||
|
|
||||
|
func Logger(inner http.Handler, name string) http.Handler { |
||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
||||
|
start := time.Now() |
||||
|
|
||||
|
inner.ServeHTTP(w, r) |
||||
|
|
||||
|
log.Printf( |
||||
|
"%s\t%s\t%s\t%s", |
||||
|
r.Method, |
||||
|
r.RequestURI, |
||||
|
name, |
||||
|
time.Since(start), |
||||
|
) |
||||
|
}) |
||||
|
} |
||||
|
func NewRouter() *mux.Router { |
||||
|
router := mux.NewRouter().StrictSlash(true) |
||||
|
for _, route := range routes { |
||||
|
var handler http.Handler |
||||
|
handler = route.HandlerFunc |
||||
|
handler = Logger(handler, route.Name) |
||||
|
|
||||
|
router. |
||||
|
Methods(route.Method). |
||||
|
Path(route.Pattern). |
||||
|
Name(route.Name). |
||||
|
Handler(handler) |
||||
|
} |
||||
|
return router |
||||
|
} |
||||
|
|
||||
|
func Index(w http.ResponseWriter, r *http.Request) { |
||||
|
fmt.Fprintln(w, "send images to the /image path") |
||||
|
} |
||||
|
func NewImage(w http.ResponseWriter, r *http.Request) { |
||||
|
_, handler, err := r.FormFile("file") |
||||
|
check(err) |
||||
|
|
||||
|
//imageName := strings.Split(handler.Filename, ".")[0]
|
||||
|
//fileName := imageName + ".png"
|
||||
|
|
||||
|
//data, err := ioutil.ReadAll(file)
|
||||
|
//check(err)
|
||||
|
img := readImage(handler.Filename) |
||||
|
result := knn(dataset, img) |
||||
|
|
||||
|
fmt.Println("seems to be a " + result) |
||||
|
fmt.Fprintln(w, "seems to be a "+result) |
||||
|
} |