@@ -129,6 +129,7 @@ def run(self):
129129 if self .is_save is not None :
130130 self .belt_video = cv2 .VideoWriter (os .path .join (self .results_dir , 'belt.avi' ), cv2 .VideoWriter_fourcc (* 'MJPG' ), 30 , (int (self .bbox_belt [2 ]), int (self .bbox_belt [3 ])))
131131 self .cc_video = cv2 .VideoWriter (os .path .join (self .results_dir , 'cc.avi' ), cv2 .VideoWriter_fourcc (* 'MJPG' ), 30 , (int (self .bbox_belt [2 ]), int (self .bbox_belt [3 ])))
132+ self .track_video = cv2 .VideoWriter (os .path .join (self .results_dir , 'track.avi' ), cv2 .VideoWriter_fourcc (* 'MJPG' ), 30 , (int (self .bbox_belt [2 ]), int (self .bbox_belt [3 ])))
132133
133134 while True :
134135 ret , frame = self .camera .read ()
@@ -166,10 +167,6 @@ def run(self):
166167 # self.components_centroids = components_centroids
167168
168169 # TODO: 对零件特征点使用光流法来跟踪,目前直接对连通域的坐标进行卡尔曼滤波
169- for candidate in self .candidates :
170- # 处理物体消失
171- if frame_id - candidate .last .timestamp > 10 or candidate .last .center .y < 30 :
172- self .candidates .remove (candidate )
173170 # 遍历当前连通域,与候选目标队列的连通域进行匹配
174171 for i in range (components_num_labels ):
175172 x , y , w , h , area = components_stats [i ]
@@ -192,14 +189,24 @@ def run(self):
192189 if candidate .last .timestamp < frame_id and overlap > 0.8 :
193190 matched = True
194191 # 当x坐标基本不变,y坐标在减小,并且面积基本不变时,认为跟踪成功
195- if abs (dx ) < 3 and dy < 0 and abs ( candidate . last . area - area ) < 0.1 * area :
192+ if abs (dx ) < 3 and dy < 0 :
196193 candidate .update (cur_info )
197194 break
198195
199- # 如果不属于已有物体,则加入候选目标队列
200- if not matched :
196+ # 如果不属于已有物体,并且在检测区域的下半部分,才加入候选目标队列
197+ if ( not matched ) and y > belt . shape [ 0 ] / 3 :
201198 self .candidates .append (TargetTrack (cur_info ))
202199
200+ # 过期检查
201+ for candidate in self .candidates :
202+ if candidate .last .rect .y < 30 :
203+ self .__logger .info ("目标已经离开传送带,目标信息:{}" .format (candidate .last ))
204+ self .candidates .remove (candidate )
205+ continue
206+ # 处理过期物体
207+ if frame_id - candidate .last .timestamp > 3 :
208+ self .candidates .remove (candidate )
209+
203210 # TODO: 卡尔曼滤波
204211 if self .is_show :
205212 # 形态学处理的结果
@@ -227,14 +234,19 @@ def run(self):
227234 # 绘制矩形
228235 cv2 .rectangle (candidates_belt , (candidate .last .rect .x , candidate .last .rect .y ), (candidate .last .rect .x + candidate .last .rect .w , candidate .last .rect .y + candidate .last .rect .h ), (0 , 255 , 0 ), 2 )
229236 # 显示连通域编号,中心点坐标,面积,时间戳
230- # cv2.putText(candidates_belt, str(candidate.last.timestamp) + ": (" + str(candidate.last.center.x) + ", " + str(candidate.last.center.y) + ")", (candidate.last.rect.x, candidate.last.rect.y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
231- cv2 .putText (candidates_belt , "{0}: ({1:.2f}, {2:.2f})" .format (candidate .last .timestamp , candidate .last .center .x , candidate .last .center .y ), (candidate .last .rect .x , candidate .last .rect .y ), cv2 .FONT_HERSHEY_SIMPLEX , 1 , (0 , 0 , 255 ), 2 )
237+ cv2 .putText (candidates_belt , "{0}: ({1:.2f}, {2:.2f})" .format (candidate .last .timestamp , candidate .last .center .x , candidate .last .center .y ), (candidate .last .rect .x , candidate .last .rect .y ), cv2 .FONT_HERSHEY_PLAIN , 1 , (0 , 0 , 255 ), 2 )
238+ cv2 .putText (candidates_belt , "{0}x{1}, {2}" .format (candidate .last .rect .w , candidate .last .rect .h , candidate .last .area ), (int (candidate .last .center .x ), int (candidate .last .center .y )), cv2 .FONT_HERSHEY_PLAIN , 1 , (0 , 0 , 255 ), 2 )
239+ # 显示坐标和速度
240+ c_x , c_y = candidate .get_position ()
241+ v_x , v_y = candidate .get_velocity ()
242+ cv2 .putText (candidates_belt , "({0:.2f}, {1:.2f}), ({2:.2f}, {3:.2f})" .format (c_x , c_y , v_x , v_y ), (candidate .last .rect .x , candidate .last .rect .y + candidate .last .rect .h ), cv2 .FONT_HERSHEY_PLAIN , 1 , (0 , 0 , 255 ), 2 )
232243 cv2 .namedWindow ("Candidates Belt" , cv2 .WINDOW_NORMAL )
233244 cv2 .imshow ("Candidates Belt" , candidates_belt )
234245
235246 if self .is_save :
236247 self .belt_video .write (belt )
237248 self .cc_video .write (selected_belt )
249+ self .track_video .write (candidates_belt )
238250
239251
240252 keyboard = cv2 .waitKey (30 )
0 commit comments