-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy path_capture_init.py
executable file
·649 lines (511 loc) · 26.3 KB
/
_capture_init.py
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
import cv2
import time
import datetime
import os
import numpy as np
import image_processing as improc
import math_operation as mo
import _vehicle_init as vehicleInit
import _trajectory_init as trajectoryInit
import shadow_removal as sr
from PyQt4 import QtGui, QtCore
from PyQt4 import uic
from cv2 import ocl
from munkres import Munkres
ocl.setUseOpenCL(False) # set flag OCL to False if you build OPENCV -D WITH_OPENCL=ON
class QtCapture:
def setVideoMode(self, video_mode):
self.video_mode = video_mode
def getVideoMode(self):
return self.video_mode
def setVideoOutput(self, video_output):
self.video_output = video_output
def getVideoOutput(self):
return self.video_output
def setBackgroundSubtraction(self, backgroundSubtraction):
self.backgroundSubtracion = backgroundSubtraction
def getBackgroundSubtraction(self):
return self.backgroundSubtracion
def setBoundary(self, boundary):
self.boundary = boundary
def getBoundary(self):
return self.boundary
def setROI(self, roi):
self.roi = roi
def getROI(self):
return self.roi
def setShadow(self, shadow):
self.shadow = shadow
def getShadow(self):
return self.shadow
def setFPS(self, fps):
self.fps = fps
def getFPS(self):
return self.fps
def setAlt(self, alt):
self.alt = alt
def getAlt(self):
return self.alt
def setElevated(self, elevated):
self.elevated = elevated
def getElevated(self):
return self.elevated
def setFocal(self, focal):
self.focal = focal
def getFocal(self):
return self.focal
def setSensorSize(self, height, width):
self.sensorHeight = height
self.sensorWidth = width
def getSensorSize(self):
return self.sensorHeight, self.sensorWidth
def setCroppingFactor(self, croppingFactor):
self.croppingfFactor = croppingFactor
def getCroppingFactor(self):
return self.croppingfFactor
def setLengthLV(self, lenghtLV):
self.lengthLV = lenghtLV
def getLengthLV(self):
return self.lengthLV
def setWidthLV(self, widthLV):
self.widthLV = widthLV
def getWidthLV(self):
return self.widthLV
def setHighLV(self, highLV):
self.highLV = highLV
def getHighLV(self):
return self.highLV
def setLengthHV(self, lenghtHV):
self.lengthHV = lenghtHV
def getLengthHV(self):
return self.lengthHV
def setWidthHV(self, widthHV):
self.widthHV = widthHV
def getWidthHV(self):
return self.widthHV
def setHighHV(self, highHV):
self.highHV = highHV
def getHighHV(self):
return self.highHV
def setDetectionLine(self, x1, y1, x2, y2):
self.detectX1 = int(x1)
self.detectY1 = int(y1)
self.detectX2 = int(x2)
self.detectY2 = int(y2)
def getDetectionLine(self):
return self.detectX1, self.detectY1, self.detectX2, self.detectY2
def setRegistrationLine(self, x1, y1, x2, y2):
self.registX1 = int(x1)
self.registY1 = int(y1)
self.registX2 = int(x2)
self.registY2 = int(y2)
def getRegistrationLine(self):
return self.registX1, self.registY1, self.registX2, self.registY2
def getTotalLV(self):
return self.total_LV
def getTotalHV(self):
return self.total_HV
def getFrameCount(self):
return self.frame
def __init__(self, filename, frame):
self.video_frame = frame
# Global variable
self.start_time = None
self.width_frame = 1120 # pixel
self.height_frame = 630 # pixel
self.init_time = 5 # second /fps (fps 30) -> 24/30 = 0.8 -> 8 second
self.frame = 0
self.total_LV = 0
self.total_HV = 0
self.totalVehicle = 0
self.initMOG2 = cv2.createBackgroundSubtractorMOG2() # Mixture of Gaussian initialization
self.initMOG = cv2.bgsegm.createBackgroundSubtractorMOG()
self.avg = 0
self.currentListVehicle = []
self.tempListVehicle = []
self.pastListVehicle = []
self.currentTrajectory = []
# Start Capture Video
self.filename = filename
self.cap = cv2.VideoCapture(filename)
self.statusNextFrame = True
# Initiation vehicle module
self.vehicle = vehicleInit.vehicle
self.trajectory = trajectoryInit.trajectory
# Fps
self.firstFrame = 0
self.endFrame = 0
self.processTime = 0
# Initiation to moving average
_, PrimImg_frame = self.cap.read()
PrimImg_frame = improc.cvtBGR2RGB(PrimImg_frame)
PrimImg_frame = cv2.resize(PrimImg_frame, (self.width_frame, self.height_frame))
self.avg = np.float32(PrimImg_frame)
def start(self):
self.timer = QtCore.QTimer()
self.timer.start(1000. / self.getFPS())
self.timer.timeout.connect(self.timeFirstFrame)
self.timer.timeout.connect(self.getNextStatusFrame)
self.timer.timeout.connect(self.timeEndFrame)
self.start_time = time.time()
# Initiation file
if self.getVideoOutput():
now = datetime.datetime.now()
formatDate = now.strftime("%d-%m-%Y %H-%M")
self.file = open("output/{0}.csv".format(formatDate), "a")
if os.stat("output/{0}.csv".format(formatDate)).st_size == 0:
self.file.write("No,Waktu,Jenis Kendaraan,Panjang,Gambar\n")
# Initiation folder
path = "output"
self.formatFolder = now.strftime("{0}/%d-%m-%Y %H-%M").format(path)
if not os.path.isdir(self.formatFolder):
os.makedirs(self.formatFolder)
def stop(self):
self.timer.stop()
def timeFirstFrame(self):
self.firstFrame = time.time()
return self.firstFrame
def timeEndFrame(self):
self.endFrame = time.time()
self.processTime = self.endFrame - self.firstFrame
return self.processTime
def deleteLater(self):
# Stop capture
self.cap.release()
# Closing file
if self.getVideoOutput():
self.file.write("Filename:" + "," + str(self.filename) + "\n" +
"Focal:" + "," + str(self.getFocal()) + "\n" +
"Angle:" + "," + str(self.getElevated()) + "\n" +
"Altitude:" + "," + str(self.getAlt()) + "\n" +
"Total LV:" + "," + str(self.total_LV) + "\n" +
"Total HV:" + "," + str(self.total_HV) + "\n" +
"Total Vehicle:" + "," + str(self.total_HV + self.total_LV) + "\n")
self.file.flush()
self.file.close()
self.total_HV = 0
self.total_LV = 0
self.frame = 0
def getNextStatusFrame(self):
totalFrame = self.cap.get(cv2.CAP_PROP_FRAME_COUNT)
if self.frame == totalFrame:
self.stop()
self.deleteLater()
self.statusNextFrame = False
elif self.statusNextFrame is True:
self.nextFrame()
def nextFrame(self):
initTime = time.time()
ret, PrimImg_frame = self.cap.read()
self.frame = int(self.cap.get(cv2.CAP_PROP_POS_FRAMES))
# ----------- Do not disturb this source code ---------- #
# Default color model is BGR format
PrimResize_frame = cv2.resize(PrimImg_frame, (self.width_frame, self.height_frame))
PrimRGB_frame = improc.cvtBGR2RGB(PrimResize_frame)
# ------ [1] Initiation background subtraction ----------#
# Initial State (IS) : RGB - primary frame
# Final State (FS) : Binary - foreground frame
if self.getBackgroundSubtraction() == "MA": # if choose Moving Average
# Moving Average subtraction
cvtScaleAbs = improc.backgroundSubtractionAverage(PrimRGB_frame, self.avg, 0.01)
movingAverage_frame = cvtScaleAbs
initBackground = improc.initBackgrounSubtraction(initTime, self.start_time, self.init_time)
# --- [x] Convert to Different Color Space ----------------#
# IS :
# FS :
PrimGray_frame = improc.cvtRGB2GRAY(PrimRGB_frame)
BackgroundGray_frame = improc.cvtRGB2GRAY(movingAverage_frame)
PrimHSV_frame = cv2.cvtColor(PrimRGB_frame, cv2.COLOR_RGB2HSV)
BackgroundHSV_frame = cv2.cvtColor(movingAverage_frame, cv2.COLOR_RGB2HSV)
# PrimLAB_frame = cv2.cvtColor(PrimRGB_frame, cv2.COLOR_RGB2LAB)
# BackgroundLAB_frame = cv2.cvtColor(movingAverage_frame, cv2.COLOR_RGB2LAB)
PrimHue, PrimSat, PrimVal = cv2.split(PrimHSV_frame)
BackHue, BackSat, BackVal = cv2.split(BackgroundHSV_frame)
# PrimLight, PrimA, PrimB = cv2.split(PrimLAB_frame)
# BackLight, BackA, BackB = cv2.split(BackgroundLAB_frame)
# -- [x] Background Extraction ---------------------------#
# IS :
# FS :
ImgDiffRGB = cv2.absdiff(PrimGray_frame, BackgroundGray_frame)
ImgDiffHSV = cv2.absdiff(PrimVal, BackVal)
# ImgDiffLAB = cv2.absdiff(PrimLight, BackLight)
combineRGBHSV = cv2.bitwise_or(ImgDiffRGB, ImgDiffHSV)
# combineLABHSV = cv2.bitwise_or(ImgDiffLAB, ImgDiffHSV)
# -- [x] Smoothing and Noise Reduction --------------------#
# IS :
# FS :
blurLevel = 15
# averageBlur = cv2.blur(combineRGBHSV, (blurLevel, blurLevel))
# medianBlur = cv2.medianBlur(combineRGBHSV, blurLevel)
gaussianBlur_frame = cv2.GaussianBlur(combineRGBHSV, (blurLevel, blurLevel), 0)
# -- [x] Thresholds to Binary ----------------------------#
# IS :
# FS :
thresholdLevel = 50
_, threshold = cv2.threshold(gaussianBlur_frame, thresholdLevel, 255, cv2.THRESH_BINARY)
# _, blur1threshold = cv2.threshold(averageBlur, thresholdLevel, 255, cv2.THRESH_BINARY)
# _, blur2threshold = cv2.threshold(gaussianBlur_frame, thresholdLevel, 255, cv2.THRESH_BINARY)
else: # Mixture of Gaussian
# Mixture of Gaussian Model Background Subtraction
MOG_frame = self.initMOG2.apply(PrimRGB_frame)
thresholdLevel = 128
_, threshold = cv2.threshold(MOG_frame, thresholdLevel, 255, cv2.THRESH_BINARY)
# medianFilter = cv2.medianBlur(MOG_frame, 21)
bin_frame = threshold.copy()
blank_frame = np.zeros((self.height_frame, self.width_frame, 1), np.uint8)
# -- [x] Draw Detection and RegistrationLine -------------#
# IS :
# FS :
thick = 2
detectLine_color = (255, 0, 0)
registLine_color = (0, 0, 255)
detectX1, detectY1, detectX2, detectY2 = self.getDetectionLine()
registX1, registY1, registX2, registY2 = self.getRegistrationLine()
if self.getROI():
cv2.line(PrimRGB_frame, (detectX1, detectY1), (detectX2, detectY2), detectLine_color, thick)
cv2.line(PrimRGB_frame, (registX1, registY1), (registX2, registY2), registLine_color, thick)
# -- [x] Morphological Operation -------------------------#
# IS : ~
# FS : ~
kernel = np.array([
[0, 1, 0],
[1, 1, 1],
[0, 1, 0]], dtype=np.uint8)
morph_frame = cv2.erode(bin_frame, kernel, iterations=3)
# morph_frame = cv2.dilate(morph_frame, kernel, iterations=2)
bin_frame = morph_frame
# -- [x] Shadow Removal ---------------------------------#
# IS :
# FS :
kernel = np.array([
[0, 1, 0],
[1, 1, 1],
[0, 1, 0]], dtype=np.uint8)
shadowThreshold = 0.1
maskBin = cv2.merge([bin_frame, bin_frame,bin_frame])
maskRgbAndBin = cv2.bitwise_and(PrimRGB_frame, maskBin)
if self.getShadow():
hsvShadowRemoval = sr.hsvPassShadowRemoval(maskRgbAndBin, shadowThreshold)
hsvMerge = cv2.merge([hsvShadowRemoval, hsvShadowRemoval, hsvShadowRemoval])
maskShadow = cv2.bitwise_and(maskRgbAndBin, hsvMerge)
gaussianBlur_shadowFrame = cv2.GaussianBlur(maskShadow, (5, 5), 0)
grayShadow = cv2.cvtColor(gaussianBlur_shadowFrame, cv2.COLOR_RGB2GRAY)
_, thresholdShadow = cv2.threshold(grayShadow, 5, 255, cv2.THRESH_BINARY)
dilateShadow = cv2.dilate(thresholdShadow, kernel, iterations=3)
erodeShadow = cv2.erode(dilateShadow, kernel, iterations=1)
# openingShadow = cv2.morphologyEx(thresholdShadow, cv2.MORPH_OPEN, kernel, iterations=2)
bin_frame = erodeShadow
binMerge = cv2.merge([bin_frame, bin_frame, bin_frame])
maskShadow = cv2.bitwise_and(PrimRGB_frame, binMerge)
# -- [x] Mask Boundary ROI ------------------------------#
# IS : ~
# FS : ~
color = (255, 255, 0)
roiThreshold = 10
ImgZero_frame = np.zeros((self.height_frame, self.width_frame), np.uint8)
x1ROI = mo.funcX_line(detectX1, detectY1, registX1, registY1, detectY1)
x2ROI = mo.funcX_line(detectX2, detectY2, registX2, registY2, detectY2)
x3ROI = mo.funcX_line(detectX1, detectY1, registX1, registY1, self.height_frame)
x4ROI = mo.funcX_line(detectX2, detectY2, registX2, registY2, self.height_frame)
pts = np.array([
[x1ROI - roiThreshold, detectY1], [x2ROI + roiThreshold, detectY2],
[x4ROI + roiThreshold, self.height_frame], [x3ROI - roiThreshold, self.height_frame]])
cv2.fillPoly(ImgZero_frame, [pts], color)
roiBinary_frame = cv2.bitwise_and(ImgZero_frame, bin_frame)
# -- [x] Contour Detection ------------------------------#
# IS :
# FS :
image, contours, hierarchy = cv2.findContours(roiBinary_frame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contoursList = len(contours)
for i in range(0, contoursList):
cnt = contours[i]
areaContours = cv2.contourArea(cnt)
xContour, yContour, widthContour, highContour = cv2.boundingRect(cnt)
# Point A : (xContour, yContour)
# Point B : (xContour + widthContour, yContour)
# Point C : (xContour + widthContour, yContour + highContour)
# Point D : (xContour, yContour + highContour)
areaBoundary = widthContour * highContour
# -- [x] Pin Hole Model -------------------------#
# IS :
# FS :
focal = self.getFocal()
theta = self.getElevated()
sensorHeight, sensorWidth = self.getSensorSize()
cropFactor = self.getCroppingFactor()
altitude = self.getAlt()
maxHighLV = self.getHighLV()
maxHighHV = self.getHighHV()
maxLengthLV = self.getLengthLV()
maxLengthHV = self.getLengthHV()
maxWidthHV = self.getWidthHV()
heightInFullFrame = (self.width_frame * 2.0) / 3
heightSurplus = (heightInFullFrame - self.height_frame) / 2
y1Vehicle = (self.height_frame + heightSurplus) - (yContour + highContour)
y2Vehicle = (self.height_frame + heightSurplus) - yContour
aspectRatioHeight = (sensorWidth / self.width_frame) * heightInFullFrame
# cropFactor = mo.determineCropFactor(self.sensorWidth, self.sensorHeight)
horizontalFocal = (((focal * 1) / aspectRatioHeight) * self.height_frame)
# verticalFocal = (focal / sensorWidth) * self.width_frame
lengthVehicle = mo.vertikalPinHoleModel(heightInFullFrame, horizontalFocal, altitude, theta, y1Vehicle, y2Vehicle, maxHighLV, maxHighHV, maxLengthLV)
centerVehicle = mo.centeroidPinHoleMode(heightInFullFrame, horizontalFocal, altitude, theta, y1Vehicle)
widthVehicle = mo.horizontalPinHoleModel(self.width_frame, horizontalFocal, altitude, xContour, (xContour + widthContour), centerVehicle)
alternative = False
if alternative:
fov = 160.0
theta = 90.0 - theta
horizontalFOV, verticalFOV = mo.transformDiagonalFOV(fov)
focal = mo.getFocalfromFOV(self.height_frame, verticalFOV)
lengthVehicle = mo.vertikalPinHoleModel(self.height_frame, focal, altitude, theta, y1Vehicle, y2Vehicle,
maxHighLV, maxHighHV, maxLengthLV)
# -- [x] Draw Boundary -----------------------#
# IS :
# FS :
colorLV = (0, 255, 0)
colorHV = (0, 0, 255)
thick = 2
size = 2
areaThreshold = 10
if (widthVehicle >= 1.0) and (widthVehicle <= 3.0) and (lengthVehicle >= 2) and (lengthVehicle < 18) and (areaContours >= (float(areaBoundary) * (float(areaThreshold) / 100))):
# Get moment for centroid
Moment = cv2.moments(cnt)
xCentroid = int(Moment['m10'] / Moment['m00'])
yCentroid = int(Moment['m01'] / Moment['m00'])
# print "length: {0} | width: {1}".format(lengthVehicle, widthVehicle)
# -- [x] Vehicle Classification -------------#
# IS :
# FS :
if lengthVehicle <= maxLengthLV:
vehicleClassification = "LV"
color = colorLV
else:
vehicleClassification = "HV"
color = colorHV
if self.getBoundary():
cv2.rectangle(PrimRGB_frame, (xContour + widthContour, yContour + highContour), (xContour, yContour), color, thick)
improc.addText(bin_frame, lengthVehicle, size, xContour, (yContour - 3))
# -- [x] Set Vehicle Identity ---------------#
# IS :
# FS :
xPredictLeftRoad = mo.funcX_line(detectX1, detectY1, registX1, registY1, y1Vehicle)
distLeftRoadtoVehicle = mo.horizontalPinHoleModel(self.width_frame, horizontalFocal, altitude, xPredictLeftRoad, (xContour + (widthContour / 2)), centerVehicle)
# print "left road to center {0} meter | camera to front vehicle {1} meter".format(distLeftRoadtoVehicle, centerVehicle)
self.currentListVehicle.append(self.vehicle(self.totalVehicle + 1 + self.currentListVehicle.__len__(), xCentroid, yCentroid,distLeftRoadtoVehicle, centerVehicle, lengthVehicle, widthVehicle, vehicleClassification, xContour, yContour, widthContour, highContour, False))
self.currentTrajectory.append(self.trajectory(self.totalVehicle + 1 + self.currentListVehicle.__len__(), xCentroid, yCentroid))
if self.pastListVehicle.__len__() == 0:
self.pastListVehicle = self.currentListVehicle
elif self.pastListVehicle.__len__() != self.currentListVehicle.__len__():
self.pastListVehicle = self.currentListVehicle
elif self.currentListVehicle.__len__() < self.pastListVehicle.__len__():
self.currentListVehicle = self.pastListVehicle
# -- [x] Hungarian Algorithm ----------------------------#
# IS :
# FS : Hungarian algorithm by munkres
hungarianAlgorithmStatus = True
trackingStatus = False
# print "temp: {0}".format(self.tempList.__len__())
# print "curr: {0}".format(self.currentListVehicle.__len__())
if self.pastListVehicle.__len__() != 0 and self.currentListVehicle.__len__() != 0 and hungarianAlgorithmStatus is True:
distance = [[0 for i in range(self.pastListVehicle.__len__())] for j in range(self.currentListVehicle.__len__())]
for i in range(self.pastListVehicle.__len__()):
for j in range(self.currentListVehicle.__len__()):
x1 = self.pastListVehicle[i].distLeft
y1 = self.pastListVehicle[i].distFront
x2 = self.currentListVehicle[j].distLeft
y2 = self.currentListVehicle[j].distFront
distance[j][i] = mo.euclideanDistance(x1, y1, x2, y2)
hungarian = Munkres()
indexes = hungarian.compute(distance)
for row, column in indexes:
self.currentListVehicle[row].idState = self.pastListVehicle[column].idState
# print "vID: {0} | idState: {1}".format(self.pastListVehicle[row].vehicleID, self.pastListVehicle[row].idState)
trackingStatus = True
# -- [x] Print Trajectory -------------------------------#
# IS :
# FS :
thick = 2
size = 1
trajectoryThreshold = 30
if self.currentTrajectory.__len__() > trajectoryThreshold:
self.currentTrajectory.pop(0)
for i in range(self.currentTrajectory.__len__()):
xTrajectory = self.currentTrajectory[i].xCoordinate
yTrajectory = self.currentTrajectory[i].yCoordinate
yPredictTrajectory = mo.funcY_line(registX1, registY1, registX2, registY2, xTrajectory)
if (yTrajectory < yPredictTrajectory) and (xTrajectory >= registX1) and (xTrajectory <= registX2):
cv2.circle(PrimRGB_frame, (xTrajectory, yTrajectory), size, (0, 255, 255), thick)
# -- [x] Counting Detection -----------------------------#
# IS :
# FS :
font = cv2.FONT_HERSHEY_DUPLEX
thick = 2
size = 2
stopGap = 200
changeRegistLine_color = (255, 255, 255)
changeThick = 4
if trackingStatus is True:
for i in range(self.currentListVehicle.__len__()):
vehicleID = self.currentListVehicle[i].vehicleID
xCentroid = self.currentListVehicle[i].xCoordinate
yCentroid = self.currentListVehicle[i].yCoordinate
yFront = self.currentListVehicle[i].yContour + self.currentListVehicle[i].highContour
lengthVehicle = self.currentListVehicle[i].vehicleLength
vehicleClassification = self.currentListVehicle[i].vehicleClass
idState = self.currentListVehicle[i].idState
print int(PrimSat[xCentroid, yCentroid])
# print "vid count : {0} | idState: {1} | xCord: {2} | yCord: {3} | xLastCord: {4} | yLastCord: {5}".format(vehicleID, idState, xCentroid, yCentroid, xCenteroidBefore, yCenteroidBefore)
yPredictRegist = mo.funcY_line(registX1, registY1, registX2, registY2, xCentroid)
yPredictDetect = mo.funcY_line(detectX1, detectY1, detectX2, detectY2, xCentroid)
countClass = improc.initCounting(registX1, registY1, registX2, registX2, xCentroid, yPredictRegist, vehicleClassification)
# print "predictRegist: {0} | predictDetect : {1}".format(yPredictRegist, yPredictDetect)
if (yFront > yPredictDetect + stopGap) and (yFront < yPredictRegist) and (xCentroid >= registX1) and (xCentroid <= registX2) and (idState is False):
self.pastListVehicle[i].idState = True
if (yFront < yPredictRegist) and (xCentroid >= registX1) and (xCentroid <= registX2):
cv2.circle(PrimRGB_frame, (xCentroid, yFront), size, (0, 0, 255), thick)
cv2.putText(PrimRGB_frame, "{0}".format(vehicleID), (xCentroid + 1, yCentroid + 1), font, 1, (0, 0, 255))
if (yFront > yPredictRegist) and (xCentroid >= registX1) and (xCentroid <= registX2) and (idState is True):
if countClass == "LV":
self.total_LV += 1
elif countClass == "HV":
self.total_HV += 1
self.totalVehicle = self.total_LV + self.total_HV
self.pastListVehicle[i].idState = False
improc.addText(PrimRGB_frame, vehicleID, size, (xCentroid + 5), (yCentroid - 5))
cv2.line(PrimRGB_frame, (registX1, registY1), (registX2, registY2), changeRegistLine_color, changeThick)
print "Total LV: {0} | Total HV: {1} | class: {2} length: {3} width: {4}".format(self.total_LV, self.total_HV, countClass, lengthVehicle, widthVehicle)
# -- [x] Crop Image -------------------------#
# IS :
# FS :
xContour = self.currentListVehicle[i].xContour
yContour = self.currentListVehicle[i].yContour
widthContour = self.currentListVehicle[i].widthContour
highContour = self.currentListVehicle[i].highContour
if self.getVideoOutput():
now = datetime.datetime.now()
formatDate = now.strftime("%d%m%Y_%H%M%S")
formatFileName = "{0}/{1}_{2:03}_{3}.jpg".format(self.formatFolder, countClass, (self.total_LV + self.total_HV), formatDate)
cropping_frame = PrimResize_frame[yContour:yContour + highContour, xContour:xContour + widthContour]
cv2.imwrite(formatFileName, cropping_frame)
# -- [x] Save Filename to Text --------------#
# IS :
# FS :
formatDate = now.strftime("%d:%m:%Y %H:%M:%S")
self.file.write(str(self.totalVehicle) + "," +
str(formatDate) + "," +
str(countClass) + "," +
str(lengthVehicle) + "," +
str(formatFileName) + "\n")
self.file.flush()
# Return variable
self.currentListVehicle = []
# ---------- Do not disturb this source code ----------- #
if self.getVideoMode() == "RGB":
show_frame = PrimRGB_frame
img = QtGui.QImage(show_frame, show_frame.shape[1], show_frame.shape[0], QtGui.QImage.Format_RGB888)
# RGB image - Format_RGB888
else:
show_frame = PrimSat
img = QtGui.QImage(show_frame, show_frame.shape[1], show_frame.shape[0], QtGui.QImage.Format_RGB16)
# Gray scale, binary image - Format_Indexed8
pix = QtGui.QPixmap.fromImage(img)
self.video_frame.setPixmap(pix)