1

I am trying to classify object captured from my embedded webcam on my laptop using python. Below are the code that I modified from this link http://www.pyimagesearch.com/2016/08/10/imagenet-classification-with-python-and-keras/

from keras.preprocessing import image as image_utils
from imagenet_utils import decode_predictions
from imagenet_utils import preprocess_input
from squeezenet import squeeze
import numpy as np
import cv2

camera = cv2.VideoCapture(0)

while True:
    ret, frame = camera.read()
    print("[INFO] loading and preprocessing image...")
    image = image_utils.load_img(camera)
    image = image_utils.img_to_array(image)

    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)

    print("[INFO] loading network...")
    model = squeeze(weights = "imagenet")

    #classify the image
    print("[INFO] classifying image...")
    preds = model.predict(image)
    (inID, label) = decode_predictions(preds)[0]

    print("ImageNet ID: {}, Label: {}".format(inID, label))
    cv2.putText(orig, "Label: {}".format(label), (10, 30),
        cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
    cv2.imshow("Classification", camera)
    cv2.waitKey(0)

camera.release()
cv2.destroyAllWindows()    

I am new in classifying image/object using python so forgive me if this modified code looks silly.

I run the code and it return a type error like this

Using Theano backend.
Using gpu device 0: GeForce 920MX (CNMeM is disabled, cuDNN 5005)
[INFO] loading and preprocessing image...
Traceback (most recent call last):
  File "camdetect.py", line 13, in <module>
    image = image_utils.load_img(camera)
  File "/usr/local/lib/python2.7/dist-packages/keras/preprocessing/image.py", line 165, in load_img
img = Image.open(path)
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2285, in open
fp = io.BytesIO(fp.read())
TypeError: 'tuple' does not have the buffer interface

I tried searching about the error but did not find any answer.

My question is how to classify object captured from the webcam? and if there is any suggestion on how I can rewrite this code or maybe solve the error will be good too.

Thanks in advance.

Ling
  • 891
  • 5
  • 17
  • 40

1 Answers1

0

The error lies as in reported message:

image = image_utils.load_img(camera)

The fix could be:

# image = image_utils.load_img(camera)
image = image_utils.img_to_array(frame)
kuskus
  • 176
  • 7