diff --git a/imageOperations.go b/imageOperations.go index abfb38b..1344c25 100644 --- a/imageOperations.go +++ b/imageOperations.go @@ -6,6 +6,7 @@ import ( "image/jpeg" "image/png" + "github.com/anthonynsimon/bild/effect" "github.com/nfnt/resize" ) @@ -77,3 +78,7 @@ func Resize(img image.Image) image.Image { r := resize.Resize(uint(config.ImgWidth), uint(config.ImgHeigh), img, resize.Lanczos3) return r } +func EdgeDetection(img image.Image) image.Image { + r := effect.EdgeDetection(img, 1.0) + return r +} diff --git a/knn.go b/knn.go index d923892..7789835 100644 --- a/knn.go +++ b/knn.go @@ -42,14 +42,43 @@ func isNeighbour(neighbours []Neighbour, dist float64, label string) []Neighbour return neighbours } +func getMapKey(dataset map[string]ImgDataset) string { + for k, _ := range dataset { + return k + } + return "" +} + +type LabelCount struct { + Label string + Count int +} + +func averageLabel(neighbours []Neighbour) string { + labels := make(map[string]int) + for _, n := range neighbours { + labels[n.Label]++ + } + //create array from map + var a []LabelCount + for k, v := range labels { + a = append(a, LabelCount{k, v}) + } + sort.Slice(a, func(i, j int) bool { + return a[i].Count > a[j].Count + }) + fmt.Println(a) + //send the most appeared neighbour in k + return a[0].Label +} func knn(dataset Dataset, input [][]float64) string { - k := 3 + k := 10 var neighbours []Neighbour - //d := euclideanDist(dataset["leopard"][0], input) + label := getMapKey(dataset) for i := 0; i < k; i++ { /*neighbours[i].Dist = euclideanDist(dataset["leopard"][0], input) neighbours[i].Label = "leopard"*/ - neighbours = append(neighbours, Neighbour{euclideanDist(dataset["leopard"][0], input), "leopard"}) + neighbours = append(neighbours, Neighbour{euclideanDist(dataset[label][0], input), label}) } for l, v := range dataset { for i := 0; i < len(v); i++ { @@ -66,5 +95,6 @@ func knn(dataset Dataset, input [][]float64) string { fmt.Println(neighbours[i].Dist) } - return neighbours[0].Label + r := averageLabel(neighbours) + return r } diff --git a/readDataset.go b/readDataset.go index 9b2c6a8..f66545e 100644 --- a/readDataset.go +++ b/readDataset.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "image" "io/ioutil" "strconv" "strings" @@ -21,7 +22,7 @@ func byteArrayToFloat64Array(b []byte) []float64 { return f } -func readImage(path string) [][]float64 { +func readImage(path string) image.Image { //open image file dat, err := ioutil.ReadFile(path) check(err) @@ -34,9 +35,12 @@ func readImage(path string) [][]float64 { //resize the image to standard size image := Resize(imageRaw) - //convert the image to histogram(RGBA) - histogram := imageToHistogram(image) - return histogram + return image + /* + //convert the image to histogram(RGBA) + histogram := imageToHistogram(image) + return histogram + */ } func readDataset(path string) map[string]ImgDataset { dataset := make(Dataset) @@ -49,9 +53,16 @@ func readDataset(path string) map[string]ImgDataset { folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) for _, file := range folderFiles { + //get the image as original image := readImage(path + "/" + folder.Name() + "/" + file.Name()) + histogram := imageToHistogram(image) - imgDataset = append(imgDataset, image) + //get the image with EdgeDetection filter + imageED := EdgeDetection(image) + histogramED := imageToHistogram(imageED) + + imgDataset = append(imgDataset, histogram) + imgDataset = append(imgDataset, histogramED) } diff --git a/server.go b/server.go index f86f691..be147e1 100644 --- a/server.go +++ b/server.go @@ -80,8 +80,9 @@ func NewImage(w http.ResponseWriter, r *http.Request) { //data, err := ioutil.ReadAll(file) //check(err) img := readImage(handler.Filename) - result := knn(dataset, img) + histogram := imageToHistogram(img) + result := knn(dataset, histogram) - fmt.Println("seems to be a " + result) + c.Purple("seems to be a " + result) fmt.Fprintln(w, "seems to be a "+result) } diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..6be4f47 --- /dev/null +++ b/test.sh @@ -0,0 +1,24 @@ +echo "sending leopard to server" +echo "server response:" +curl -F file=@./leop2.jpg http://127.0.0.1:3055/image +echo "" + +echo "sending leopard to server" +echo "server response:" +curl -F file=@./leopardtest2.jpg http://127.0.0.1:3055/image +echo "" + +echo "sending laptop to server" +echo "server response:" +curl -F file=@./laptoptest.jpg http://127.0.0.1:3055/image +echo "" + +echo "sending leopard to server" +echo "server response:" +curl -F file=@./l1.jpg http://127.0.0.1:3055/image +echo "" + +echo "sending leopard to server" +echo "server response:" +curl -F file=@./l2.jpg http://127.0.0.1:3055/image +echo "" diff --git a/tests.sh b/tests.sh deleted file mode 100644 index c224aed..0000000 --- a/tests.sh +++ /dev/null @@ -1,5 +0,0 @@ -curl -F file=@./leop2.jpg http://127.0.0.1:3055/image - -curl -F file=@./test2.jpg http://127.0.0.1:3055/image - -curl -F file=@./test.jpg http://127.0.0.1:3055/image