我正在尝试使用python对笔记本电脑上嵌入的网络摄像头捕获的对象进行分类。下面是我从这个链接修改的代码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()
我是使用python对图像/对象进行分类的新手,所以如果修改后的代码看起来很傻,请原谅我。
我运行代码,它返回一个类型错误,像这样
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
我试着搜索这个错误,但是没有找到任何答案。
我的问题是如何分类从网络摄像头捕获的对象?如果有任何关于我如何重写这段代码或解决错误的建议也会很好。
错误如下:
image = image_utils.load_img(camera)
修复方法可以是:
# image = image_utils.load_img(camera)
image = image_utils.img_to_array(frame)