builtins.AttributeError: module 'cv2' has no attribute 'legacy'

I installed opencv-python version with pip3 and I get

builtins.AttributeError: module 'cv2' has no attribute 'legacy'

error when trying to use MOSSE tracker. If I go without legacy, then I’ll get

builtins.AttributeError: module 'cv2' has no attribute 'TrackerCSRT_create'

etc. errors according to combination of dots and underscores I use. Are the trackers (for MultiTracker in my case) still available or completely removed from OpenCV? If they are, then ghow can I use them? I’m using Python 3.8.10, here is the code 'm trying to use:

import sys
import cv2
import numpy as np
# set path for output video
video_out_path = '/path/video_out.mp4'
# set test viedo path
input_video = 'test_video2.mp4'
# dictionary for available trackers
trackers = {'csrt': cv2.legacy.TrackerCSRT_create,
                    'kcf' : cv2.legacy.TrackerKCF_create, # KCF might be worth trying and comparing to MOSSE
                    'boosting' : cv2.legacy.TrackerBoosting_create,
                    'mil': cv2.legacy.TrackerMIL_create,
                    'tld': cv2.legacy.TrackerTLD_create,
                    'medianflow': cv2.legacy.TrackerMedianFlow_create,
# create OpenCV MultiTracker instance
tracker = cv2.MultiTracker()
# create OpenCV VideoCapture instance for reading the test video
cap = cv2.VideoCapture(input_video)
# get the first frame
ret, frame = cap.read()
# exit if frame capture does not work
if not ret:
    print('The first frame could not be captured.')
size  = (cap.get(3),cap.get(4))
# create OpenCV VideoWriter instance for writing output video with the tracking rectangles
video_out = cv2.VideoWriter(video_out_path, cv2.VideoWriter_fourcc('*mp4'), 25.0, size)
# number of dancers in the test video
nbr_of_dancers = 4
for dancer in range(nbr_of_dancers):
    #cv2.imshow('Frame', frame)
    # region of interest is the whole frame for all the dancers
    bbox = cv2.selectROI('dance', frame)
    # MOSSE has a high trakcing speed and it is good at continuing with lost objects. It is also prone to continuing 
    # with objects no longer visible in the frame. however, it is safe to assume that the dancers are visible in 
    # every frame during the performance, especially if the camera is above the dancers.
    MOSSE = trackers['mosse']()
    # add a Minimum Output Sum of Squared Error tracker to the MultiTracker instance 
    tracker.add(MOSSE, frame, bbox) ```

Feel free to point out additional errors in the code. Thanks!

to use the (legacy) trackers:

  • pip3 uninstall opencv-python
  • pip3 install opencv-contrib-python

Does not work.:

builtins.ModuleNotFoundError: No module named 'cv2'

pip list or pip3 list please

also: how many pythons do you have? are there any virtual environments? what editor/IDE do you use?

I got it working after a reinstall and a restart. Thank you and sorry for taking your time.