如何在视频帧中使用caffe convnet进行对象检测?

时间:2018-03-16 06:30:55

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

我使用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' 

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

1 个答案:

答案 0 :(得分:0)

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

#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才能确定。