-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack_objects.py
160 lines (127 loc) · 4.88 KB
/
track_objects.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
import numpy as np
import cv2
import time
import os
import nms
import traceback
home = os.path.expanduser('~')
def check_is_movement(possible_objects):
for i in possible_objects:
if i[2] - i[0] > 30 or i[3] - i[1] > 30:
return True
if len(possible_objects) > 10:
return True
return False
def save_video(frame_array, frame, count):
cv2.imwrite(home + '/video_results_3/'+str(count)+'.jpg', first_frame)
fps = 25
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
vout = cv2.VideoWriter()
vout.open(home + '/video_results_3/' + time.time().__str__() + '.mp4', fourcc, fps, (frame.shape[1], frame.shape[0]),
True)
for frame in frame_array:
vout.write(frame)
vout.release()
cv2.destroyAllWindows()
def apply_contours(image: np.ndarray):
im2, contours, hierarchy = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
new_contours = []
for c in contours:
shp = np.asarray(c).shape
if shp[0] is not 1 or shp[1] is not 1:
new_contours.append(c)
rect_list = []
for contour in contours[1:]:
arr = np.squeeze(np.asarray(contour), axis=1)
rect_list.append([min(arr[:, 0:-1])[0], min(arr[:, 1:])[0], max(arr[:, 0:-1])[0], max(arr[:, 1:])[0]])
return rect_list
def pad_black(frame):
frame[0:2] = np.full(frame[0:2].shape, fill_value=0, dtype=np.uint8)
frame[-2:] = np.full(frame[-2:].shape, fill_value=0, dtype=np.uint8)
frame[:, 0:2] = np.full(frame[:, 0:2].shape, fill_value=0, dtype=np.uint8)
frame[:, -2:] = np.full(frame[:, -2:].shape, fill_value=0, dtype=np.uint8)
return frame
def check_with_first_frame(first_frame, intermediate_frame):
first_frame_gray = cv2.cvtColor(first_frame, cv2.COLOR_BGR2GRAY)
intermediate_frame_gray = cv2.cvtColor(intermediate_frame, cv2.COLOR_BGR2GRAY)
frameDelta = cv2.absdiff(first_frame_gray, intermediate_frame_gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.medianBlur(thresh, ksize=7)
thresh = pad_black(thresh)
dilated = cv2.dilate(thresh, kernel, iterations=2)
possible_objects = apply_contours(dilated)
return check_is_movement(possible_objects)
cap = cv2.VideoCapture('/home/axis-inside/Desktop/skimming/19-Sep-17.mp4')
fgbg = cv2.createBackgroundSubtractorMOG2()
cv2.createBackgroundSubtractorMOG2(detectShadows=False)
kernel = np.ones((5, 5), np.uint8)
frame_count = 1
previous = 0
first_frame = None
frame_array = []
no_mov_arr = []
rejected_arr = []
first_count = 1
while(1):
ret, frame = cap.read()
if frame is None:
if previous == 0:
previous = 1
continue
if previous == 1:
break
if first_frame is None:
first_frame = frame
try:
fgmask = fgbg.apply(frame)
fgmask = cv2.medianBlur(fgmask, ksize=7)
fgmask = cv2.threshold(fgmask, 25, 255, cv2.THRESH_BINARY)[1]
fgmask = cv2.dilate(fgmask, kernel, iterations=2)
fgmask = pad_black(fgmask)
possible_objects = apply_contours(fgmask)
#possible_objects = nms.non_max_suppression(np.asarray(possible_objects))
frame_count += 1
if check_is_movement(possible_objects):
frame_array.append(frame)
elif check_with_first_frame(first_frame, frame):
frame_array.append(frame)
if len(rejected_arr) == 0:
rejected_arr.append(frame_count)
if frame_count - rejected_arr[-1] >1:
rejected_arr = [frame_count]
if frame_count - rejected_arr[-1] == 1:
rejected_arr.append(frame_count)
if len(rejected_arr) >=1000:
save_video(list(frame_array), first_frame, first_count)
first_count += 1
frame_array =[]
first_frame = frame
rejected_arr = []
no_mov_arr = []
continue
else:
if len(frame_array)!=0:
if len(no_mov_arr) == 0:
no_mov_arr.append(frame_count)
if frame_count - no_mov_arr[-1] >1:
no_mov_arr = [frame_count]
if frame_count - no_mov_arr[-1] == 1:
no_mov_arr.append(frame_count)
if len(no_mov_arr) >=12:
save_video(list(frame_array), first_frame, first_count)
first_count += 1
frame_array = []
first_frame = frame
no_mov_arr = []
rejected_arr = []
continue
print(frame_count, frame.shape, "No Mov Arr len ", len(no_mov_arr))
# cv2.imshow('frame', fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
except Exception:
traceback.print_exc()
break
cap.release()
cv2.destroyAllWindows()