labelImage.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "bufio"
  17. "bytes"
  18. "fmt"
  19. "image"
  20. _ "image/jpeg"
  21. _ "image/png"
  22. "os"
  23. "path"
  24. "sort"
  25. "sync"
  26. tflite "github.com/mattn/go-tflite" //nolint:typecheck
  27. "github.com/nfnt/resize"
  28. "github.com/lf-edge/ekuiper/pkg/api"
  29. )
  30. type labelImage struct {
  31. modelPath string
  32. labelPath string
  33. once sync.Once
  34. interpreter *tflite.Interpreter
  35. labels []string
  36. }
  37. func (f *labelImage) Validate(args []interface{}) error {
  38. if len(args) != 1 {
  39. return fmt.Errorf("labelImage function only supports 1 parameter but got %d", len(args))
  40. }
  41. return nil
  42. }
  43. func (f *labelImage) Exec(args []interface{}, ctx api.FunctionContext) (interface{}, bool) {
  44. arg0, ok := args[0].([]byte)
  45. if !ok {
  46. return fmt.Errorf("labelImage function parameter must be a bytea, but got %[1]T(%[1]v)", args[0]), false
  47. }
  48. img, _, err := image.Decode(bytes.NewReader(arg0))
  49. if err != nil {
  50. return err, false
  51. }
  52. var outerErr error
  53. f.once.Do(func() {
  54. ploc := path.Join(ctx.GetRootPath(), "data", "functions")
  55. f.labels, err = loadLabels(path.Join(ploc, f.labelPath))
  56. if err != nil {
  57. outerErr = fmt.Errorf("fail to load labels: %s", err)
  58. return
  59. }
  60. model := tflite.NewModelFromFile(path.Join(ploc, f.modelPath))
  61. if model == nil {
  62. outerErr = fmt.Errorf("fail to load model: %s", err)
  63. return
  64. }
  65. defer model.Delete()
  66. options := tflite.NewInterpreterOptions()
  67. options.SetNumThread(4)
  68. options.SetErrorReporter(func(msg string, user_data interface{}) {
  69. fmt.Println(msg)
  70. }, nil)
  71. defer options.Delete()
  72. interpreter := tflite.NewInterpreter(model, options)
  73. if interpreter == nil {
  74. outerErr = fmt.Errorf("cannot create interpreter")
  75. return
  76. }
  77. status := interpreter.AllocateTensors()
  78. if status != tflite.OK {
  79. outerErr = fmt.Errorf("allocate failed")
  80. interpreter.Delete()
  81. return
  82. }
  83. f.interpreter = interpreter
  84. // TODO If created, the interpreter will be kept through the whole life of kuiper. Refactor this later.
  85. // defer interpreter.Delete()
  86. })
  87. if f.interpreter == nil {
  88. return fmt.Errorf("fail to load model %s %s", f.modelPath, outerErr), false
  89. }
  90. input := f.interpreter.GetInputTensor(0)
  91. wantedHeight := input.Dim(1)
  92. wantedWidth := input.Dim(2)
  93. wantedChannels := input.Dim(3)
  94. wantedType := input.Type()
  95. resized := resize.Resize(uint(wantedWidth), uint(wantedHeight), img, resize.NearestNeighbor)
  96. bounds := resized.Bounds()
  97. dx, dy := bounds.Dx(), bounds.Dy()
  98. if wantedType == tflite.UInt8 {
  99. bb := make([]byte, dx*dy*wantedChannels)
  100. for y := 0; y < dy; y++ {
  101. for x := 0; x < dx; x++ {
  102. col := resized.At(x, y)
  103. r, g, b, _ := col.RGBA()
  104. bb[(y*dx+x)*3+0] = byte(float64(r) / 255.0)
  105. bb[(y*dx+x)*3+1] = byte(float64(g) / 255.0)
  106. bb[(y*dx+x)*3+2] = byte(float64(b) / 255.0)
  107. }
  108. }
  109. input.CopyFromBuffer(bb)
  110. } else {
  111. return fmt.Errorf("is not wanted type"), false
  112. }
  113. status := f.interpreter.Invoke()
  114. if status != tflite.OK {
  115. return fmt.Errorf("invoke failed"), false
  116. }
  117. output := f.interpreter.GetOutputTensor(0)
  118. outputSize := output.Dim(output.NumDims() - 1)
  119. b := make([]byte, outputSize)
  120. type result struct {
  121. score float64
  122. index int
  123. }
  124. status = output.CopyToBuffer(&b[0])
  125. if status != tflite.OK {
  126. return fmt.Errorf("output failed"), false
  127. }
  128. var results []result
  129. for i := 0; i < outputSize; i++ {
  130. score := float64(b[i]) / 255.0
  131. if score < 0.2 {
  132. continue
  133. }
  134. results = append(results, result{score: score, index: i})
  135. }
  136. sort.Slice(results, func(i, j int) bool {
  137. return results[i].score > results[j].score
  138. })
  139. // output is the biggest score labelImage
  140. if len(results) > 0 {
  141. return f.labels[results[0].index], true
  142. } else {
  143. return "", true
  144. }
  145. }
  146. func (f *labelImage) IsAggregate() bool {
  147. return false
  148. }
  149. func loadLabels(filename string) ([]string, error) {
  150. labels := []string{}
  151. f, err := os.Open(filename)
  152. if err != nil {
  153. return nil, err
  154. }
  155. defer f.Close()
  156. scanner := bufio.NewScanner(f)
  157. for scanner.Scan() {
  158. labels = append(labels, scanner.Text())
  159. }
  160. return labels, nil
  161. }
  162. var LabelImage = labelImage{
  163. modelPath: "labelImage/mobilenet_quant_v1_224.tflite",
  164. labelPath: "labelImage/labels.txt",
  165. }