Browse Source

small update, commented Grayscale analysis, to avoid needing too RAM, README.md updated

master
arnaucode 7 years ago
parent
commit
62da9eee6c
5 changed files with 90 additions and 31 deletions
  1. +44
    -8
      README.md
  2. +20
    -8
      knn.go
  3. +18
    -8
      readDataset.go
  4. +1
    -0
      server.go
  5. +7
    -7
      test.sh

+ 44
- 8
README.md

@ -2,33 +2,69 @@
machine learning server, for image classification machine learning server, for image classification
- Reads all the datasets in the folder /dataset - Reads all the datasets in the folder /dataset
- Each image is resized to the same size, configured in the config.json
- Runs a server that allows to upload images to classify
- Accepts PNG, JPG, JPEG
- Each image is resized to the same size and converted to PNG type, configured in the config.json
- For the input images, calculates the euclidean distances - For the input images, calculates the euclidean distances
- Gets the nearest neighbour
- Show the result, that is the label of the object in the image
- Applyies KNN (K-Nearest Neighbours algorithm) to classify the images
- Server returns the classification result, that is the label of the object in the image
### Instructions
Put dataset in /dataset directory with subdirectories, where each subdirectory contains images of one element.
For example:
```
dataset/
leopard/
img01.png
img02.png
img03.png
...
laptop/
img01.png
img02.png
...
camera/
img01.png
img02.png
...
```
So, we have each image and to which element category is (the name of subdirectory).
Then, run the server:
```
>./galdric
```
Now, just need to perform petitions with new images, to get the response from the server classifying them:
```bash
curl -F file=@./testimage.png http://127.0.0.1:3055/image
```
And the server will return:
```
seems to be a leopard
```
Can perform some tests with the test.sh file:
```
bash test.sh
```
------------- -------------
#### Useful commands
send file over ssh: send file over ssh:
``` ```
scp dataset.tar.gz root@51.255.193.106:/root/galdric
scp dataset.tar.gz root@SERVERIP:/root/galdric
``` ```
on the server, untar file: on the server, untar file:
``` ```
tar -xvzf dataset.tar.gz tar -xvzf dataset.tar.gz
```
```

+ 20
- 8
knn.go

