我一直在尝试实现用于目标跟踪的OpenCv CSRT算法。下面是代码。
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/那拿到了代码。但除了从命令行运行之外,他们还实现了网络摄像头和视频捕获选项的代码。但我只是想在我的空闲时间跑一跑,看看录像带的选项。因此,我修改了一点,以便在空闲状态下运行。但是当我运行它时,我得到了以下错误
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' 你能给我指出正确的方向吗?
发布于 2021-06-10 14:36:53
函数.read()返回一个布尔值(True/False)和一个帧(一个数组)。如果帧被正确读取,它将是True。因此,您应该将frame = vs.read()更改为_, frame = vs.read(),如下所示(我希望如果没有其他错误,它应该可以工作):
...
while True:
_, frame = vs.read()
frame = imutils.resize(frame, width=500)
(H, W) = frame.shape[:2]
...https://stackoverflow.com/questions/67913283
复制相似问题