-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtrack.py
147 lines (122 loc) · 4.13 KB
/
track.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
# coding: utf8
"""
Version : 0.2.0
Date : 7th Jan 2018
Author : xiaochus
Email : [email protected]
Affiliation : School of Computer Science and Communication Engineering
- Jiangsu University - China
License : MIT
Status : Under Active Development
Description :
OpenCV 3 & Keras implementation of the vehicle tracking.
"""
import sys
import copy
import argparse
import cv2
import numpy as np
from keras.models import load_model
from utils.entity import Entity
def main(argv):
parser = argparse.ArgumentParser()
# Required arguments.
parser.add_argument(
"--file",
help="Input video file.",
)
# Optional arguments.
parser.add_argument(
"--iou",
default=0.2,
help="threshold for tracking",
)
args = parser.parse_args()
track('video//' + args.file, args.iou)
def overlap(box1, box2):
"""
Check the overlap of two boxes
"""
endx = max(box1[0] + box1[2], box2[0] + box2[2])
startx = min(box1[0], box2[0])
width = box1[2] + box2[2] - (endx - startx)
endy = max(box1[1] + box1[3], box2[1] + box2[3])
starty = min(box1[1], box2[1])
height = box1[3] + box2[3] - (endy - starty)
if (width <= 0 or height <= 0):
return 0
else:
Area = width * height
Area1 = box1[2] * box1[3]
Area2 = box2[2] * box2[3]
ratio = Area / (Area1 + Area2 - Area)
return ratio
def track(video, iou):
camera = cv2.VideoCapture(video)
res, frame = camera.read()
y_size = frame.shape[0]
x_size = frame.shape[1]
# Load CNN classification model
model = load_model('model//weights.h5')
# Definition of MOG2 Background Subtraction
bs = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
history = 20
frames = 0
counter = 0
track_list = []
cv2.namedWindow("detection", cv2.WINDOW_NORMAL)
while True:
res, frame = camera.read()
if not res:
break
# Train the MOG2 with first frames frame
fg_mask = bs.apply(frame)
if frames < history:
frames += 1
continue
# Expansion and denoising the original frame
th = cv2.threshold(fg_mask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations=2)
image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Check the bouding boxs
for c in contours:
x, y, w, h = cv2.boundingRect(c)
if cv2.contourArea(c) > 3000:
# Extract roi
img = frame[y: y + h, x: x + w, :]
rimg = cv2.resize(img, (64, 64), interpolation=cv2.INTER_CUBIC)
image_data = np.array(rimg, dtype='float32')
image_data /= 255.
roi = np.expand_dims(image_data, axis=0)
flag = model.predict(roi)
if flag[0][0] > 0.5:
e = Entity(counter, (x, y, w, h), frame)
# Exclude existing targets in the tracking list
if track_list:
count = 0
num = len(track_list)
for p in track_list:
if overlap((x, y, w, h), p.windows) < iou:
count += 1
if count == num:
track_list.append(e)
else:
track_list.append(e)
counter += 1
# Check and update goals
if track_list:
tlist = copy.copy(track_list)
for e in tlist:
x, y = e.center
if 10 < x < x_size - 10 and 10 < y < y_size - 10:
e.update(frame)
else:
track_list.remove(e)
frames += 1
cv2.imshow("detection", frame)
if cv2.waitKey(110) & 0xff == 27:
break
camera.release()
if __name__ == '__main__':
main(sys.argv)