I am trying to track an object over time using opencv’s object tracking features. I can successfully read a graphic with imread() and display it with imshow() and select a target with selectROI(). But when I try to create a tracker with cv2.TrackerBoosting_create() it fails with error:
AttributeError: module ‘cv2’ has no attribute ‘TrackerBoosting_create’
I tried uninstalling opencv-python in favor of just opencv-contrib-python but then it failed much earlier, at my call to cv2.imread().
My source code is:
import cv2
import backgroundTarget as bt
import os
import datetime
import numpy as np
def fileLoop():
fileList = sorted(os.listdir(path))
print("fileList = " + str(fileList))
# img = cv.imread('messi5.jpg', cv.IMREAD_GRAYSCALE)
# img = cv2.imread(path + fileList[0], cv2.IMREAD_COLOR)
frame = cv2.imread(path + fileList[0], cv2.IMREAD_COLOR)
fileLoopIndex = 0
startTimestamp = datetime.datetime.now()
# outfile.writelines(str(startTimestamp))
for currFile in fileList:
name, ext = os.path.splitext(currFile)
if ext == '.jpg':
# img = cv2.imread(path + fileList[fileLoopIndex], cv2.IMREAD_COLOR)
img = cv2.imread(path + fileList[fileLoopIndex], cv2.IMREAD_GRAYSCALE)
# img = cv2.imread(path + fileList[fileLoopIndex], cv2.IMREAD_COLOR)
cv2.imshow("image", img)
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
bbox = cv2.selectROI(img, False)
tracker_type = 'BOOSTING'
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
print(cv2.__version__)
if int(major_ver) < 4 and int(minor_ver) < 3:
tracker = cv2.cv2.Tracker_create(tracker_type)
else:
if tracker_type == 'BOOSTING':
tracker = cv2.TrackerBoosting_create()
else:
print("else clause")
# Initialize tracker with first frame and bounding box
ok = tracker.init(img, bbox)
print(ok)
# Define the tracking parameters
tracker = cv2.TrackerKCF_create()
cv2.waitKey(0)
fileLoopIndex += 1
return img
path = "../../Desktop/winter field/5 side/"
img = fileLoop()
cv2.destroyAllWindows()