@ -87,14 +87,20 @@ func knn(datasets []Dataset, imgInput image.Image) string {
k := 6 k := 6
var neighbours []Neighbour var neighbours []Neighbour
var neighboursED []Neighbour var neighboursED []Neighbour
var neighboursG []Neighbour
/*
var neighboursG []Neighbour
*/
imgED := EdgeDetection(imgInput) imgED := EdgeDetection(imgInput)
imgG := Grayscale(imgInput)
/*
imgG := Grayscale(imgInput)
*/
histogram := imageToHistogram(imgInput) histogram := imageToHistogram(imgInput)
histogramED := imageToHistogram(imgED) histogramED := imageToHistogram(imgED)
histogramG := imageToHistogram(imgG)
/*
histogramG := imageToHistogram(imgG)
*/
//get a key from map dataset, the key is a label //get a key from map dataset, the key is a label
label := getMapKey(datasets[0]) label := getMapKey(datasets[0])
@ -102,15 +108,21 @@ func knn(datasets []Dataset, imgInput image.Image) string {
for i := 0; i < k; i++ { for i := 0; i < k; i++ {
neighbours = append(neighbours, Neighbour{euclideanDist(datasets[0][label][0], histogram), label}) neighbours = append(neighbours, Neighbour{euclideanDist(datasets[0][label][0], histogram), label})
neighboursED = append(neighboursED, Neighbour{euclideanDist(datasets[1][label][0], histogramED), label}) neighboursED = append(neighboursED, Neighbour{euclideanDist(datasets[1][label][0], histogramED), label})
neighboursG = append(neighboursG, Neighbour{euclideanDist(datasets[2][label][0], histogramG), label})
/*
neighboursG = append(neighboursG, Neighbour{euclideanDist(datasets[2][label][0], histogramG), label})
*/
} }
neighbours = distNeighboursFromDataset(datasets[0], neighbours, histogram) neighbours = distNeighboursFromDataset(datasets[0], neighbours, histogram)
neighboursED = distNeighboursFromDataset(datasets[1], neighboursED, histogramED) neighboursED = distNeighboursFromDataset(datasets[1], neighboursED, histogramED)
neighboursG = distNeighboursFromDataset(datasets[2], neighboursG, histogramG)
//neighbours = append(neighbours, neighboursED...)
neighbours = append(neighbours, neighboursG...)
/*
neighboursG = distNeighboursFromDataset(datasets[2], neighboursG, histogramG)
*/
neighbours = append(neighbours, neighboursED...)
/*
neighbours = append(neighbours, neighboursG...)
*/
for i := 0; i < len(neighbours); i++ { for i := 0; i < len(neighbours); i++ {
fmt.Print(neighbours[i].Label + " - ") fmt.Print(neighbours[i].Label + " - ")

+ 18
- 8
readDataset.go

@ -46,7 +46,9 @@ func readDataset(path string) []Dataset {
var resultDatasets []Dataset var resultDatasets []Dataset
dataset := make(Dataset) dataset := make(Dataset)
datasetED := make(Dataset) datasetED := make(Dataset)
datasetG := make(Dataset)
/*
datasetG := make(Dataset)
*/
folders, _ := ioutil.ReadDir(path) folders, _ := ioutil.ReadDir(path)
for _, folder := range folders { for _, folder := range folders {
@ -54,7 +56,9 @@ func readDataset(path string) []Dataset {
var imgDataset ImgDataset var imgDataset ImgDataset
var imgDatasetED ImgDataset var imgDatasetED ImgDataset
var imgDatasetG ImgDataset
/*
var imgDatasetG ImgDataset
*/
folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
for _, file := range folderFiles { for _, file := range folderFiles {
@ -68,20 +72,26 @@ func readDataset(path string) []Dataset {
histogramED := imageToHistogram(imageED) histogramED := imageToHistogram(imageED)
imgDatasetED = append(imgDatasetED, histogramED) imgDatasetED = append(imgDatasetED, histogramED)
//get the image with Grayscale filter
imageG := Grayscale(image)
histogramG := imageToHistogram(imageG)
imgDatasetG = append(imgDatasetG, histogramG)
/*
//get the image with Grayscale filter
imageG := Grayscale(image)
histogramG := imageToHistogram(imageG)
imgDatasetG = append(imgDatasetG, histogramG)
*/
} }
//add the foldername to the Dataset map //add the foldername to the Dataset map
dataset[folder.Name()] = imgDataset dataset[folder.Name()] = imgDataset
datasetED[folder.Name()] = imgDatasetED datasetED[folder.Name()] = imgDatasetED
datasetG[folder.Name()] = imgDatasetG
/*
datasetG[folder.Name()] = imgDatasetG
*/
} }
resultDatasets = append(resultDatasets, dataset) resultDatasets = append(resultDatasets, dataset)
resultDatasets = append(resultDatasets, datasetED) resultDatasets = append(resultDatasets, datasetED)
resultDatasets = append(resultDatasets, datasetG)
/*
resultDatasets = append(resultDatasets, datasetG)
*/
return resultDatasets return resultDatasets
} }

+ 1
- 0
server.go

@ -74,6 +74,7 @@ func NewImage(w http.ResponseWriter, r *http.Request) {
_, handler, err := r.FormFile("file") _, handler, err := r.FormFile("file")
check(err) check(err)
fmt.Println(handler.Filename)
img := readImage(handler.Filename) img := readImage(handler.Filename)
//histogram := imageToHistogram(img) //histogram := imageToHistogram(img)
result := knn(datasets, img) result := knn(datasets, img)

+ 7
- 7
test.sh

@ -1,24 +1,24 @@
echo "sending leopard to server" echo "sending leopard to server"
echo "server response:" echo "server response:"
curl -F file=@./leop2.jpg http://127.0.0.1:3055/image
curl -F file=@./testleopard1.png http://127.0.0.1:3055/image
echo "" echo ""
echo "sending leopard to server" echo "sending leopard to server"
echo "server response:" echo "server response:"
curl -F file=@./leopardtest2.jpg http://127.0.0.1:3055/image
curl -F file=@./testleopard2.png http://127.0.0.1:3055/image
echo "" echo ""
echo "sending laptop to server"
echo "sending leopard to server"
echo "server response:" echo "server response:"
curl -F file=@./laptoptest.jpg http://127.0.0.1:3055/image
curl -F file=@./testleopard3.png http://127.0.0.1:3055/image
echo "" echo ""
echo "sending leopard to server" echo "sending leopard to server"
echo "server response:" echo "server response:"
curl -F file=@./l1.jpg http://127.0.0.1:3055/image
curl -F file=@./testleopard4.png http://127.0.0.1:3055/image
echo "" echo ""
echo "sending leopard to server"
echo "sending laptop to server"
echo "server response:" echo "server response:"
curl -F file=@./l2.jpg http://127.0.0.1:3055/image
curl -F file=@./testlaptop1.png http://127.0.0.1:3055/image
echo "" echo ""

Loading…
Cancel
Save