質問編集履歴
2
ソースコード
test
CHANGED
File without changes
|
test
CHANGED
@@ -66,520 +66,6 @@
|
|
66
66
|
|
67
67
|
###該当のソースコード
|
68
68
|
|
69
|
-
Python
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
coding: UTF-8
|
76
|
-
|
77
|
-
import cv2
|
78
|
-
|
79
|
-
import cv2.cv as cv
|
80
|
-
|
81
|
-
import numpy as np
|
82
|
-
|
83
|
-
import os.path
|
84
|
-
|
85
|
-
import sys
|
86
|
-
|
87
|
-
import copy
|
88
|
-
|
89
|
-
import csv
|
90
|
-
|
91
|
-
import math
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
fps = 30
|
98
|
-
|
99
|
-
fourcc = cv2.cv.CV_FOURCC('m','p','4','v')
|
100
|
-
|
101
|
-
delete_cord_x1 = 2770
|
102
|
-
|
103
|
-
delete_cord_y1 = 1791
|
104
|
-
|
105
|
-
delete_cord_x2 = 2970
|
106
|
-
|
107
|
-
delete_cord_y2 = 1791
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
font = cv2.FONT_ITALIC
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
cascade_path = "./Cascade/cascade.xml"
|
122
|
-
|
123
|
-
cascade = cv2.CascadeClassifier(cascade_path)
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
color_green = (0,255,0)
|
130
|
-
|
131
|
-
color_blue = (255,0,0)
|
132
|
-
|
133
|
-
color_red = (0,0,255)
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
DetectInformations = []
|
140
|
-
|
141
|
-
tmp_DetectInformations = []
|
142
|
-
|
143
|
-
tmp = []
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
argv = sys.argv
|
150
|
-
|
151
|
-
argc = len(argv)
|
152
|
-
|
153
|
-
if(argc != 2):
|
154
|
-
|
155
|
-
print 'Usage: python object_tracking.py FileName'
|
156
|
-
|
157
|
-
quit()
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
def func():
|
164
|
-
|
165
|
-
f = open("new_od_data.csv","w")
|
166
|
-
|
167
|
-
csvWriter = csv.writer(f,lineterminator="\n")
|
168
|
-
|
169
|
-
firstline = ["ID","Origin","Destination","speed"]
|
170
|
-
|
171
|
-
csvWriter.writerow(firstline)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
csv_array = []
|
176
|
-
|
177
|
-
cap = cv2.VideoCapture(argv[1])
|
178
|
-
|
179
|
-
width = int(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH))
|
180
|
-
|
181
|
-
hight = int(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT))
|
182
|
-
|
183
|
-
goal_frame = int(cap.get(cv.CV_CAP_PROP_FRAME_COUNT))
|
184
|
-
|
185
|
-
tmp_name,ext = os.path.splitext(argv[1])
|
186
|
-
|
187
|
-
output_name = tmp_name+"new_tracked.m4v"
|
188
|
-
|
189
|
-
output = cv2.VideoWriter( output_name,fourcc,fps,(width,hight))
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
frame_num = 0
|
194
|
-
|
195
|
-
ID = 0
|
196
|
-
|
197
|
-
print 'goal is',goal_frame
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
while(cap.isOpened):
|
206
|
-
|
207
|
-
ret,frame = cap.read()
|
208
|
-
|
209
|
-
gray_frame = cv2.cvtColor(frame,cv2.cv.CV_BGR2GRAY)
|
210
|
-
|
211
|
-
if ret == False:
|
212
|
-
|
213
|
-
break
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
if len(DetectInformations) == 0:
|
220
|
-
|
221
|
-
rects = cascade.detectMultiScale(gray_frame, scaleFactor=1.11,minNeighbors=3,maxSize=(80,80),minSize=(30,30))
|
222
|
-
|
223
|
-
for rect in rects:
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
if rect[0] < delete_cord_x1 and rect[1] < delete_cord_y1:
|
230
|
-
|
231
|
-
pass
|
232
|
-
|
233
|
-
elif delete_cord_x2 < rect[0] and rect[1] < delete_cord_y2:
|
234
|
-
|
235
|
-
pass
|
236
|
-
|
237
|
-
else:
|
238
|
-
|
239
|
-
ID = ID + 1
|
240
|
-
|
241
|
-
DetectInformations.append(DetectInfo(rect,ID))
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
DrawRect(frame)
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
else:
|
252
|
-
|
253
|
-
rects = cascade.detectMultiScale(gray_frame, scaleFactor=1.11,minNeighbors=3,maxSize=(80,80),minSize=(30,30))
|
254
|
-
|
255
|
-
del tmp[:]
|
256
|
-
|
257
|
-
for rect in rects:
|
258
|
-
|
259
|
-
tmp.append(DetectInfo(rect,0))
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
for tmp_DI in tmp:
|
266
|
-
|
267
|
-
if tmp_DI.first_rect[0] < delete_cord_x1 and tmp_DI.first_rect[1] < delete_cord_y1:
|
268
|
-
|
269
|
-
tmp_DI.delete = True
|
270
|
-
|
271
|
-
if delete_cord_x2 < tmp_DI.first_rect[0] and tmp_DI.first_rect[1] < delete_cord_y2:
|
272
|
-
|
273
|
-
tmp_DI.delete = True
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
for DI in DetectInformations:
|
280
|
-
|
281
|
-
for tmp_DI in tmp:
|
282
|
-
|
283
|
-
if DI.now_rect[0] < delete_cord_x1 and DI.now_rect[1] < delete_cord_y1:
|
284
|
-
|
285
|
-
DI.delete = True
|
286
|
-
|
287
|
-
elif delete_cord_x2 < DI.now_rect[0] and DI.now_rect[1] < delete_cord_y2:
|
288
|
-
|
289
|
-
DI.delete = True
|
290
|
-
|
291
|
-
else:
|
292
|
-
|
293
|
-
distance = math.hypot(tmp_DI.first_rect[0] - DI.now_rect[0],tmp_DI.first_rect[1] - DI.now_rect[1])
|
294
|
-
|
295
|
-
if distance < 40:
|
296
|
-
|
297
|
-
tmp_DI.delete = True
|
298
|
-
|
299
|
-
DI.new = False
|
300
|
-
|
301
|
-
DI.now_rect = tmp_DI.first_rect
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
elif distance == 0:
|
306
|
-
|
307
|
-
DI.delete = True
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
for DI in DetectInformations:
|
314
|
-
|
315
|
-
movement = math.hypot(DI.first_rect[0] - DI.now_rect[0], DI.first_rect[1] - DI.now_rect[1])
|
316
|
-
|
317
|
-
if movement < 5:
|
318
|
-
|
319
|
-
DI.delete = True
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
for DI in DetectInformations:
|
330
|
-
|
331
|
-
if DI.delete:#DI.delete=True
|
332
|
-
|
333
|
-
Origin = CalCenter(DI.first_rect)
|
334
|
-
|
335
|
-
Destination = CalCenter(DI.now_rect)
|
336
|
-
|
337
|
-
speed = CalSpeed(Origin[0] ,Origin[1],Destination[0] ,Destination[1],DI.count)
|
338
|
-
|
339
|
-
line = [DI.ID,CalCenter(DI.first_rect),CalCenter(DI.now_rect),speed]
|
340
|
-
|
341
|
-
csvWriter.writerow(line)
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
DeleteTmp()
|
348
|
-
|
349
|
-
DeleteDetectInfo()
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
for tmp_DI in tmp:
|
356
|
-
|
357
|
-
ID = ID + 1
|
358
|
-
|
359
|
-
DetectInformations.append(DetectInfo(tmp_DI.first_rect,ID))
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
DrawRect(frame)
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
output.write(frame)
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
frame_num += 1
|
378
|
-
|
379
|
-
if(frame_num == goal_frame):
|
380
|
-
|
381
|
-
break
|
382
|
-
|
383
|
-
if (frame_num % 10 == 0):
|
384
|
-
|
385
|
-
print frame_num
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
###################動画処理終了###########################
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
text = "#########################################"
|
398
|
-
|
399
|
-
csvWriter.writerow(text)
|
400
|
-
|
401
|
-
for DI in DetectInformations:
|
402
|
-
|
403
|
-
Origin = CalCenter(DI.first_rect)
|
404
|
-
|
405
|
-
Destination = CalCenter(DI.now_rect)
|
406
|
-
|
407
|
-
speed = CalSpeed(Origin[0] ,Origin[1],Destination[0] ,Destination[1],DI.count)
|
408
|
-
|
409
|
-
line = [DI.ID,Origin,Destination,speed]
|
410
|
-
|
411
|
-
csvWriter.writerow(line)
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
cap.release()
|
416
|
-
|
417
|
-
output.release()
|
418
|
-
|
419
|
-
cv2.destroyAllWindows()
|
420
|
-
|
421
|
-
f.close()
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
print frame_num
|
428
|
-
|
429
|
-
print "output :",output_name,"od_data.csv"
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
def DrawRect(frame):
|
436
|
-
|
437
|
-
for DI in DetectInformations:
|
438
|
-
|
439
|
-
DI.count = DI.count + 1
|
440
|
-
|
441
|
-
if DI.new == True:
|
442
|
-
|
443
|
-
cv2.rectangle(frame,tuple(DI.first_rect[0:2]),tuple(DI.first_rect[0:2]+DI.first_rect[2:4]),color_blue,1)
|
444
|
-
|
445
|
-
cv2.putText(frame,DI.ID,(DI.first_rect[0]+2,DI.first_rect[1]+2),font,0.65,color_red)
|
446
|
-
|
447
|
-
if DI.count > 5:
|
448
|
-
|
449
|
-
DI.delete = True
|
450
|
-
|
451
|
-
DeleteDetectInfo()
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
elif DI.new == False:
|
456
|
-
|
457
|
-
cv2.rectangle(frame,tuple(DI.now_rect[0:2]),tuple(DI.now_rect[0:2]+DI.now_rect[2:4]),color_green,1)
|
458
|
-
|
459
|
-
cv2.putText(frame,DI.ID,(DI.now_rect[0]+2,DI.now_rect[1]+2),font,0.65,color_red)
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
def CalCenter(rect):
|
466
|
-
|
467
|
-
x = rect[0]+rect[2]/2
|
468
|
-
|
469
|
-
y = rect[1]+rect[3]/2
|
470
|
-
|
471
|
-
center = []
|
472
|
-
|
473
|
-
center.append(x)
|
474
|
-
|
475
|
-
center.append(y)
|
476
|
-
|
477
|
-
return center
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
def CalSpeed(x1,y1,x2,y2,frame_count):
|
484
|
-
|
485
|
-
dist = math.hypot(x1 - x2,y1 - y2)
|
486
|
-
|
487
|
-
if frame_count == 0:
|
488
|
-
|
489
|
-
return 0
|
490
|
-
|
491
|
-
else:
|
492
|
-
|
493
|
-
speed = dist / frame_count
|
494
|
-
|
495
|
-
return speed
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
def DeleteDetectInfo():
|
502
|
-
|
503
|
-
del tmp_DetectInformations[:]
|
504
|
-
|
505
|
-
for DI in DetectInformations:
|
506
|
-
|
507
|
-
if DI.delete == False:
|
508
|
-
|
509
|
-
tmp_DetectInformations.append(DI)
|
510
|
-
|
511
|
-
del DetectInformations[:]
|
512
|
-
|
513
|
-
for DI in tmp_DetectInformations:
|
514
|
-
|
515
|
-
DetectInformations.append(DI)
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
def DeleteTmp():
|
522
|
-
|
523
|
-
del tmp_DetectInformations[:]
|
524
|
-
|
525
|
-
for DI in tmp:
|
526
|
-
|
527
|
-
if DI.delete == False:
|
528
|
-
|
529
|
-
tmp_DetectInformations.append(DI)
|
530
|
-
|
531
|
-
del tmp[:]
|
532
|
-
|
533
|
-
for DI in tmp_DetectInformations:
|
534
|
-
|
535
|
-
tmp.append(DI)
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
class DetectInfo:
|
540
|
-
|
541
|
-
def __init__(self,rect,ID):
|
542
|
-
|
543
|
-
self.first_rect = rect
|
544
|
-
|
545
|
-
self.now_rect = rect
|
546
|
-
|
547
|
-
self.previous_rect = rect
|
548
|
-
|
549
|
-
self.ID = str(ID)
|
550
|
-
|
551
|
-
self.delete = False
|
552
|
-
|
553
|
-
self.new = True
|
554
|
-
|
555
|
-
self.count = 0
|
556
|
-
|
557
|
-
self.cmpcount = 0
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
def Get_now_rect(self):
|
562
|
-
|
563
|
-
return self.now_rect
|
564
|
-
|
565
|
-
def Get_first_rect(self):
|
566
|
-
|
567
|
-
return self.first_rect
|
568
|
-
|
569
|
-
def Get_ID(self):
|
570
|
-
|
571
|
-
return self.ID
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
if __name__ == '__main__':
|
580
|
-
|
581
|
-
func()
|
582
|
-
|
583
69
|
|
584
70
|
|
585
71
|
|
1
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|