-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
249 lines (204 loc) · 6.56 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"flag"
"image"
"image/color"
"log"
"github.com/LdDl/goparking/framedata"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/LdDl/goparking/parklot"
"github.com/LdDl/goparking/pkgs/fifo"
"github.com/LdDl/goparking/pkgs/inits"
"gocv.io/x/gocv"
)
var (
// Atomic variables for grabbing and processing functions
boolGrab atomic.Value
boolProc atomic.Value
// FIFO Queue
fifoFrames *fifo.FIFOQueue
wg sync.WaitGroup
initialParameters inits.InitParams
boolFirstFrame = true
counter = 0
)
// Grabber - считывание кадров
func Grabber() {
boolProc.Store(true)
var webcam *gocv.VideoCapture
var err error
switch initialParameters.PJSON.VideoType {
case "url":
webcam, err = gocv.VideoCaptureFile(initialParameters.PJSON.VideoSource)
if err != nil {
log.Printf("Error opening video capture url: %v\n", initialParameters.PJSON.VideoSource)
log.Fatalln(err)
return
}
break
case "device":
deviceIndex, err := strconv.Atoi(initialParameters.PJSON.VideoSource)
if err != nil {
log.Fatalln(err)
return
}
webcam, err = gocv.VideoCaptureDevice(deviceIndex)
if err != nil {
log.Printf("Error opening video device index: %v\n", deviceIndex)
log.Fatalln(err)
return
}
break
}
defer webcam.Close()
var singleFrame framedata.FrameData
singleFrame.FrameMatTrue = gocv.NewMat()
defer singleFrame.FrameMatTrue.Close()
singleFrame.FrameMat = gocv.NewMat()
defer singleFrame.FrameMat.Close()
singleFrame.FrameCounter = 0
// Read fisrt frame and recalculate parameters depending on image scale
if ok := webcam.Read(&singleFrame.FrameMatTrue); !ok {
// log.Printf("Error cannot read URL or device: %v\n", initialParameters.PJSON.VideoSource)
log.Fatalf("Error cannot read URL or device: %v\n", initialParameters.PJSON.VideoSource)
return
}
singleFrame.FrameMatScale = singleFrame.FrameMatTrue.Cols() / initialParameters.PJSON.ImageResizing[0]
for i := range initialParameters.ParkingLots {
var tmp parklot.Lot
tmp.SetID(initialParameters.ParkingLots[i].GetID())
points := initialParameters.ParkingLots[i].GetContourPoints()[0]
for j := range points {
points[j].X /= singleFrame.FrameMatScale
points[j].Y /= singleFrame.FrameMatScale
}
tmp.SetContourPoints(points)
tmp.CalcBoundingRect()
initialParameters.ParkingLots[i] = tmp
}
for boolProc.Load() == true {
singleFrame.FrameCounter++
if ok := webcam.Read(&singleFrame.FrameMatTrue); !ok {
// log.Printf("Error cannot read URL or device: %v\n", initialParameters.PJSON.VideoSource)
log.Fatalf("Error cannot read URL or device: %v\n", initialParameters.PJSON.VideoSource)
return
}
if singleFrame.FrameMatTrue.Empty() {
continue
}
// Resize input image
gocv.Resize(singleFrame.FrameMatTrue, &singleFrame.FrameMat, image.Point{initialParameters.PJSON.ImageResizing[0], initialParameters.PJSON.ImageResizing[1]}, 0.0, 0.0, gocv.InterpolationDefault)
singleFrame.Buf, err = gocv.IMEncode(".bmp", singleFrame.FrameMat)
if err != nil {
// Do not handle error
}
// Push image to queue
fifoFrames.Push(singleFrame)
time.Sleep(10 * time.Millisecond)
}
boolProc.Store(false)
wg.Done()
}
// Processing - отправка на обработку кадров видеопотока
func Processing() {
// Defer exits
var singleFrame framedata.FrameData
for boolProc.Load() == true {
var interFrame interface{}
var ok bool
// Pop input image from queue
interFrame = fifoFrames.Pop()
if _, ok = interFrame.(framedata.FrameData); ok {
singleFrame = interFrame.(framedata.FrameData)
} else {
continue
}
ProcessingData(&singleFrame)
}
wg.Done()
}
// ProcessingData - обработка кадров видеопотока
func ProcessingData(f *framedata.FrameData) {
var imgCopy framedata.FrameData
imgCopy = (*f).Clone()
defer imgCopy.FrameMat.Close()
defer imgCopy.FrameMatTrue.Close()
var imgGray, imgBlur, roi, laplacian, delta gocv.Mat
imgGray = gocv.NewMat()
imgBlur = gocv.NewMat()
roi = gocv.NewMat()
laplacian = gocv.NewMat()
delta = gocv.NewMatFromScalar(gocv.Scalar{Val1: 0, Val2: 0, Val3: 0, Val4: 0}, gocv.MatTypeCV64F)
defer imgGray.Close()
defer imgBlur.Close()
defer roi.Close()
defer laplacian.Close()
defer delta.Close()
gocv.CvtColor(imgCopy.FrameMat, &imgGray, gocv.ColorBGRToGray)
gocv.GaussianBlur(imgGray, &imgBlur, image.Point{5, 5}, 3, 3, gocv.BorderDefault)
for i := range initialParameters.ParkingLots {
roi = imgBlur.Region(initialParameters.ParkingLots[i].GetBoundingRect())
gocv.Laplacian(roi, &laplacian, gocv.MatTypeCV64F, 1, 1, 0, gocv.BorderDefault)
laplAbs := gocv.NewMat()
defer laplAbs.Close()
emptyScalar := gocv.NewMatFromScalar(gocv.Scalar{Val1: 0, Val2: 0, Val3: 0, Val4: 0}, gocv.MatTypeCV64F)
defer emptyScalar.Close()
gocv.AbsDiff(laplacian, emptyScalar, &laplAbs) // alternative to cv::Abs(Mat)
mask := initialParameters.ParkingLots[i].GetMask().Clone()
/*START Alternative to cv::mean(laplAbs, mask); START*/
nonZeroesMask := float64(gocv.CountNonZero(mask))
meanMask := 0.0
for h := 0; h < mask.Rows(); h++ {
for g := 0; g < mask.Cols(); g++ {
if mask.GetUCharAt(h, g) > 0 {
meanMask += laplAbs.GetDoubleAt(h, g)
}
}
}
meanMask /= nonZeroesMask
/*END Alternative to cv::mean(laplAbs, mask); END*/
if meanMask > initialParameters.PJSON.Laplacian {
log.Printf("Parking lot with id: %v is occupied", initialParameters.ParkingLots[i].GetID())
gocv.DrawContours(&imgCopy.FrameMat, initialParameters.ParkingLots[i].GetContourPoints(), -1, color.RGBA{255, 0, 0, 0}, 1)
} else {
gocv.DrawContours(&imgCopy.FrameMat, initialParameters.ParkingLots[i].GetContourPoints(), -1, color.RGBA{0, 255, 0, 0}, 1)
}
}
boolFirstFrame = false
if initialParameters.ShowIm {
initialParameters.GlobaWindow.IMShow(imgCopy.FrameMat)
if initialParameters.GlobaWindow.WaitKey(1) >= 0 {
return
}
}
}
func main() {
log.Println("Starting program...")
var err error
cfgName := flag.String("cfg", "go_ip.json", "Config file path")
flag.Parse()
err = initialParameters.SetParams(*cfgName)
if err != nil {
log.Println(err)
return
}
if initialParameters.ShowIm {
initialParameters.GlobaWindow = gocv.NewWindow("Input Video")
initialParameters.GlobaWindow.ResizeWindow(initialParameters.PJSON.ImageResizing[0], initialParameters.PJSON.ImageResizing[1])
defer initialParameters.GlobaWindow.Close()
}
fifoFrames = fifo.NewQueue(60)
boolGrab.Store(true)
boolProc.Store(true)
wg.Add(1)
go Grabber()
wg.Add(1)
go Processing()
wg.Wait()
boolGrab.Store(false)
boolProc.Store(false)
log.Println("Done!")
}