我正在尝试实现本教程(https://www.learnopencv.com/multitracker-multiple-object-tracking-using-opencv-c-python/)中所述的OpenCV的多跟踪器,但是我遇到了一些有关OpenCV版本的问题。 如关于SO的其他答案所述,对于单跟踪器和多跟踪器构造函数,我必须使用opencv-contrib;但是,一旦安装了opencv-contrib代替opencv,我将无法再使用VideoCapture功能。由于需要两个函数,所以我陷入了僵局,但是contrib-opencv和opencv似乎不能在同一解释器中共存。 有谁遇到过同样的问题,可以帮助我吗?
import sys
import cv2
from random import randint
trackerTypes = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
def createTrackerByName(trackerType):
# Create a tracker based on tracker name
if trackerType == trackerTypes[0]:
tracker = cv2.TrackerBoosting_create()
elif trackerType == trackerTypes[1]:
tracker = cv2.TrackerMIL_create()
elif trackerType == trackerTypes[2]:
tracker = cv2.TrackerKCF_create()
elif trackerType == trackerTypes[3]:
tracker = cv2.TrackerTLD_create()
elif trackerType == trackerTypes[4]:
tracker = cv2.TrackerMedianFlow_create()
elif trackerType == trackerTypes[5]:
tracker = cv2.TrackerGOTURN_create()
elif trackerType == trackerTypes[6]:
tracker = cv2.TrackerMOSSE_create()
elif trackerType == trackerTypes[7]:
tracker = cv2.TrackerCSRT_create()
else:
tracker = None
print('Incorrect tracker name')
print('Available trackers are:')
for t in trackerTypes:
print(t)
return tracker
def main():
# Set video to load
videoPath = "../../data/videos/case-1-2D.avi"
# Create a video capture object to read videos
cap = cv2.VideoCapture(videoPath)
# Read first frame
success, frame = cap.read()
# quit if unable to read the video file
if not success:
print('Failed to read video')
sys.exit(1)
## Select boxes
bboxes = []
colors = []
while True:
bbox = cv2.selectROI('MultiTracker', frame)
bboxes.append(bbox)
colors.append((randint(0, 255), randint(0, 255), randint(0, 255)))
print("Press q to quit selecting boxes and start tracking")
print("Press any other key to select next object")
k = cv2.waitKey(0) & 0xFF
if (k == 113): # q is pressed
break
print('Selected bounding boxes {}'.format(bboxes))
# Specify the tracker type
trackerType = "CSRT"
# Create MultiTracker object
multiTracker = cv2.MultiTracker_create()
# Initialize MultiTracker
for bbox in bboxes:
multiTracker.add(createTrackerByName(trackerType), frame, bbox)
# Process video and track objects
while cap.isOpened():
success, frame = cap.read()
if not success:
break
# get updated location of objects in subsequent frames
success, boxes = multiTracker.update(frame)
# draw tracked objects
for i, newbox in enumerate(boxes):
p1 = (int(newbox[0]), int(newbox[1]))
p2 = (int(newbox[0] + newbox[2]), int(newbox[1] + newbox[3]))
cv2.rectangle(frame, p1, p2, colors[i], 2, 1)
# show frame
cv2.imshow('MultiTracker', frame)
# quit on ESC button
if cv2.waitKey(1) & 0xFF == 27: # Esc pressed
break
if __name__ == '__main__':
main()
如果我使用opencv-python软件包,则会收到错误消息:
AttributeError: module 'cv2.cv2' has no attribute
'MultiTracker_create'
如果我使用opencv-contrib-python软件包,则会收到错误消息:
AttributeError: module 'cv2' has no attribute
'VideoCapture'
如果有帮助,我正在使用适用于我的Python解释器的VirtualEnv在MacOS上使用PyCharm。 我已经尝试安装opencv-contrib(单独),opencv(单独),将opencv-contrib和opencv都保留在我的解释器中,将opencv降级到应该支持我需要的功能的早期版本(opencv 3.4)。