arnaucube
arnaucube/galdricPublic Self-hosted repository
small update, commented Grayscale analysis, to avoid needing too RAM, README.md updated
arnaucode idoctnef@openmailbox.org
Thu, 10 Aug 2017 14:46:48 +0200
commit

62da9eee6c31f8bfcb7bfd5726408dfd9ea75b75

parent

c3e167726b8ab51cbb049b761609b59e746a7ee1

5 files changed, 89 insertions(+), 30 deletions(-)

jump to
M README.md → README.md

@@ -2,33 +2,69 @@ # galdric

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 -```+```
M knn.go → 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 @@ //fill the first k neighbours

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) + /* + neighboursG = distNeighboursFromDataset(datasets[2], neighboursG, histogramG) + */ - //neighbours = append(neighbours, neighboursED...) - neighbours = append(neighbours, neighboursG...) + neighbours = append(neighbours, neighboursED...) + /* + neighbours = append(neighbours, neighboursG...) + */ for i := 0; i < len(neighbours); i++ { fmt.Print(neighbours[i].Label + " - ")
M readDataset.go → 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 @@ fmt.Println(folder.Name())

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 @@ imageED := EdgeDetection(image)

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 }
M server.go → 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)
M test.sh → 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 ""