Browse Source

k neighbours implemented, also adding filters to image to get more comparisons (not finished yet)

master
arnaucode 7 years ago
parent
commit
03f62889e7
6 changed files with 82 additions and 16 deletions
  1. +5
    -0
      imageOperations.go
  2. +34
    -4
      knn.go
  3. +16
    -5
      readDataset.go
  4. +3
    -2
      server.go
  5. +24
    -0
      test.sh
  6. +0
    -5
      tests.sh

+ 5
- 0
imageOperations.go

@ -6,6 +6,7 @@ import (
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"github.com/anthonynsimon/bild/effect"
"github.com/nfnt/resize" "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) r := resize.Resize(uint(config.ImgWidth), uint(config.ImgHeigh), img, resize.Lanczos3)
return r return r
} }
func EdgeDetection(img image.Image) image.Image {
r := effect.EdgeDetection(img, 1.0)
return r
}

+ 34
- 4
knn.go

@ -42,14 +42,43 @@ func isNeighbour(neighbours []Neighbour, dist float64, label string) []Neighbour
return neighbours 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 { func knn(dataset Dataset, input [][]float64) string {
k := 3
k := 10
var neighbours []Neighbour var neighbours []Neighbour
//d := euclideanDist(dataset["leopard"][0], input)
label := getMapKey(dataset)
for i := 0; i < k; i++ { for i := 0; i < k; i++ {
/*neighbours[i].Dist = euclideanDist(dataset["leopard"][0], input) /*neighbours[i].Dist = euclideanDist(dataset["leopard"][0], input)
neighbours[i].Label = "leopard"*/ 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 l, v := range dataset {
for i := 0; i < len(v); i++ { for i := 0; i < len(v); i++ {
@ -66,5 +95,6 @@ func knn(dataset Dataset, input [][]float64) string {
fmt.Println(neighbours[i].Dist) fmt.Println(neighbours[i].Dist)
} }
return neighbours[0].Label
r := averageLabel(neighbours)
return r
} }

+ 16
- 5
readDataset.go

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"image"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
@ -21,7 +22,7 @@ func byteArrayToFloat64Array(b []byte) []float64 {
return f return f
} }
func readImage(path string) [][]float64 {
func readImage(path string) image.Image {
//open image file //open image file
dat, err := ioutil.ReadFile(path) dat, err := ioutil.ReadFile(path)
check(err) check(err)
@ -34,9 +35,12 @@ func readImage(path string) [][]float64 {
//resize the image to standard size //resize the image to standard size
image := Resize(imageRaw) 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 { func readDataset(path string) map[string]ImgDataset {
dataset := make(Dataset) dataset := make(Dataset)
@ -49,9 +53,16 @@ func readDataset(path string) map[string]ImgDataset {
folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
for _, file := range folderFiles { for _, file := range folderFiles {
//get the image as original
image := readImage(path + "/" + folder.Name() + "/" + file.Name()) 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)
} }

+ 3
- 2
server.go

@ -80,8 +80,9 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
//data, err := ioutil.ReadAll(file) //data, err := ioutil.ReadAll(file)
//check(err) //check(err)
img := readImage(handler.Filename) 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) fmt.Fprintln(w, "seems to be a "+result)
} }

+ 24
- 0
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 ""

+ 0
- 5
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

Loading…
Cancel
Save