Search code examples
pythonopencvdeep-learningobject-detectionvideo-tracking

How to use caffe convnet for object detection in video frames?


I have use codes from this link and sucessfully done the detection but the problem is it is only from webcam. I tried to modify the code so that it can read from file. the part I have modified is : I have written 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()

instead of this (code from the above link)

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()

For running the program from terminal I am using this command for both the cases:

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

The error I am getting when reading from file is

the error I am getting is :

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' 

I don't know where I am doing wrong. Please guide me.


Solution

  • I am unfamiliar with any of the code you are referencing, but the error is straightforward and similar errors hav been answered in other questions: You're trying to do a fancy method on a plain tuple object. Here's an example of this python concept using a common package, 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'
    

    As you can see in my last two lines, the first time I call .shape on the numpy array, the call is valid. This call returns a tuple, so the last call to .shape.shape is invalid, it is operating on (4,). As for how to fix it? I don't know. For example, in this question the original poster thought that they were getting back some kind of image object, instead they were getting a tuple (maybe a tuple of image objects). Something similar is happening to you: Your VideoStream.read() call is returning a tuple. So when you call imutils.resize(frame, width=400) you are passing in a tuple, not an image or frame. So when that method tries to call .shape you get the error. VideoStream.read() may return a tuple by design, or an error condition. You'd have to read up on VideoStream to be sure.