-
Notifications
You must be signed in to change notification settings - Fork 0
/
mltbd.py.save
175 lines (166 loc) · 6.01 KB
/
mltbd.py.save
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
import argparse
import sys
import os, os.path, re
from shutil import copyfile
import cv2
import numpy as np
#import libraries from other folders
sys.path.insert(1, './darknet/python')
sys.path.insert(1, './iou-tracker')
#import for detector
from detect_dknet import detect_img
#imports for tracker
from iou_tracker import track_iou
from util import load_mot
from util import iou
from format import build_array
from format import build_array2
#global FAR/Threshold
FAR = 0.05
class tracker_args:
detection_path = './data/detections'
output_path = './data/tracks'
frames_path = './data/frames'
fmt = 'motchallenge'
sigma_l = 0.9
sigma_h = 0.98
sigma_iou = 0.1
t_min = 23
ttl = 8
nms = 0.6
def input_data(vid_loc):
'''open video, store all frames to data/img_frames'''
path_output_dir = './data/img_frames'
video = cv2.VideoCapture(vid_loc)
count = 0
while(video.isOpened()):
flag, img = video.read()
if (count > 100):
break
else:
pass
if flag:
cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count, img)
print('\t[-] Frame '+str(count))
count += 1
else:
break
cv2.destroyAllWindows()
video.release()
def tracker(args,detections):
'''runs a detection, object-oriented version of demo.py'''
#might need to add nms to load_mot
dets = load_mot(detections)
print('\t[-] Track with Intersection over Union')
tracks = track_iou(dets, args.sigma_l, args.sigma_h, args.sigma_iou, args.t_min)
print('\t[-] Format Tracks')
tracks = build_array2(tracks, fmt=args.fmt)
return tracks
def detection(image,thresh,frame_num):
'''runs a darknet detection. Returns an array of form [(object, probability, (b.x, b.y, b.w, b.h)), (object2....]
image should be the path (relative to root) showing the input image zoomed in on a track's bounding boxes'''
print('\t[-] Calling Detection')
detections = detect_img(image,thresh)
dt2 = []
print('[-] Detection Formatting Sequence')
for dt in range(len(detections)):
print('\t\t[-] Formatting Detection '+str(dt)+'/'+str(len(detections)))
#take out info in each detection, reformat for tracker
temp = []
#frame, id, b.x,b.y,b.w,b.h,prob.
#tried extend but python threw a fit
'''temp.append(frame_num)
temp.append(detections[dt][0])
temp.append(detections[dt][2][0])
temp.append(detections[dt][2][1])
temp.append(detections[dt][2][2])
temp.append(detections[dt][2][3])
temp.append(detections[1])
temp = np.asarray(temp)'''
temp = np.array([frame_num,detections[dt][0],detections[dt][2][0],detections[dt][2][1],detections[dt][2][2],detections[dt][2][3],detections[dt][1]])
dt2 = np.concatenate((dt2, temp),axis = 0)
dt2 = dt2.reshape((len(detections),7))
return dt2
def ev_thresh(len_ious,len_tracks):
global FAR
if(len_tracks == 0):
FAR = FAR + .05
elif(len_ious < len_tracks):
FAR = FAR + .05
else:
pass
return
def gui_feed():
external_call(frames,detects)
def main():
args = tracker_args()
frame_count = 0
vid_loc = './data/video.mov'
print('Track-before-Detect with Neural Networks')
print('[1] Creating Frame Data')
input_data(vid_loc)
#create bootstrap detection
print('[2] Creating Bootstrap Detection')
detections = detection('./data/img_frames/1'+'.png',FAR,0)
#enter loop where tracker is fed detections, detector fed tracks, and threshold evaluated as this changes
print('[3] Run T-b-D over frames')
while(frame_count < 100):
#grab a set amt. of frames from ./data/frames and move it to args.frames_path
#delete args.frame_path first
print('[*] Stage Frame Cluster')
for root, dirs, files in os.walk(args.frames_path):
for file in files:
os.remove(os.path.join(root, file))
#grab next 100 frames from ./data/img_frames offset by frame_count
for i in range(100):
src = './data/img_frames/'+str(i+frame_count)+'.png'
copyfile(src,args.frames_path+'/'+str(i+frame_count)+'.png')
frame_count += 100
#call tracks now
print('[*] Track Frame Cluster')
#testing here
print(detections)
tracks = tracker(args,detections)
#call detection() on last image in args.frames_path
print('[*] Detect Frame Cluster')
detections = detection(args.frames_path+'/'+str(frame_count-1)+'.png',FAR,frame_count-1)
#call detect and then mesh detect coord. over track coord. and compare, avoid detect() interepret time.
#array of detection bounding boxes
print('[*] Evaluate T-b-D Performance')
dt_boxes = []
for dt in range(len(detections)):
temp = []
temp.append(detections[dt][2])
temp.append(detections[dt][3])
temp.append(detections[dt][4])
temp.append(detections[dt][5])
dt_boxes.append(temp)
ious = []
if(len(tracks) == 0):
print('There are no tracks')
else:
for track in range(len(tracks)):
track_cmp = []
track_cmp.append(tracks[track][2])
track_cmp.append(tracks[track][3])
track_cmp.append(tracks[track][4])
track_cmp.append(tracks[track][5])
for dt in range(len(dt_boxes)):
#compare current track against all boxes
tx1,ty1,tx2,ty2 = track_cmp[0],track_cmp[1],track_cmp[0]+track_cmp[2],track_cmp[1]+track_cmp[3]
bbox1 = [tx1,ty1,tx2,ty2]
bbox1 = np.asarray(bbox1)
dx1,dy1,dx2,dy2 = dt[0],dt[1],dt[0]+dt[2],dt[1]+dt[3]
bbox2 = [dx1,dy1,dx2,dy2]
bbox2 = np.asarray(bbox2)
intovunion = iou(bbox1,bbox2)
if(intovunion > args.sigma_l):
ious.append(intovunion)
else:
pass
#call ev_thresh() to see if FAR shoud be updated
print('[*] Update False Alarm Rate Threshold')
ev_thresh(len(ious),len(tracks))
print('Track-before-Detect Complete!')
if __name__ == '__main__':
main()