mirror of
https://github.com/arnaucube/galdric.git
synced 2026-02-06 18:56:45 +01:00
small update, commented Grayscale analysis, to avoid needing too RAM, README.md updated
This commit is contained in:
56
README.md
56
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
|
||||
```
|
||||
```
|
||||
|
||||
26
knn.go
26
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)
|
||||
/*
|
||||
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 + " - ")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
26
test.sh
26
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 leopard to server"
|
||||
echo "server response:"
|
||||
curl -F file=@./testleopard3.png http://127.0.0.1:3055/image
|
||||
echo ""
|
||||
|
||||
echo "sending leopard to server"
|
||||
echo "server response:"
|
||||
curl -F file=@./testleopard4.png 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
|
||||
curl -F file=@./testlaptop1.png http://127.0.0.1:3055/image
|
||||
echo ""
|
||||
|
||||
Reference in New Issue
Block a user