mirror of
https://github.com/arnaucube/galdric.git
synced 2026-02-07 03:06:45 +01:00
k neighbours implemented, also adding filters to image to get more comparisons (not finished yet)
This commit is contained in:
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
38
knn.go
38
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
|
return image
|
||||||
|
/*
|
||||||
//convert the image to histogram(RGBA)
|
//convert the image to histogram(RGBA)
|
||||||
histogram := imageToHistogram(image)
|
histogram := imageToHistogram(image)
|
||||||
return histogram
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
test.sh
Normal file
24
test.sh
Normal file
@@ -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 ""
|
||||||
Reference in New Issue
Block a user