0

我已使用链接中的代码并成功完成了检测,但问题是它仅来自网络摄像头。我试图修改代码,以便它可以从文件中读取。我修改的部分是:我写了这个

print("[INFO] starting video stream...")
vs= cv2.VideoCapture('cars.avi')
time.sleep(2.0)
fps = FPS().start()
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()

而不是这个(来自上面链接的代码)

print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)
fps = FPS().start()
# loop over the frames from the video stream
while True:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()

为了从终端运行程序,我在这两种情况下都使用了这个命令:

python real_time_object_detection.py  --prototxt 
MobileNetSSD_deploy.prototxt.txt  --model MobileNetSSD_deploy.caffemodel

从文件读取时出现的错误是

我得到的错误是:

C:\Users\DEBASMITA\AppData\Local\Programs\Python\Python35\real-time-object-
detection>python videoobjectdetection.py  --prototxt 
MobileNetSSD_deploy.prototxt.txt  --model MobileNetSSD_deploy.caffemodel
[INFO] loading model...
Traceback (most recent call last):
  File "videoobjectdetection.py", line 54, in <module>
    frame = imutils.resize(frame, width=400)
  File "C:\Users\DEBASMITA\AppData\Local\Programs\Python\Python35\lib\site-
packages\imutils\convenience.py", line 69, in resize
    (h, w) = image.shape[:2]
AttributeError: 'tuple' object has no attribute 'shape' 

我不知道我在哪里做错了。请指导我。

4

1 回答 1

0

我不熟悉您引用的任何代码,但是错误很简单,并且在其他问题中已经回答了类似的错误:您正在尝试对普通的元组对象执行一种奇特的方法。这是一个使用通用包 numpy 用于数组的 Python 概念示例:

#an example of the error you are getting with a plain tuple
>>>tup = (1,2,3,4)
>>>len(tup)
4
>>> tup.shape
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'shape'

#an example that uses an attribute called 'shape'
>>> import numpy as np
>>> x = np.array([1,2,3,4])
>>> x.shape
(4,)
>>> x.shape.shape
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'shape'

正如您在我的最后两行中看到的,我第一次调用.shapenumpy 数组时,调用是有效的。这个调用返回一个元组,所以最后一次调用.shape.shape是无效的,它在(4,). 至于怎么修?我不知道。例如,在这个问题中,原始发布者认为他们正在取回某种图像对象,而不是他们得到一个元组(可能是图像对象的元组)。类似的事情发生在你身上:你的VideoStream.read()电话正在返回一个元组。因此,当您调用时,imutils.resize(frame, width=400)您传递的是一个元组,而不是图像或框架。因此,当该方法尝试调用.shape您时,您会收到错误消息。VideoStream.read()可能会通过设计或错误条件返回元组。您必须阅读 VideoStream 才能确定。

于 2018-03-16T07:48:16.200 回答