我正在使用OpenCV MultiTracking实现。实时运行的演示效果很好,但是当我重写检测更新以进行回调时,它开始表现得很奇怪。这是我的检测回调:
def detectionCallback(self,data):
self.tracker = cv2.TrackerKCF_create()
self.trackers = cv2.MultiTracker_create()
detectionFrame = self.frame
for i, item in enumerate(data.data):
print("Iteration:{}".format(i))
box = (item.x, item.y, item.width, item.height)
print("Current box:",box)
self.trackers.add(self.tracker, detectionFrame, box)
(success, boxes) = self.trackers.update(detectionFrame)
print("Boxes post-update:",boxes)
#boxes = np.unique(boxes, axis = 0)
#print(boxes)
for box in boxes:
(x, y, w, h) = [int(v) for v in box]
cv2.rectangle(self.frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
self.detectionCounter += 1
self.prBoxes = boxes
print("detection %d" %self.detectionCounter)
我正在使用detectionFrame
以避免在更新检测期间进行帧更新。我的意思是确保在同一帧上执行trackers.add
和trackers.update
。所以其余的基本上与教程中的一样[like this one)
所以您可以在输出中看到问题:
Iteration:0
('Current box:', (317, 276, 17, 12))
Iteration:1
('Current box:', (351, 275, 16, 11))
Iteration:2
('Current box:', (375, 275, 16, 10))
Iteration:3
('Current box:', (0, 291, 28, 52))
Iteration:4
('Current box:', (0, 270, 82, 51))
Iteration:5
('Current box:', (222, 281, 23, 15))
Iteration:6
('Current box:', (243, 280, 28, 16))
Iteration:7
('Current box:', (518, 239, 18, 15))
Iteration:8
('Current box:', (308, 248, 14, 19))
Iteration:9
('Current box:', (481, 254, 21, 10))
('Boxes post-update:', array([[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.],
[317., 276., 17., 12.]]))
detection 1
我不确定trackers.update
是做什么的,但是结果对我来说看起来不正确,有什么建议吗?
对于那些可能有相同问题的人,答案很简单:应该为每个检测到的对象创建对象tracker
,因此可以解决问题:
def detectionCallback(self,data):
self.trackers = cv2.MultiTracker_create()
detectionFrame = self.frame
for i, item in enumerate(data.data):
print("Iteration:{}".format(i))
self.tracker = cv2.TrackerKCF_create()
box = (item.x, item.y, item.width, item.height)
print("Current box:",box)
self.trackers.add(self.tracker, detectionFrame, box)
(success, boxes) = self.trackers.update(detectionFrame)
print("Boxes post-update:",boxes)
...
the rest is the same