| 121 | } |
| 122 | |
| 123 | func printBestLabel(probabilities []float32, labelsFile string) { |
| 124 | bestIdx := 0 |
| 125 | for i, p := range probabilities { |
| 126 | if p > probabilities[bestIdx] { |
| 127 | bestIdx = i |
| 128 | } |
| 129 | } |
| 130 | // Found the best match. Read the string from labelsFile, which |
| 131 | // contains one line per label. |
| 132 | file, err := os.Open(labelsFile) |
| 133 | if err != nil { |
| 134 | log.Fatal(err) |
| 135 | } |
| 136 | defer file.Close() |
| 137 | scanner := bufio.NewScanner(file) |
| 138 | var labels []string |
| 139 | for scanner.Scan() { |
| 140 | labels = append(labels, scanner.Text()) |
| 141 | } |
| 142 | if err := scanner.Err(); err != nil { |
| 143 | log.Printf("ERROR: failed to read %s: %v", labelsFile, err) |
| 144 | } |
| 145 | fmt.Printf("BEST MATCH: (%2.0f%% likely) %s\n", probabilities[bestIdx]*100.0, labels[bestIdx]) |
| 146 | } |
| 147 | |
| 148 | // Convert the image in filename to a Tensor suitable as input to the Inception model. |
| 149 | func makeTensorFromImage(filename string) (*tf.Tensor, error) { |