python OpenCV属性错误模块“cv2.cv2”没有属性“Tracker_create”

ma8fv8wu  于 2023-01-29  发布在  Python
关注(0)|答案(6)|浏览(715)

我已经尝试运行此代码,但得到一个属性错误。任何帮助将不胜感激。

import cv2
    import sys
     
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    
    if __name__ == '__main__':
 
    # Set up tracker.
    # Instead of MIL, you can also use
 
    tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'CSRT', 'MOSSE']
    tracker_type = tracker_types[5]
 
    if int(minor_ver) < 3:
        tracker = cv2.cv2.Tracker_create(tracker_type)
    else:
        if tracker_type == 'BOOSTING':
            tracker = cv2.TrackerBoosting_create()
        if tracker_type == 'MIL':
            tracker = cv2.TrackerMIL_create()
        if tracker_type == 'KCF':
            tracker = cv2.TrackerKCF_create()
        if tracker_type == 'TLD':
            tracker = cv2.TrackerTLD_create()
        if tracker_type == 'MEDIANFLOW':
            tracker = cv2.TrackerMedianFlow_create()
        if tracker_type == 'CSRT':
            tracker = cv2.TrackerCSRT_create()
        if tracker_type == 'MOSSE':
            tracker = cv2.TrackerMOSSE_create()
 
    # Read video
    video = cv2.VideoCapture("./videos/chaplin.mp4")
 
    # Exit if video not opened.
    if not video.isOpened():
        print("Could not open video")
        sys.exit()
 
    # Read first frame.
    ok, frame = video.read()
    if not ok:
        print('Cannot read video file')
        sys.exit()
     
    # Define an initial bounding box
    bbox = (287, 23, 86, 320)
 
    # Uncomment the line below to select a different bounding box
    bbox = cv2.selectROI(frame, False)
 
    # Initialize tracker with first frame and bounding box
    ok = tracker.init(frame, bbox)
 
    while True:
        # Read a new frame
        ok, frame = video.read()
        if not ok:
            break
         
        # Start timer
        timer = cv2.getTickCount()
 
        # Update tracker
        ok, bbox = tracker.update(frame)
 
        # Calculate Frames per second (FPS)
        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);
 
        # Draw bounding box
        if ok:
            # Tracking success
            p1 = (int(bbox[0]), int(bbox[1]))
            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
            cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
        else :
            # Tracking failure
            cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)
 
        # Display tracker type on frame
        cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
     
        # Display FPS on frame
        cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);
 
        # Display result
        cv2.imshow("Tracking", frame)
 
        # Exit if ESC pressed
        k = cv2.waitKey(1) & 0xff
        if k == 27 : break
    • 输出:**
Traceback (most recent call last):
  File "C:\Users\Jatin\OpenCV-Object-Tracking\index.py", line 15, in <module>
    tracker = cv2.cv2.Tracker_create(tracker_type)
AttributeError: module 'cv2.cv2' has no attribute 'Tracker_create'
7tofc5zh

7tofc5zh1#

这取决于您安装的OpenCV包和版本。
我相信您需要**OpenCV 3.4+**才能运行这些模型。一些跟踪器模型在3.2和3.3中可用。脚本中的所有跟踪器在3.4中可用
OpenCV软件包:opencv-python:此存储库包含OpenCV库的主要模块。
opencv-contrib-python:opencv-contrib-python存储库包含主模块沿着contrib模块
python -m pip install opencv-contrib-python,检查一下你是否有3.4+,与pip show opencv
有关详细信息,请参见how to install opencv

更新

正如@user48956所指出的,opencv v 4.5.x已经将其中的一些算法转移到了cv2.legacy中,例如,要访问TrackerMOSSE_create函数,你必须从cv2.legacy.TrackerMOSSE_create中获得它。
我建议继续使用opencv GitHub,因为一些函数/算法可能会移动或删除。

qkf9rpyu

qkf9rpyu2#

如果您使用的是OpenCV 4或更高版本,则上述代码可能无法正常工作。
if int(minor_ver) < 3:用于OpenCV 4的换行符
对于此实验,您只需要安装opencv contrib

我的虚拟环境包含

pip install -r requirements.txt
要求. txt

dlib==19.19.0
imutils==0.5.3
numpy==1.18.4
opencv-contrib-python==4.2.0.34
scipy==1.4.1

修改代码,兼容OpenCV 4版本

tracker.py 例如

ghg1uchk

ghg1uchk3#

至少用于:

  • 打开控件-Python==4.5.2.52

您需要替换:

cv2.TrackerMOSSE_create()

和...

cv2.legacy.TrackerMOSSE_create()
hs1rzwqc

hs1rzwqc4#

我收到以下错误:tracker = cv2.TrackerMOSSE.create()
甚至以为我已经安装了:开放式控件-Python
我可以使用以下代码来修复错误:tracker = cv2.legacy_TrackerMOSSE.create()

zf9nrax1

zf9nrax15#

我的Python版本是3.7,它似乎没有忽略下面的代码。

if int(minor_ver) < 3:
        tracker = cv2.cv2.Tracker_create(tracker_type)

我只是简单地删除它和模块运行。
这是一个无关但希望有用的建议。在上面代码的末尾,我添加了一个'destroyAllWindows()命令,它可以清理查看器:

if k == 27:
            cv2.destroyAllWindows()
            break
0yg35tkg

0yg35tkg6#

只需安装以下组件。

pip install opencv-contrib-python

这里通过使用CSRT跟踪器。

import cv2

cap = cv2.VideoCapture("video_path")
tracker = cv2.TrackerCSRT_create()
ret, frame = cap.read()
bbox = cv2.selectROI(frame, False)
tracker.init(frame, bbox)

while True:   
    frame_id = 0 
    ret, frame = cap.read() 
    success, bbox = tracker.update(frame)    
    if success:
        x, y, w, h = [int(i) for i in bbox]
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)    
    cv2.imshow("Tracking", frame)    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

相关问题