【问题标题】:How to use caffe convnet for object detection in video frames?如何使用 caffe convnet 进行视频帧中的对象检测?
【发布时间】:2018-08-25 03:39:18
【问题描述】:

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

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' 

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

【问题讨论】:

  • 查看两个应用程序中 vs 的格式并找出不同之处。
  • 你能解释一下吗?我是该领域的新手,并将其作为我的 mtech 项目进行
  • 对不起,我的意思是:查看两个应用程序中“框架”的格式并找出差异。您可能无法期望 VideoStream 和 cv2.VideoCapture 提供具有相同属性/格式的图像。
  • 好的..检查一下
  • Micka 我的问题解决了。是的,问题出在视频捕捉中。我必须使用 FileVideoStream 而不是 VideoStream 并完成

标签: python opencv deep-learning object-detection video-tracking


【解决方案1】:

我不熟悉您引用的任何代码,但错误很简单,other questions 中已经回答了类似的错误:您正在尝试对一个普通的元组对象执行一种奇特的方法。下面是一个使用通用包 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'

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

【讨论】:

  • 我的问题解决了。是的,问题出在视频捕捉中。我必须使用 FileVideoStream 而不是 VideoStream 及其完成。谢谢
  • @DebasmitaBhoumik 很好,如果我的回答有帮助,请接受。
猜你喜欢
  • 2023-04-04
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-29
相关资源
最近更新 更多