From 62da9eee6c31f8bfcb7bfd5726408dfd9ea75b75 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Thu, 10 Aug 2017 14:46:48 +0200 Subject: [PATCH] small update, commented Grayscale analysis, to avoid needing too RAM, README.md updated --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++-------- knn.go | 28 +++++++++++++++++++-------- readDataset.go | 26 +++++++++++++++++-------- server.go | 1 + test.sh | 14 +++++++------- 5 files changed, 90 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index b63d02e..a3e090b 100644 --- a/README.md +++ b/README.md @@ -2,33 +2,69 @@ machine learning server, for image classification - - 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 - - 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: ``` -scp dataset.tar.gz root@51.255.193.106:/root/galdric +scp dataset.tar.gz root@SERVERIP:/root/galdric ``` on the server, untar file: ``` tar -xvzf dataset.tar.gz -``` \ No newline at end of file +``` diff --git a/knn.go b/knn.go index 819ea3b..dd8159f 100644 --- a/knn.go +++ b/knn.go @@ -87,14 +87,20 @@ func knn(datasets []Dataset, imgInput image.Image) string { k := 6 var neighbours []Neighbour var neighboursED []Neighbour - var neighboursG []Neighbour + /* + var neighboursG []Neighbour + */ imgED := EdgeDetection(imgInput) - imgG := Grayscale(imgInput) + /* + imgG := Grayscale(imgInput) + */ histogram := imageToHistogram(imgInput) histogramED := imageToHistogram(imgED) - histogramG := imageToHistogram(imgG) + /* + histogramG := imageToHistogram(imgG) + */ //get a key from map dataset, the key is a label label := getMapKey(datasets[0]) @@ -102,15 +108,21 @@ func knn(datasets []Dataset, imgInput image.Image) string { for i := 0; i < k; i++ { neighbours = append(neighbours, Neighbour{euclideanDist(datasets[0][label][0], histogram), 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) 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++ { fmt.Print(neighbours[i].Label + " - ") diff --git a/readDataset.go b/readDataset.go index 598e710..5cc1335 100644 --- a/readDataset.go +++ b/readDataset.go @@ -46,7 +46,9 @@ func readDataset(path string) []Dataset { var resultDatasets []Dataset dataset := make(Dataset) datasetED := make(Dataset) - datasetG := make(Dataset) + /* + datasetG := make(Dataset) + */ folders, _ := ioutil.ReadDir(path) for _, folder := range folders { @@ -54,7 +56,9 @@ func readDataset(path string) []Dataset { var imgDataset ImgDataset var imgDatasetED ImgDataset - var imgDatasetG ImgDataset + /* + var imgDatasetG ImgDataset + */ folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) for _, file := range folderFiles { @@ -68,20 +72,26 @@ func readDataset(path string) []Dataset { histogramED := imageToHistogram(imageED) 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 dataset[folder.Name()] = imgDataset datasetED[folder.Name()] = imgDatasetED - datasetG[folder.Name()] = imgDatasetG + /* + datasetG[folder.Name()] = imgDatasetG + */ } resultDatasets = append(resultDatasets, dataset) resultDatasets = append(resultDatasets, datasetED) - resultDatasets = append(resultDatasets, datasetG) + /* + resultDatasets = append(resultDatasets, datasetG) + */ return resultDatasets } diff --git a/server.go b/server.go index 015eee8..2de0cb2 100644 --- a/server.go +++ b/server.go @@ -74,6 +74,7 @@ func NewImage(w http.ResponseWriter, r *http.Request) { _, handler, err := r.FormFile("file") check(err) + fmt.Println(handler.Filename) img := readImage(handler.Filename) //histogram := imageToHistogram(img) result := knn(datasets, img) diff --git a/test.sh b/test.sh index 6be4f47..a20f9f3 100644 --- a/test.sh +++ b/test.sh @@ -1,24 +1,24 @@ echo "sending leopard to server" 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 "sending leopard to server" 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 "sending laptop to server" +echo "sending leopard to server" 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 "sending leopard to server" 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 "sending leopard to server" +echo "sending laptop to server" 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 ""