galdric/readDataset.go
readDataset.goRaw
1package main
2
3import (
4 "fmt"
5 "io/ioutil"
6 "strconv"
7 "strings"
8)
9
10//each image is [][]float64, is a array of pixels
11type ImgDataset [][][]float64
12
13type Dataset map[string]ImgDataset
14
15func byteArrayToFloat64Array(b []byte) []float64 {
16 var f []float64
17 for i := 0; i < len(b); i++ {
18 val, _ := strconv.ParseFloat(string(b[i]), 64)
19 f = append(f, val)
20 }
21 return f
22}
23
24func readImage(path string) [][]float64 {
25 //open image file
26 dat, err := ioutil.ReadFile(path)
27 check(err)
28
29 pathSplited := strings.Split(path, ".")
30 imageExtension := pathSplited[len(pathSplited)-1]
31 imageRaw, err := dataToImage(dat, imageExtension)
32 check(err)
33
34 //resize the image to standard size
35 image := Resize(imageRaw)
36
37 //convert the image to histogram(RGBA)
38 histogram := imageToHistogram(image)
39 return histogram
40}
41func readDataset(path string) map[string]ImgDataset {
42 dataset := make(Dataset)
43
44 folders, _ := ioutil.ReadDir(path)
45 for _, folder := range folders {
46 fmt.Println(folder.Name())
47
48 var imgDataset ImgDataset
49
50 folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name())
51 for _, file := range folderFiles {
52 image := readImage(path + "/" + folder.Name() + "/" + file.Name())
53
54 imgDataset = append(imgDataset, image)
55
56 }
57
58 //add the foldername to the Dataset map
59 dataset[folder.Name()] = imgDataset
60 }
61
62 return dataset
63}