-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA03.py
657 lines (547 loc) · 25.8 KB
/
A03.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
from sklearn.cluster import MiniBatchKMeans
import numpy as np
from General_A03 import *
from matplotlib import pyplot as plt
def get_bound_box_image(image, box):
# (ymin, xmin, ymax, xmax)
ymin, xmin, ymax, xmax = box
subimage = image[ymin:ymax, xmin:xmax, :]
return subimage
def is_out_of_bounds(point, box):
# (ymin, xmin, ymax, xmax)
ymin, xmin, ymax, xmax = box
x, y = point
if x < xmin or x > xmax or y < ymin or y > ymax:
return True
else:
return False
def scale_box(box, psy, psx, ph, pw):
(ymin, xmin, ymax, xmax) = box
height = ymax - ymin
width = xmax - xmin
offy = psy*height
offx = psx*width
ymin += offy
xmin += offx
ymin = int(ymin)
xmin = int(xmin)
height *= ph
height = int(height)
ymax = ymin + height
width *= pw
width = int(width)
xmax = xmin + width
return (ymin, xmin, ymax, xmax)
def cluster_colors(image, k):
samples = image.astype("float32")
samples = np.reshape(samples, [-1, 3])
# https://docs.opencv.org/4.x/d1/d5c/tutorial_py_kmeans_opencv.html
ret, labels, centers = cv2.kmeans(samples, k, None,
(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 70, 0.1),
3,cv2.KMEANS_RANDOM_CENTERS)
recolor = centers[labels.flatten()]
recolor = np.reshape(recolor, image.shape)
#recolor /= 255.0
recolor = cv2.convertScaleAbs(recolor)
return recolor, labels
def k_means_colors(image, k):
(h, w) = image.shape[:2]
# convert the image from the RGB color space to the L*a*b*
# color space -- since we will be clustering using k-means
# which is based on the euclidean distance, we'll use the
# L*a*b* color space where the euclidean distance implies
# perceptual meaning
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
# reshape the image into a feature vector so that k-means
# can be applied
image = image.reshape((image.shape[0] * image.shape[1], 3))
# apply k-means using the specified number of clusters and
# then create the quantized image based on the predictions
clt = MiniBatchKMeans(n_clusters = k)
labels = clt.fit_predict(image)
quant = clt.cluster_centers_.astype("uint8")[labels]
# reshape the feature vectors to images
quant = quant.reshape((h, w, 3))
image = image.reshape((h, w, 3))
# convert from L*a*b* to RGB
quant = cv2.cvtColor(quant, cv2.COLOR_LAB2BGR)
image = cv2.cvtColor(image, cv2.COLOR_LAB2BGR)
# display the images and wait for a keypress
return quant, labels
def get_features_to_track(frame, region_of_interest):
#convert to gray
roi = get_bound_box_image(frame,region_of_interest)
roi_gray = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)
background_subtractor = cv2.createBackgroundSubtractorMOG2() #or KNN
fg_mask = background_subtractor.apply(roi)
track_points = cv2.goodFeaturesToTrack(roi_gray,100,0.01,5,mask=fg_mask,useHarrisDetector=True)
#for some reason if you don't make sure track points are np.float32's it won't work
track_points = offset_corners(track_points,region_of_interest)
return track_points
def offset_corners(track_points,track_window):
#adjust points for bounding box first frame
ymin,xmin,ymax,xmax = track_window
for point in track_points:
x = point[0][0]
y = point[0][1]
point[0][0] += xmin
point[0][1] += ymin
point = np.array([point], dtype=np.float32)
track_points = np.intp(track_points).astype(np.float32)
return track_points
def feature_point_tracking(frames, region_of_interest):
# NOTE: this worked a lot better before but I did something
# to the logic that made it stop working right :(
all_windows = []
frames = np.copy(frames)
frame = frames[0]
old_frame = np.copy(frame)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
#get subimage and then the corners of first bounding box to track
roi = get_bound_box_image(frame,region_of_interest)
roi_gray = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)
track_points = cv2.goodFeaturesToTrack(roi_gray,25,0.01,5,useHarrisDetector=True)
track_points = np.intp(track_points).astype(np.float32)
track_window = region_of_interest
ymin = track_window[0]
xmin = track_window[1]
ymax = track_window[2]
xmax = track_window[3]
track_points = offset_corners(track_points,track_window)
# Parameters for Lucas-Kanade optical flow
lk_params = dict(winSize=(7, 7), # Window size
maxLevel=2, # Number of pyramid levels
criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01))
width = old_frame.shape[1]
height = old_frame.shape[0]
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
new_points = None
all_frames = []
for frame in frames:
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
img = frame.copy()
temp = track_points.copy()
track_points, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, track_points, new_points, **lk_params)
old_points = temp
good_new = track_points[st == 1] # st==1 means found point
good_old = old_points[st == 1]
growth = .2
while len(good_new) == 0:
ymin = track_window[0]
xmin = track_window[1]
ymax = track_window[2]
xmax = track_window[3]
new_win = [int(ymin-ymin*growth),int(xmin-xmin*growth),int(ymax+ymax*growth),int(xmax+xmax*growth)]
#print("new win",new_win,"vs track win",track_window)
track_points = get_features_to_track(frame,new_win)
track_points, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, track_points, new_points, **lk_params)
good_new = track_points[st == 1]
track_window = new_win
if len(good_new) == 0: growth += .1
avg_x_movement = 0
avg_y_movement = 0
disregarded = 0
ymin = track_window[0]
xmin = track_window[1]
ymax = track_window[2]
xmax = track_window[3]
smallest_x = ymin
biggest_x = xmin
smallest_y = ymax
biggest_y = xmax
for index, point in enumerate(good_new):
# Calculate movement
a, b = good_new[index].ravel()
c, d = good_old[index].ravel()
#print("point",index,"is",good_new[index])
#print("track box is",track_window,"\n")
#if its not in the box or it moved way too much we ditch it
if abs(a-c) >= 50 or abs(b-d) >= 50 or is_out_of_bounds([a,b],track_window) or is_out_of_bounds([c,d],track_window):
disregarded += 1
#if it's bigger than increase that track window
else: #otherwise we're increasing the movement
if a < smallest_x: smallest_x = a
if a > biggest_x: biggest_x = a
if c < smallest_y: smallest_y = c
if c > biggest_y: biggest_y = c
avg_x_movement += a-c
avg_y_movement += b-d
#print("point",index,"moved by: [",a-c,",",b-d,"]")
# Draw the tracks
#mask = cv2.line(mask, (int(a), int(b)), (int(c), int(d)), (0, 255, 0), 2)
frame = cv2.circle(frame, (int(a), int(b)), 1, (0, 255, 0), -1)
img = cv2.add(frame, mask)
point = good_new.reshape(-1, 1, 2)
try:
avg_x_movement /= (len(good_new)-disregarded)
except:
print("no x points?")
avg_x_movement = 0
try:
avg_y_movement /= (len(good_new)-disregarded)
except:
print("no y points?")
avg_y_movement = 0
#print("\nAVERAGE X MOVEMENT",avg_x_movement)
#print("AVERAGE Y MOVEMENT",avg_y_movement)
new_places = []
#shift box around
new_places.append(int(ymin + avg_y_movement))
new_places.append(int(xmin + avg_x_movement))
new_places.append(int(ymax + avg_y_movement))
new_places.append(int(xmax + avg_x_movement))
track_window[0] = ymin+avg_y_movement
track_window[2] = ymax+avg_y_movement
track_window[1] = xmin+avg_x_movement
track_window[3] = xmax+avg_x_movement
#convert to int so we don't crash draw dog box
track_window[0] = int(track_window[0])
track_window[1] = int(track_window[1])
track_window[2] = int(track_window[2])
track_window[3] = int(track_window[3])
#print("track win after",track_window,"\n")
#idk why i had to make it its own thing but appending track_window wasn't appending the updated vals. idek
all_windows.append(new_places)
frame = cv2.rectangle(frame, (new_places[1],new_places[0]), (new_places[3],new_places[2]), (255,0,255), thickness=2)
all_frames.append(frame)
# Update previous frame and points
old_gray = frame_gray.copy()
return all_windows, all_frames
def camshift_tracker(frames, track_window, repetitions):
background_subtractor = cv2.createBackgroundSubtractorMOG2()
frames = np.copy(frames)
# set up the Region of
# Interest for tracking
roi = get_bound_box_image(frames[0],track_window)
ymin,xmin,ymax,xmax = track_window
centery = int((ymin+ymax)/2)
centerx = int((xmin+xmax)/2)
h = (track_window[3]-track_window[1])
w = (track_window[2]-track_window[0])
background = np.copy(frames[0])
#get dominant color: https://stackoverflow.com/a/43111221
'''pixels = np.float32(background.reshape(-1, 3))
n_colors = 5
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1)
flags = cv2.KMEANS_RANDOM_CENTERS
_, labels, palette = cv2.kmeans(pixels, n_colors, None, criteria, 10, flags)
_, counts = np.unique(labels, return_counts=True)
dominant = palette[np.argmax(counts)]
#removing the dog and replacing with max color
background[ymin:ymax, xmin:xmax, :] = dominant
#running the bg with no dog thru the background remover to hopefully train it to remove less dog
_ = background_subtractor.apply(background)'''
track_window_mini = [centery-10,
centerx-10,
centery+10,
centerx+10]
track_window_alt = [centery-50,
centerx-50,
centery+50,
centerx+50]
track_window_center = [int(track_window[0]+h*.1),
track_window[1],
int(track_window[0]+h*.3),
track_window[3]]
#apparently this goes neg on a few dogs? lol
if track_window_alt[0] < 0: track_window_alt[0] = 0
if track_window_alt[2] < 0: track_window_alt[2] = 0
psy = 0.2
psx = 0.0
ph = 0.5
pw = 1.0
(symin, sxmin, symax, sxmax) = scale_box(track_window,psy, psx, ph, pw)
track_window_center = (symin, sxmin, symax, sxmax)
roi_center = get_bound_box_image(frames[0],track_window_center)
#print("track window",track_window_center,"\nwin alt",track_window_alt)
roi_alt = get_bound_box_image(frames[0],track_window_alt)
# convert ROI from BGR to
# HSV format
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
hsv_roi_center = cv2.cvtColor(roi_center, cv2.COLOR_BGR2HSV)
hsv_roi_alt = cv2.cvtColor(roi_alt, cv2.COLOR_BGR2HSV)
# perform masking operation
roi_mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_center_mask = cv2.inRange(hsv_roi_center, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_alt_mask = cv2.inRange(hsv_roi_alt, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
# generate histograms & normalize them
roi_hist = cv2.calcHist([hsv_roi],[0,1],roi_mask,[8,8],[0,181,0,256])
roi_center_hist = cv2.calcHist([hsv_roi_center],[0,1],roi_center_mask,[8,8],[0,180,0,256])
roi_alt_hist = cv2.calcHist([hsv_roi_alt],[0,1],roi_alt_mask,[8,8],[0,180,0,256])
roi_center_hist_hue_only = cv2.calcHist([hsv_roi_center],[0],roi_center_mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
cv2.normalize(roi_center_hist,roi_center_hist,0,100,cv2.NORM_MINMAX)
cv2.normalize(roi_alt_hist,roi_alt_hist,0,100,cv2.NORM_MINMAX)
cv2.normalize(roi_center_hist_hue_only,roi_center_hist_hue_only,0,255,cv2.NORM_MINMAX)
#fixed back projection hack
max_index = 180*256
hue_sat_index_hist = np.arange(max_index, dtype="float32")
hue_sat_index_hist = np.reshape(hue_sat_index_hist, (180,256))
sub_hsv = cv2.cvtColor(roi_center, cv2.COLOR_BGR2HSV)
sub_hue_sat_index = cv2.calcBackProject([sub_hsv], [0,1], hue_sat_index_hist, [0,180,0,256],1)
combo_sub = np.stack([sub_hue_sat_index, sub_hsv[...,2]], axis=-1)
sub_hist = cv2.calcHist([combo_sub], [0, 1], None, [max_index, 256], [0,max_index,0,256])
cv2.normalize(sub_hist, sub_hist, 0, 255, cv2.NORM_MINMAX)
# Setup the termination criteria,
# either 15 iteration or move by
# atleast 2 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 20, 1)
all_frames = []
dst_frames = []
all_points = []
all_points_hue = []
eroded_frames = []
track_win = track_window
track_win_orig = track_window
frame_height, frame_width = frames[0].shape[:2]
#looping thru frames a few times for best results. sweet spot is between 3-5
cnt = 0
while cnt < repetitions:
for frame in frames:
# Resize the video frames.
frame = np.copy(frame)
ymin,xmin,ymax,xmax = track_win
h = ymax-ymin
w = xmax-xmin
#blur image
blurred=cv2.blur(frame,(10,10))
#remove bg
fg_mask = background_subtractor.apply(blurred)
# Generate output
frame_masked = cv2.bitwise_and(frame, frame, mask=fg_mask)
# perform thresholding on
# the video frames
'''mask_thresh = cv2.threshold(fg_mask,
180, 155,
cv2.THRESH_TOZERO_INV)[1]
frame_masked2 = cv2.bitwise_and(frame, frame, mask=mask_thresh)'''
eroded_frames.append(frame_masked)
# convert from BGR to HSV
# format.
hsv = cv2.cvtColor(frame_masked,
cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],
[0,1],
roi_center_hist,
[0,180,0,256], 1)
dst_hue = cv2.calcBackProject([hsv],
[0,1],
roi_alt_hist,
[0,180,0,256], 1)
'''dst_hue = cv2.calcBackProject([hsv],
[0],
roi_center_hist_hue_only,
[0,180], 1)'''
# Converting to unique index for hue-saturation
image_hsv = cv2.cvtColor(frame_masked, cv2.COLOR_BGR2HSV)
fr_hue_sat_index_hist = np.arange(max_index, dtype="float32")
fr_hue_sat_index_hist = np.reshape(fr_hue_sat_index_hist, (180,256))
img_hue_sat_index = cv2.calcBackProject([image_hsv], [0,1],
fr_hue_sat_index_hist, [0,180,0,256],1)
combo_image = np.stack([img_hue_sat_index, image_hsv[...,2]], axis=-1)
heat_map_hsv = cv2.calcBackProject([combo_image], [0,1], sub_hist, [0,max_index,0,256],1)
# apply Camshift to get the
# new location
ret, track_window = cv2.CamShift(dst,
track_window,
term_crit)
ret_hue, track_win = cv2.CamShift(dst_hue,
track_win,
term_crit)
# Draw it on image
pts = cv2.boxPoints(ret)
pts2 = cv2.boxPoints(ret_hue)
# convert from floating
# to integer
pts = np.intp(pts)
pts2 = np.intp(pts2)
# Draw Tracking window on the
# video frame.
res = cv2.polylines(frame,
[pts],
True,
(0, 255, 255),
2)
res = cv2.polylines(res,
[pts2],
True,
(255, 0, 255),
2)
if cnt >= repetitions-1:
all_points.append([pts])
all_points_hue.append([pts2])
all_frames.append(res)
dst_frames.append(dst)
track_win = track_win_orig
cnt +=1
return all_frames, dst_frames, eroded_frames, all_points, all_points_hue, roi, roi_center
def cv_tracker(frames, track_window):
# initialize a dictionary that maps strings to their corresponding
# OpenCV object tracker implementations
# TODO: maybe combine this with meanshift to adjust size?
# we wanna average possibly and if meanshift is too far away do NOT change the size
# or pos of the box much
# initialize OpenCV's special multi-object tracker
#trackers = cv2.MultiTracker_create()
tracker = cv2.TrackerMIL.create()
#need to rearrange track window from ymin,xmin,ymax,xmax to xmin,ymin,boxwidth,boxheight
track_window = (track_window[1],track_window[0],track_window[3]-track_window[1],track_window[2]-track_window[0])
bgremover = cv2.createBackgroundSubtractorMOG2()
framey = bgremover.apply(frames[0])
tracker.init(framey,track_window)
all_frames = []
all_boxes = []
for frame in frames:
frame_drawn = bgremover.apply(frame)
# grab the updated bounding box coordinates (if any) for each
# object that is being tracked
(success, box) = tracker.update(frame)
# loop over the bounding boxes and draw then on the frame
(x, y, w, h) = box
cv2.rectangle(frame_drawn, (x, y), (x + w, y + h), (0, 255, 0), 2)
all_boxes.append([y,x,y+h,x+w])
all_frames.append(frame_drawn)
return all_boxes, all_frames
def track_doggo(video_frames, region_of_interest):
frame_height, frame_width = frames[0].shape[:2]
final_boxes = []
#CV_TRACKER METHOD
roi_ymin, roi_xmin, roi_ymax, roi_xmax = region_of_interest
centery = int((roi_ymin+roi_ymax)/2)
centerx = int((roi_xmin+roi_xmax)/2)
h = roi_ymax-roi_ymin
w = roi_xmax-roi_xmin
# track box gets a smol window bc it gets lost when its big. mostly just to keep shit on track.
# if camshift flips out and is too far away from it we will prefer tracker
# we will ignore the track box if it gets stuck
track_window_mini = [centery-10,
centerx-10,
centery+10,
centerx+10]
track_window_thin = [int(roi_ymin+h*.2),
roi_xmin,
int(roi_ymax+h*.4),
roi_xmax]
#get boxes from cv tracker
cv_track_boxes = cv_tracker(video_frames,track_window_mini)[0]
cv_track_boxes_big = cv_tracker(video_frames,region_of_interest)[0]
for index in range(0,len(cv_track_boxes)):
tcenterx = int((cv_track_boxes[index][2]+cv_track_boxes[index][0])/2)
tcentery = int((cv_track_boxes[index][2]+cv_track_boxes[index][0])/2)
th = cv_track_boxes[index][2]-cv_track_boxes[index][0]
t2h = cv_track_boxes_big[index][2]-cv_track_boxes_big[index][0]
tw = cv_track_boxes[index][3]-cv_track_boxes[index][1]
t2w = cv_track_boxes_big[index][3]-cv_track_boxes_big[index][1]
cv_track_boxes[index][0] = int((cv_track_boxes[index][0]+cv_track_boxes_big[index][0])/2)
cv_track_boxes[index][1] = int((cv_track_boxes[index][1]+cv_track_boxes_big[index][1])/2)
cv_track_boxes[index][2] = int((cv_track_boxes[index][2]+cv_track_boxes_big[index][2])/2)
cv_track_boxes[index][3] = int((cv_track_boxes[index][3]+cv_track_boxes_big[index][3])/2)
#get points from camshift
cs_points = camshift_tracker(video_frames,region_of_interest,repetitions=2)[3]
cs_boxes = []
# turn camshift points into square boxes in format ymin,xmin,ymax,xmax
for point_group in cs_points:
#set defaults to first set of points in the group
ymin = point_group[0][0][1]
xmin = point_group[0][0][0]
ymax = point_group[0][0][1]
xmax = point_group[0][0][0]
#adjust in case something smaller/larger is found
for point_pair in point_group:
for point in point_pair:
if point[1] > ymax: ymax = point[1]
if point[1] < ymin: ymin = point[1]
if point[0] > xmax: xmax = point[0]
if point[0] < xmin: xmin = point[0]
#tend to track the back of the dog so we're trying to make adjustments and make the final box less extreme
if (ymax-ymin) > 5*(xmax-xmin):
ymax = int(ymax/2)
if (ymax-ymin)*5 < (xmax-xmin):
ymax *= 2 #less aggressive about increasing height cuz dogs are generally rectangles
if (xmax-xmin) > 5*(ymax-ymin):
xmax = int(xmax/2)
if (xmax-xmin)*5 < (ymax-ymin):
xmax *= 2
cs_boxes.append([ymin,xmin,ymax,xmax]) #append the final square box since draw dog boxes doesn't care about rotation
#we will at least get the first box right!
cs_boxes[0] = region_of_interest
if (abs(ymin-cs_boxes[1][0]) >= 300 or abs(xmin-cs_boxes[1][1]) >= 300 or abs(xmax-cs_boxes[1][3]) >= 300 or abs(ymax-cs_boxes[1][2]) >= 300):
cs_weight = 0.15
tracker_weight = 0.85
else:
cs_weight = .9
tracker_weight = .1
prev_box = []
for index in range(1,len(cs_boxes)):
# sometimes camshift just fails bc it doesn't always get enough loops thru
# the vid to "learn" its tracking so we use the tracker as a backup.
# its better than nothing.
if cs_boxes[index][0] == 0 and cs_boxes[index][1] == 0 and cs_boxes[index][2] == 0 and cs_boxes[index][3] == 0:
cs_boxes[index] = cv_track_boxes[index]
else:
#if camshift jumps too far away from last point, then don't use it. because it means it snapped to a bg object. sigh.
if abs(cs_boxes[index][0]-cs_boxes[index-1][0]) > 150 or abs(cs_boxes[index][0]-cv_track_boxes[index-1][0]) > 150:
cs_boxes[index] = cv_track_boxes[index]
else:
cs_boxes[index][0] = int(cs_boxes[index][0]*cs_weight+cv_track_boxes[index][0]*tracker_weight)
cs_boxes[index][1] = int(cs_boxes[index][1]*cs_weight+cv_track_boxes[index][1]*tracker_weight)
cs_boxes[index][2] = int(cs_boxes[index][2]*cs_weight+cv_track_boxes[index][2]*tracker_weight)
cs_boxes[index][3] = int(cs_boxes[index][3]*cs_weight+cv_track_boxes[index][3]*tracker_weight)
# some minor scaling to maximize IOU
# bc it loves the dog's back/head in a lot of these
# i'm sorry for cheese
cur_ymin,cur_xmin,cur_ymax,cur_xmax = cs_boxes[index]
cur_centery = int((cur_ymin+cur_ymax)/2)
cur_centerx = int((cur_xmin+cur_xmax)/2)
cur_h = (cur_ymax-cur_ymin)
cur_w = (cur_xmax-cur_ymax)
#box gets a lil wider
cs_boxes[index][1] = int(cs_boxes[index][1]*.95)
#box gets a lil taller
cs_boxes[index][2] = int(cs_boxes[index][2]*1.15)
return cs_boxes
# fp tracker. does not work well anymore.
# fp_boxes = feature_point_tracking(frames, region_of_interest)[0]
# return fp_boxes
# Load dog dataset
dog_indexes = 7,8,9,11
dog_index = 18
max_images_to_load = 60 #120
starting_index = 30 #0
frames, dog_boxes, dog_video_name = load_dog_video(dog_index,
max_images_to_load=max_images_to_load,
starting_frame_index=starting_index)
def main():
index = 0
key = -1
cam_boxes = track_doggo(frames, dog_boxes[0])
#cv_track_boxes = cv_tracker(frames, dog_boxes[0])[0]
#print("dof_boxes",dof_boxes)
while key != 27: #ESC_KEY
old_frame = np.copy(frames[index])
frame = np.copy(frames[index])
dofs = np.copy(frame)
image = np.copy(frames[index])
draw_dog_box(image, dog_boxes[index], (0,0,0))
#draw_dog_box(image, cv_track_boxes[index], (140,0,0)) #navy
draw_dog_box(image, cam_boxes[index], (255,0,255)) #pink
# DOG
cv2.imshow("DOG", image)
key = cv2.waitKey(33)
#if key != -1: print("KEYPRESS",key)
if key == 32:
print("\nFRAME",index)
print("CS BOX",cam_boxes[index])
#print("CT BOX",cv_track_boxes[index])
if key == 97: #A
index -= 1
elif key == 100: #D
index += 1
if index >= len(frames):
index = 0
elif index < 0:
index = len(frames)-1
cv2.destroyAllWindows()
if __name__ == "__main__":
main()