2017-09-30 3 views
-1

J'essaye d'analyser un grand ensemble de données d'image. J'utilise filepath.Walk] et le traitement de chaque fichier que je trouve là. Je voudrais le chemin de fichier.Comment exécuter filepath.Walkfunc en tant que goroutine

package main 

import (
    "fmt" 
    "image/color" 
    "image/png" 
    "math/rand" 
    "os" 
) 

var (
    Black = color.Gray{0} 
) 

func getRandFloatNumber(min, max float32) float32 { 
    return (rand.Float32()*2 - min) * max 
} 

func openImage(path string, info os.FileInfo, err error) error { 
    infile, _ := os.Open(path) 
    defer infile.Close() 
    img, err := png.Decode(infile) 
    if err != nil { 
     return nil 
    } 

    array := make([]float32, 128*128) 
    for y := 0; y < 128; y++ { 
     for x := 0; x < 128; x++ { 
      c := color.GrayModel.Convert(img.At(x, y)).(color.Gray) 
      if c == Black { 
       array[x*y] = getRandFloatNumber(0.7, 0.95) 
      } else { 
       array[x*y] = getRandFloatNumber(0.1, 0.25) 
      } 
     } 
    } 

    fmt.Println(info.Name()) 

    return nil 
} 

Comment exécuter openImage en tant que gorutine? Ou comment optimiser ce code?

+0

'go openImage()' –

Répondre

0

Vous ne pouvez pas obtenir filepath.Walk pour appeler votre fonction dans un goroutine, mais vous pouvez simplement démarrer un goroutine dans votre WalkFunc.

package main 

import (
    "os" 
    "path/filepath" 
) 

func main() { 
    filepath.Walk("/my/dir", func(path string, info os.FileInfo, err error) error { 
      if err != nil { 
        return err 
      } 

      if info.IsDir() { 
        return nil 
      } 

      // Check more criteria if necessary. Also consider limiting the number 
      // of concurrent goroutines. 

      go openImage(path, info) 

      return nil 
    }) 
} 

func openImage(path string, info os.FileInfo) { 
}