This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocv_resize.go
206 lines (159 loc) · 6.12 KB
/
gocv_resize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"fmt"
"image"
"log"
"os"
"gocv.io/x/gocv"
)
func main() {
// notFoodImgPath := "/Users/kahlil/Pictures/plate-food.jpg"
// foodImgPath := "/Users/kahlil/Documents/AIA AI Food Scoring /Food NotFood testing/Test1 /Images for testing/8453869e-d8ce-4009-8011-8a20b58b2037-large-thumb.jpg"
// plateFoodImgPath := "/Users/kahlil/Documents/AIA AI Food Scoring /Food NotFood testing/S3 rated imgs/1.0/2.jpg"
// s3FoodImgPath := "/Users/kahlil/Documents/AIA AI Food Scoring /Food NotFood testing/S3 rated imgs/1.0/2.jpg"
// NOTE : It is VERY important that the folder(dir) path is given with a trailing slash
// If this is ever going to be anywhere near production then
// we need to find a better way to add the file name to the directory (folder) path
foodImgsFolderPath := os.Args[1]
// filename := s3FoodImgPath
file, openErr := os.Open(foodImgsFolderPath)
if openErr != nil {
log.Fatal(openErr)
}
imgFileNames, readNamesErr := file.Readdirnames(0)
if readNamesErr != nil {
log.Fatal(readNamesErr)
}
fmt.Println(imgFileNames)
imgCount := len(imgFileNames)
fmt.Println("Image count: ", imgCount)
var allImgsPixelsArray [][3][224][224]float32
for idx := 0; idx < imgCount; idx++ {
currentImgFileName := foodImgsFolderPath + (imgFileNames[idx])
// fmt.Printf("Current file name: %v\n", currentImgFileName)
// for each image file, we need to get the file and then create the tensor and pass it to
// our function that accepts the tensor (of all imgs) and loads and runs the model.
img := gocv.IMRead(currentImgFileName, gocv.IMReadColor)
if img.Empty() {
fmt.Printf("Error reading image from: %v\n", currentImgFileName)
return
}
resizedImg := gocv.NewMat()
gocv.Resize(img, &resizedImg, image.Pt(224, 224), 0.0, 0.0, gocv.InterpolationCubic)
imgPixels, errMsg := reshapeImgPixelsForTensor(resizedImg)
if errMsg != nil {
log.Fatal(errMsg)
}
// allImgsPixelsArray[idx] = imgPixels
allImgsPixelsArray = append(allImgsPixelsArray, imgPixels)
}
runInferenceModel(imgFileNames, allImgsPixelsArray)
}
// func getImgFileNamesInFolder (){}
// func readAndResizeImage(){}
// func
func standardizeRed(inputPixelVal float32) (standardizedVal float32) {
return (inputPixelVal - 0.485) / 0.229
}
func standardizeGreen(inputPixelVal float32) (standardizedVal float32) {
return (inputPixelVal - 0.456) / 0.224
}
func standardizeBlue(inputPixelVal float32) (standardizedVal float32) {
return (inputPixelVal - 0.406) / 0.225
}
func reshapeImgPixelsForTensor(resizedImg gocv.Mat) (imgForTensor [3][224][224]float32, err error) {
var allChannelsImgPixels [3][224][224]float32
height := resizedImg.Rows()
// fmt.Println("Rows and Cols : {} {} ", height, width)
step := resizedImg.Step()
imgData := resizedImg.ToBytes()
channels := resizedImg.Channels()
var redPixels [224][224]float32
var greenPixels [224][224]float32
var bluePixels [224][224]float32
for y := 0; y < height; y++ {
// var row [][]uint8
widthCtr := 0
for x := 0; x < step; x = x + channels {
B := standardizeBlue(float32(uint8(imgData[y*step+x])) / 255.0)
G := standardizeGreen(float32(uint8(imgData[y*step+x+1])) / 255.0)
R := standardizeRed(float32(uint8(imgData[y*step+x+2])) / 255.0)
if channels == 4 {
_ = uint8(imgData[y*step+x+3])
}
// row = append(row, {R,G,B} )
// fmt.Println("Pixel values: ", B, G, R)
// fmt.Println("The index vals (y,x)", y, x)
widthCtr++
// fmt.Println("Index val: ", y, widthCtr-1)
redPixels[y][widthCtr-1] = R
greenPixels[y][widthCtr-1] = G
bluePixels[y][widthCtr-1] = B
}
}
allChannelsImgPixels[0] = redPixels
allChannelsImgPixels[1] = greenPixels
allChannelsImgPixels[2] = bluePixels
// To be used for debugging Pixel values of each channel for comparison with Rust & Python
// for ctrVal := 0; ctrVal < 10; ctrVal++ {
// fmt.Println("Red channel first 10 pixels :", redPixels[0][ctrVal])
// }
// fmt.Println()
// for ctrVal := 0; ctrVal < 10; ctrVal++ {
// fmt.Println("Green Channel first 10 pixels :", greenPixels[0][ctrVal])
// }
// fmt.Println()
// for ctrVal := 0; ctrVal < 10; ctrVal++ {
// fmt.Println("Blue Channel first 10 pixels :", bluePixels[0][ctrVal])
// }
return allChannelsImgPixels, nil
// // var a [4]int
// m := int(nrow * ncol)
// imgs[i] = make(RawImage, m)
// m_, err := io.ReadFull(r, imgs[i])
// // fmt.Println("Value of m_ & image inside loop: ", m_, imgs[i])
// currentImageBytes := imgs[i]
// currentImageFloatVals := make([][]float32, m)
// var newFmtImg [][][]float32
// // imgBytesFloat := make([]float64, m)
// // for ctr, element := range currentImageBytes {
// // imgBytesFloat[ctr] = float64(element)
// // }
// // newFmtImgs := mat.NewDense(int(nrow), int(ncol), imgBytesFloat)
// // fmt.Println("New Matrix created is: ", newFmtImgs)
// // newRows, newCols := newFmtImgs.Dims()
// // // Should be 28 x 28
// // fmt.Println("Dimensions of the new matrix are : ", newRows, newCols)
// // Manually create a 28 x 28 nested slice
// for idx := 0; idx < len(currentImageBytes); idx++ {
// floatNum := float32(currentImageBytes[idx]) // can divide by 255 here if reqd by model
// currentImageFloatVals[idx] = []float32{floatNum}
// }
// for i := 0; i+28 <= len(currentImageFloatVals); i += 28 {
// newFmtImg = append(newFmtImg, currentImageFloatVals[i:i+28])
// }
// // fmt.Println("New 28 x 28 nested slice is : ", newFmtImg)
// // fmt.Println("Length of nested slice: ", len(newFmtImg))
// // append(append(emptyRows, newFmtImg...), emptyRows...)
// // fmt.Println("New 32 x 32 with padding Image is : ", withPaddingImg)
// // fmt.Println("Length of padded img is: ", len(withPaddingImg))
// finalFmtImg := withPaddingImg
// // fmt.Println("New 32 x 32 with padding Image is : ", finalFmtImg)
// allFinalFmtImgs[i] = finalFmtImg
// if err != nil {
// return 0, 0, nil, err
// }
// if m_ != int(m) {
// return 0, 0, nil, os.ErrInvalid
// }
}
// TEST : SHOW Resized Image in a window
// window := gocv.NewWindow("Hello")
// for {
// window.IMShow(resizedImg)
// if window.WaitKey(1) >= 0 {
// break
// }
// }
// width := resizedImg.Cols()
// return pixels