1
- from perception .tasks .gate .GateSegmentation import GateSegmentationAlgo
1
+ from perception .tasks .gate .GateSegmentationAlgoA import GateSegmentationAlgoA
2
2
from perception .tasks .TaskPerceiver import TaskPerceiver
3
3
from collections import namedtuple
4
- import sys
5
4
6
5
import numpy as np
7
6
import math
8
7
import cv2 as cv
9
- import time
10
8
import statistics
11
9
12
10
13
- class GateCenter (TaskPerceiver ):
11
+ class GateCenterAlgo (TaskPerceiver ):
14
12
center_x_locs , center_y_locs = [], []
15
13
output_class = namedtuple ("GateOutput" , ["centerx" , "centery" ])
16
14
output_type = {'centerx' : np .int16 , 'centery' : np .int16 }
@@ -20,39 +18,43 @@ def __init__(self):
20
18
self .gate_center = self .output_class (250 , 250 )
21
19
self .use_optical_flow = False
22
20
self .optical_flow_c = 0.1
23
- self .gate = GateSegmentationAlgo ()
21
+ self .gate = GateSegmentationAlgoA ()
24
22
self .prvs = None
25
23
24
+ # TODO: do input and return typing
26
25
def analyze (self , frame , debug , slider_vals ):
27
- self .optical_flow_c = slider_vals ['optical_flow_c' ] / 100
28
- rect1 , rect2 , debug_filter = self .gate .analyze (frame , True )
26
+ self .optical_flow_c = slider_vals ['optical_flow_c' ]/ 100
27
+ rect , debug_filters = self .gate .analyze (frame , True )
28
+ debug_filter = debug_filters [- 1 ]
29
+ debug_filters = debug_filters [:- 1 ]
30
+
29
31
if self .prvs is None :
30
32
# frame = cv.resize(frame, None, fx=0.3, fy=0.3)
31
33
self .prvs = cv .cvtColor (frame , cv .COLOR_BGR2GRAY )
32
- else :
33
- if rect1 and rect2 :
34
- self .gate_center = self .get_center (rect1 , rect2 , frame )
34
+ else :
35
+ if rect [ 0 ] and rect [ 1 ] :
36
+ self .gate_center = self .get_center (rect [ 0 ], rect [ 1 ] , frame )
35
37
if self .use_optical_flow :
36
- cv .circle (debug_filter , self .gate_center , 5 , (3 , 186 , 252 ), - 1 )
38
+ cv .circle (debug_filter , self .gate_center , 5 , (3 ,186 ,252 ), - 1 )
37
39
else :
38
- cv .circle (debug_filter , self .gate_center , 5 , (0 , 0 , 255 ), - 1 )
39
-
40
+ cv .circle (debug_filter , self .gate_center , 5 , (0 ,0 , 255 ), - 1 )
41
+
40
42
if debug :
41
- return (self .output_class ( self . gate_center [0 ], self .gate_center [1 ]), [ frame , debug_filter ])
42
- return self . output_class (self .gate_center [0 ], self .gate_center [1 ])
43
+ return (self .gate_center [0 ], self .gate_center [1 ]), list ( debug_filters ) + [ debug_filter ]
44
+ return (self .gate_center [0 ], self .gate_center [1 ])
43
45
44
46
def center_without_optical_flow (self , center_x , center_y ):
45
47
# get starting center location, averaging over the first 2510 frames
46
48
if len (self .center_x_locs ) == 0 :
47
49
self .center_x_locs .append (center_x )
48
50
self .center_y_locs .append (center_y )
49
-
51
+
50
52
elif len (self .center_x_locs ) < 25 :
51
53
self .center_x_locs .append (center_x )
52
54
self .center_y_locs .append (center_y )
53
55
center_x = int (statistics .mean (self .center_x_locs ))
54
56
center_y = int (statistics .mean (self .center_y_locs ))
55
-
57
+
56
58
# use new center location only when it is close to the previous valid location
57
59
else :
58
60
self .center_x_locs .append (center_x )
@@ -61,11 +63,11 @@ def center_without_optical_flow(self, center_x, center_y):
61
63
self .center_y_locs .pop (0 )
62
64
x_temp_avg = int (statistics .mean (self .center_x_locs ))
63
65
y_temp_avg = int (statistics .mean (self .center_y_locs ))
64
- if math .sqrt ((center_x - x_temp_avg ) ** 2 + (center_y - y_temp_avg ) ** 2 ) > 10 :
66
+ if math .sqrt ((center_x - x_temp_avg )** 2 + (center_y - y_temp_avg )** 2 ) > 10 :
65
67
center_x , center_y = int (x_temp_avg ), int (y_temp_avg )
66
-
68
+
67
69
return (center_x , center_y )
68
-
70
+
69
71
def dense_optical_flow (self , frame ):
70
72
next_frame = cv .cvtColor (frame , cv .COLOR_BGR2GRAY )
71
73
flow = cv .calcOpticalFlowFarneback (self .prvs , next_frame , None , 0.5 , 3 , 15 , 3 , 5 , 1.2 , 0 )
@@ -82,53 +84,16 @@ def get_center(self, rect1, rect2, frame):
82
84
x2 , y2 , w2 , h2 = rect2
83
85
center_x , center_y = (x1 + x2 ) // 2 , ((y1 + h1 // 2 ) + (y2 + h2 // 2 )) // 2
84
86
self .prvs , mag , ang = self .dense_optical_flow (frame )
85
- # print(np.mean(mag))
86
- if len (self .center_x_locs ) < 25 or (np .mean (mag ) < 40 and ((not self .use_optical_flow ) or \
87
- (self .use_optical_flow and (
88
- center_x - self .gate_center [0 ]) ** 2 + (
89
- center_y - self .gate_center [
90
- 1 ]) ** 2 < 50 ))):
87
+
88
+ if len (self .center_x_locs ) < 25 or (np .mean (mag ) < 40 and ((not self .use_optical_flow ) or \
89
+ (self .use_optical_flow and (center_x - self .gate_center [0 ])** 2 + (center_y - self .gate_center [1 ])** 2 < 50 ))):
91
90
self .use_optical_flow = False
92
91
return self .center_without_optical_flow (center_x , center_y )
93
92
self .use_optical_flow = True
94
93
return (int (self .gate_center [0 ] + self .optical_flow_c * np .mean (mag * np .cos (ang ))), \
95
94
(int (self .gate_center [1 ] + self .optical_flow_c * np .mean (mag * np .sin (ang )))))
96
95
97
96
98
- # this part is temporary and will be covered by other files in the future
99
97
if __name__ == '__main__' :
100
- cap = cv .VideoCapture (sys .argv [1 ])
101
- ret_tries = 0
102
- start_time = time .time ()
103
- frame_count = 0
104
- paused = False
105
- speed = 1
106
- ret , frame1 = cap .read ()
107
- frame1 = cv .resize (frame1 , None , fx = 0.3 , fy = 0.3 )
108
- prvs = cv .cvtColor (frame1 , cv .COLOR_BGR2GRAY )
109
- hsv = np .zeros_like (frame1 )
110
- hsv [..., 1 ] = 255
111
- gate_center = GateCenter ()
112
- while ret_tries < 50 :
113
- for _ in range (speed ):
114
- ret , frame = cap .read ()
115
- if frame_count == 1000 :
116
- break
117
- if ret :
118
- frame = cv .resize (frame , None , fx = 0.3 , fy = 0.3 )
119
- center , filtered_frame = gate_center .analyze (frame , True )
120
- cv .imshow ('original' , frame )
121
- cv .imshow ('filtered_frame' , filtered_frame )
122
- ret_tries = 0
123
- key = cv .waitKey (30 )
124
- if key == ord ('q' ) or key == 27 :
125
- break
126
- if key == ord ('p' ):
127
- paused = not paused
128
- if key == ord ('i' ) and speed > 1 :
129
- speed -= 1
130
- if key == ord ('o' ):
131
- speed += 1
132
- else :
133
- ret_tries += 1
134
- frame_count += 1
98
+ from perception .vis .vis import run
99
+ run (['..\..\..\data\GOPR1142.MP4' ], GateCenterAlgo (), False )
0 commit comments