arnaucube
arnaucube/galdricPublic Self-hosted repository
k neighbours implemented, also adding filters to image to get more comparisons (not finished yet)
arnaucode idoctnef@openmailbox.org
Fri, 30 Jun 2017 12:41:02 +0200
commit

03f62889e757a981c80fba0c638da2635dd7cea3

parent

c783acc32fed359d6f99fb37922b1f5729cce65d

6 files changed, 82 insertions(+), 16 deletions(-)

jump to
M imageOperations.go → imageOperations.go

@@ -6,6 +6,7 @@ "image"

"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 +}
M knn.go → knn.go

@@ -42,14 +42,43 @@

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 @@ fmt.Print(neighbours[i].Label + " - ")

fmt.Println(neighbours[i].Dist) } - return neighbours[0].Label + r := averageLabel(neighbours) + return r }
M readDataset.go → readDataset.go

@@ -2,6 +2,7 @@ package main

import ( "fmt" + "image" "io/ioutil" "strconv" "strings"

@@ -21,7 +22,7 @@ }

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 @@

//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 @@ var imgDataset 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) }
M server.go → server.go

@@ -80,8 +80,9 @@

//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) }
A 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 ""
D tests.sh

@@ -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