You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.3 KiB

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