質問編集履歴
2
ソースコード
title
CHANGED
File without changes
|
body
CHANGED
@@ -32,265 +32,8 @@
|
|
32
32
|
Abort trap: 6
|
33
33
|
|
34
34
|
###該当のソースコード
|
35
|
-
Python
|
36
35
|
|
37
36
|
|
38
|
-
coding: UTF-8
|
39
|
-
import cv2
|
40
|
-
import cv2.cv as cv
|
41
|
-
import numpy as np
|
42
|
-
import os.path
|
43
|
-
import sys
|
44
|
-
import copy
|
45
|
-
import csv
|
46
|
-
import math
|
47
|
-
|
48
|
-
|
49
|
-
fps = 30
|
50
|
-
fourcc = cv2.cv.CV_FOURCC('m','p','4','v')
|
51
|
-
delete_cord_x1 = 2770
|
52
|
-
delete_cord_y1 = 1791
|
53
|
-
delete_cord_x2 = 2970
|
54
|
-
delete_cord_y2 = 1791
|
55
|
-
|
56
|
-
font = cv2.FONT_ITALIC
|
57
|
-
|
58
|
-
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
|
59
|
-
|
60
|
-
|
61
|
-
cascade_path = "./Cascade/cascade.xml"
|
62
|
-
cascade = cv2.CascadeClassifier(cascade_path)
|
63
|
-
|
64
|
-
|
65
|
-
color_green = (0,255,0)
|
66
|
-
color_blue = (255,0,0)
|
67
|
-
color_red = (0,0,255)
|
68
|
-
|
69
|
-
|
70
|
-
DetectInformations = []
|
71
|
-
tmp_DetectInformations = []
|
72
|
-
tmp = []
|
73
|
-
|
74
|
-
|
75
|
-
argv = sys.argv
|
76
|
-
argc = len(argv)
|
77
|
-
if(argc != 2):
|
78
|
-
print 'Usage: python object_tracking.py FileName'
|
79
|
-
quit()
|
80
|
-
|
81
|
-
|
82
|
-
def func():
|
83
|
-
f = open("new_od_data.csv","w")
|
84
|
-
csvWriter = csv.writer(f,lineterminator="\n")
|
85
|
-
firstline = ["ID","Origin","Destination","speed"]
|
86
|
-
csvWriter.writerow(firstline)
|
87
|
-
|
88
|
-
csv_array = []
|
89
|
-
cap = cv2.VideoCapture(argv[1])
|
90
|
-
width = int(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH))
|
91
|
-
hight = int(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
|
92
|
-
goal_frame = int(cap.get(cv.CV_CAP_PROP_FRAME_COUNT))
|
93
|
-
tmp_name,ext = os.path.splitext(argv[1])
|
94
|
-
output_name = tmp_name+"new_tracked.m4v"
|
95
|
-
output = cv2.VideoWriter( output_name,fourcc,fps,(width,hight))
|
96
|
-
|
97
|
-
frame_num = 0
|
98
|
-
ID = 0
|
99
|
-
print 'goal is',goal_frame
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
while(cap.isOpened):
|
104
|
-
ret,frame = cap.read()
|
105
|
-
gray_frame = cv2.cvtColor(frame,cv2.cv.CV_BGR2GRAY)
|
106
|
-
if ret == False:
|
107
|
-
break
|
108
|
-
|
109
|
-
|
110
|
-
if len(DetectInformations) == 0:
|
111
|
-
rects = cascade.detectMultiScale(gray_frame, scaleFactor=1.11,minNeighbors=3,maxSize=(80,80),minSize=(30,30))
|
112
|
-
for rect in rects:
|
113
|
-
|
114
|
-
|
115
|
-
if rect[0] < delete_cord_x1 and rect[1] < delete_cord_y1:
|
116
|
-
pass
|
117
|
-
elif delete_cord_x2 < rect[0] and rect[1] < delete_cord_y2:
|
118
|
-
pass
|
119
|
-
else:
|
120
|
-
ID = ID + 1
|
121
|
-
DetectInformations.append(DetectInfo(rect,ID))
|
122
|
-
|
123
|
-
DrawRect(frame)
|
124
|
-
|
125
|
-
|
126
|
-
else:
|
127
|
-
rects = cascade.detectMultiScale(gray_frame, scaleFactor=1.11,minNeighbors=3,maxSize=(80,80),minSize=(30,30))
|
128
|
-
del tmp[:]
|
129
|
-
for rect in rects:
|
130
|
-
tmp.append(DetectInfo(rect,0))
|
131
|
-
|
132
|
-
|
133
|
-
for tmp_DI in tmp:
|
134
|
-
if tmp_DI.first_rect[0] < delete_cord_x1 and tmp_DI.first_rect[1] < delete_cord_y1:
|
135
|
-
tmp_DI.delete = True
|
136
|
-
if delete_cord_x2 < tmp_DI.first_rect[0] and tmp_DI.first_rect[1] < delete_cord_y2:
|
137
|
-
tmp_DI.delete = True
|
138
|
-
|
139
|
-
|
140
|
-
for DI in DetectInformations:
|
141
|
-
for tmp_DI in tmp:
|
142
|
-
if DI.now_rect[0] < delete_cord_x1 and DI.now_rect[1] < delete_cord_y1:
|
143
|
-
DI.delete = True
|
144
|
-
elif delete_cord_x2 < DI.now_rect[0] and DI.now_rect[1] < delete_cord_y2:
|
145
|
-
DI.delete = True
|
146
|
-
else:
|
147
|
-
distance = math.hypot(tmp_DI.first_rect[0] - DI.now_rect[0],tmp_DI.first_rect[1] - DI.now_rect[1])
|
148
|
-
if distance < 40:
|
149
|
-
tmp_DI.delete = True
|
150
|
-
DI.new = False
|
151
|
-
DI.now_rect = tmp_DI.first_rect
|
152
|
-
|
153
|
-
elif distance == 0:
|
154
|
-
DI.delete = True
|
155
|
-
|
156
|
-
|
157
|
-
for DI in DetectInformations:
|
158
|
-
movement = math.hypot(DI.first_rect[0] - DI.now_rect[0], DI.first_rect[1] - DI.now_rect[1])
|
159
|
-
if movement < 5:
|
160
|
-
DI.delete = True
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
for DI in DetectInformations:
|
166
|
-
if DI.delete:#DI.delete=True
|
167
|
-
Origin = CalCenter(DI.first_rect)
|
168
|
-
Destination = CalCenter(DI.now_rect)
|
169
|
-
speed = CalSpeed(Origin[0] ,Origin[1],Destination[0] ,Destination[1],DI.count)
|
170
|
-
line = [DI.ID,CalCenter(DI.first_rect),CalCenter(DI.now_rect),speed]
|
171
|
-
csvWriter.writerow(line)
|
172
|
-
|
173
|
-
|
174
|
-
DeleteTmp()
|
175
|
-
DeleteDetectInfo()
|
176
|
-
|
177
|
-
|
178
|
-
for tmp_DI in tmp:
|
179
|
-
ID = ID + 1
|
180
|
-
DetectInformations.append(DetectInfo(tmp_DI.first_rect,ID))
|
181
|
-
|
182
|
-
|
183
|
-
DrawRect(frame)
|
184
|
-
|
185
|
-
|
186
|
-
output.write(frame)
|
187
|
-
|
188
|
-
|
189
|
-
frame_num += 1
|
190
|
-
if(frame_num == goal_frame):
|
191
|
-
break
|
192
|
-
if (frame_num % 10 == 0):
|
193
|
-
print frame_num
|
194
|
-
|
195
|
-
|
196
|
-
###################動画処理終了###########################
|
197
|
-
|
198
|
-
|
199
|
-
text = "#########################################"
|
200
|
-
csvWriter.writerow(text)
|
201
|
-
for DI in DetectInformations:
|
202
|
-
Origin = CalCenter(DI.first_rect)
|
203
|
-
Destination = CalCenter(DI.now_rect)
|
204
|
-
speed = CalSpeed(Origin[0] ,Origin[1],Destination[0] ,Destination[1],DI.count)
|
205
|
-
line = [DI.ID,Origin,Destination,speed]
|
206
|
-
csvWriter.writerow(line)
|
207
|
-
|
208
|
-
cap.release()
|
209
|
-
output.release()
|
210
|
-
cv2.destroyAllWindows()
|
211
|
-
f.close()
|
212
|
-
|
213
|
-
|
214
|
-
print frame_num
|
215
|
-
print "output :",output_name,"od_data.csv"
|
216
|
-
|
217
|
-
|
218
|
-
def DrawRect(frame):
|
219
|
-
for DI in DetectInformations:
|
220
|
-
DI.count = DI.count + 1
|
221
|
-
if DI.new == True:
|
222
|
-
cv2.rectangle(frame,tuple(DI.first_rect[0:2]),tuple(DI.first_rect[0:2]+DI.first_rect[2:4]),color_blue,1)
|
223
|
-
cv2.putText(frame,DI.ID,(DI.first_rect[0]+2,DI.first_rect[1]+2),font,0.65,color_red)
|
224
|
-
if DI.count > 5:
|
225
|
-
DI.delete = True
|
226
|
-
DeleteDetectInfo()
|
227
|
-
|
228
|
-
elif DI.new == False:
|
229
|
-
cv2.rectangle(frame,tuple(DI.now_rect[0:2]),tuple(DI.now_rect[0:2]+DI.now_rect[2:4]),color_green,1)
|
230
|
-
cv2.putText(frame,DI.ID,(DI.now_rect[0]+2,DI.now_rect[1]+2),font,0.65,color_red)
|
231
|
-
|
232
|
-
|
233
|
-
def CalCenter(rect):
|
234
|
-
x = rect[0]+rect[2]/2
|
235
|
-
y = rect[1]+rect[3]/2
|
236
|
-
center = []
|
237
|
-
center.append(x)
|
238
|
-
center.append(y)
|
239
|
-
return center
|
240
|
-
|
241
|
-
|
242
|
-
def CalSpeed(x1,y1,x2,y2,frame_count):
|
243
|
-
dist = math.hypot(x1 - x2,y1 - y2)
|
244
|
-
if frame_count == 0:
|
245
|
-
return 0
|
246
|
-
else:
|
247
|
-
speed = dist / frame_count
|
248
|
-
return speed
|
249
|
-
|
250
|
-
|
251
|
-
def DeleteDetectInfo():
|
252
|
-
del tmp_DetectInformations[:]
|
253
|
-
for DI in DetectInformations:
|
254
|
-
if DI.delete == False:
|
255
|
-
tmp_DetectInformations.append(DI)
|
256
|
-
del DetectInformations[:]
|
257
|
-
for DI in tmp_DetectInformations:
|
258
|
-
DetectInformations.append(DI)
|
259
|
-
|
260
|
-
|
261
|
-
def DeleteTmp():
|
262
|
-
del tmp_DetectInformations[:]
|
263
|
-
for DI in tmp:
|
264
|
-
if DI.delete == False:
|
265
|
-
tmp_DetectInformations.append(DI)
|
266
|
-
del tmp[:]
|
267
|
-
for DI in tmp_DetectInformations:
|
268
|
-
tmp.append(DI)
|
269
|
-
|
270
|
-
class DetectInfo:
|
271
|
-
def __init__(self,rect,ID):
|
272
|
-
self.first_rect = rect
|
273
|
-
self.now_rect = rect
|
274
|
-
self.previous_rect = rect
|
275
|
-
self.ID = str(ID)
|
276
|
-
self.delete = False
|
277
|
-
self.new = True
|
278
|
-
self.count = 0
|
279
|
-
self.cmpcount = 0
|
280
|
-
|
281
|
-
def Get_now_rect(self):
|
282
|
-
return self.now_rect
|
283
|
-
def Get_first_rect(self):
|
284
|
-
return self.first_rect
|
285
|
-
def Get_ID(self):
|
286
|
-
return self.ID
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
if __name__ == '__main__':
|
291
|
-
func()
|
292
|
-
|
293
|
-
|
294
37
|
###試したこと
|
295
38
|
ネットで調べましたが原因がわかりませんでした。
|
296
39
|
同じバージョン(macOS,opencv,python全て)の他のmacでは実行できます。
|
1
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|