neuralnet.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package neuralnet
  2. import (
  3. "errors"
  4. "os/exec"
  5. "path/filepath"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "imuslab.com/arozos/mod/filesystem"
  10. )
  11. /*
  12. Neural Net Package
  13. Require darknet binary in system folder to work
  14. */
  15. type ImageClass struct {
  16. Name string
  17. Percentage float64
  18. Positions []int
  19. }
  20. func getDarknetBinary() (string, error) {
  21. darknetRoot := "./system/neuralnet/"
  22. binaryName := "darknet_" + runtime.GOOS + "_" + runtime.GOARCH
  23. if runtime.GOOS == "windows" {
  24. binaryName += ".exe"
  25. }
  26. expectedDarknetBinary := filepath.Join(darknetRoot, binaryName)
  27. absPath, _ := filepath.Abs(expectedDarknetBinary)
  28. if !filesystem.FileExists(absPath) {
  29. return "", errors.New("Darknet executable not found on " + absPath)
  30. }
  31. return absPath, nil
  32. }
  33. //Analysis and get what is inside the image using Darknet19, fast but only support 1 main object
  34. func AnalysisPhotoDarknet19(filename string) ([]*ImageClass, error) {
  35. results := []*ImageClass{}
  36. //Check darknet installed
  37. darknetBinary, err := getDarknetBinary()
  38. if err != nil {
  39. return results, err
  40. }
  41. //Check source image exists
  42. imageSourceAbs, err := filepath.Abs(filename)
  43. if !filesystem.FileExists(imageSourceAbs) || err != nil {
  44. return results, errors.New("Source file not found")
  45. }
  46. //Analysis the image
  47. cmd := exec.Command(darknetBinary, "classifier", "predict", "cfg/imagenet1k.data", "cfg/darknet19.cfg", "darknet19.weights", imageSourceAbs)
  48. cmd.Dir = filepath.Dir(darknetBinary)
  49. out, err := cmd.CombinedOutput()
  50. if err != nil {
  51. return results, err
  52. }
  53. //Process the output text
  54. lines := strings.Split(string(out), "\n")
  55. for _, line := range lines {
  56. if strings.Contains(line, "%:") {
  57. //This is a resulting line. Split and push it into results
  58. info := strings.Split(strings.TrimSpace(line), "%: ") //[0] => Percentage in string, [1] => tag
  59. if s, err := strconv.ParseFloat(info[0], 32); err == nil {
  60. thisClassification := ImageClass{
  61. Name: info[1],
  62. Percentage: s,
  63. Positions: []int{},
  64. }
  65. results = append(results, &thisClassification)
  66. }
  67. }
  68. }
  69. return results, nil
  70. }
  71. //Analysis what is in the image using YOLO3, very slow but support multiple objects
  72. func AnalysisPhotoYOLO3(filename string) ([]*ImageClass, error) {
  73. results := []*ImageClass{}
  74. //Check darknet installed
  75. darknetBinary, err := getDarknetBinary()
  76. if err != nil {
  77. return results, err
  78. }
  79. //Check source image exists
  80. imageSourceAbs, err := filepath.Abs(filename)
  81. if !filesystem.FileExists(imageSourceAbs) || err != nil {
  82. return results, errors.New("Source file not found")
  83. }
  84. //Analysis the image
  85. cmd := exec.Command(darknetBinary, "detect", "cfg/yolov3.cfg", "yolov3.weights", imageSourceAbs, "-out")
  86. cmd.Dir = filepath.Dir(darknetBinary)
  87. out, err := cmd.CombinedOutput()
  88. if err != nil {
  89. return results, err
  90. }
  91. lines := strings.Split(string(out), "\n")
  92. var previousClassificationObject *ImageClass = nil
  93. for _, line := range lines {
  94. line = strings.TrimSpace(line)
  95. if len(line) > 0 && line[len(line)-1:] == "%" && strings.Contains(line, ":") {
  96. //This is a output value
  97. //Trim out the %
  98. line = line[:len(line)-1]
  99. info := strings.Split(line, ":") //[0] => class name, [1] => percentage
  100. if s, err := strconv.ParseFloat(strings.TrimSpace(info[1]), 32); err == nil {
  101. thisClassification := ImageClass{
  102. Name: info[0],
  103. Percentage: s,
  104. }
  105. previousClassificationObject = &thisClassification
  106. results = append(results, &thisClassification)
  107. }
  108. } else if len(line) > 0 && line[:4] == "pos=" && strings.Contains(line, ",") && previousClassificationObject != nil {
  109. //This is position makeup data, append to previous classification
  110. positionsString := strings.Split(line[4:], ",")
  111. positionsInt := []int{}
  112. for _, pos := range positionsString {
  113. posInt, err := strconv.Atoi(pos)
  114. if err != nil {
  115. positionsInt = append(positionsInt, -1)
  116. } else {
  117. positionsInt = append(positionsInt, posInt)
  118. }
  119. }
  120. previousClassificationObject.Positions = positionsInt
  121. }
  122. }
  123. return results, nil
  124. }