首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(h,w) = image.shape[:2] AttributeError:'tuple‘对象没有'shape’属性

(h,w) = image.shape[:2] AttributeError:'tuple‘对象没有'shape’属性
EN

Stack Overflow用户
提问于 2021-06-10 08:52:10
回答 1查看 312关注 0票数 0

我一直在尝试实现用于目标跟踪的OpenCv CSRT算法。下面是代码。

代码语言:javascript
复制
from imutils.video import VideoStream
from imutils.video import FPS
import argparse
import imutils
import time
import cv2
file1 = 'traffic.mp4'

tracker = cv2.TrackerCSRT_create()
initBB = None
vs = cv2.VideoCapture(file1)
fps = None

while True:
    frame = vs.read()
    
    if frame is None:
        break

    frame = imutils.resize(frame, width=500)
    (H, W) = frame.shape[:2]

    if initBB is not None:
        (success, box) = tracker.update(frame)
        if success:
            (x, y, w, h) = [int(v) for v in box]
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        
        fps.update()
        fps.stop()
        
        info = [
            ("Tracker", "CSRT"),
            ("Success", "Yes" if success else "No"),
            ("FPS", "{:.2f}".format(fps.fps())),
        ]
    
        for (i, (k, v)) in enumerate(info):
            text = "{}: {}".format(k, v)
            cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    
    if key == ord("s"):
        initBB = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
        tracker.init(frame, initBB)
        fps = FPS().start()

    
    elif key == ord("q"):
        break
        
vs.release()
cv2.destroyAllWindows()

我从https://www.pyimagesearch.com/那拿到了代码。但除了从命令行运行之外,他们还实现了网络摄像头和视频捕获选项的代码。但我只是想在我的空闲时间跑一跑,看看录像带的选项。因此,我修改了一点,以便在空闲状态下运行。但是当我运行它时,我得到了以下错误

代码语言:javascript
复制
line 20, in <module> frame = imutils.resize(frame, width=500)
  File "C:\Python37\lib\site-packages\imutils\convenience.py", line 69, in resize
    (h, w) = image.shape[:2]
AttributeError: 'tuple' object has no attribute 'shape' 

你能给我指出正确的方向吗?

EN

回答 1

Stack Overflow用户

发布于 2021-06-10 14:36:53

函数.read()返回一个布尔值(True/False)和一个帧(一个数组)。如果帧被正确读取,它将是True。因此,您应该将frame = vs.read()更改为_, frame = vs.read(),如下所示(我希望如果没有其他错误,它应该可以工作):

代码语言:javascript
复制
...

while True:
    _, frame = vs.read()
      
    frame = imutils.resize(frame, width=500)
    (H, W) = frame.shape[:2]
    ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67913283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档