这可能是一个愚蠢的问题,但我是机器学习和Tensorflow的新手。我尝试使用Tensorflow Lite在树莓派上运行对象检测API。我尝试在此示例的帮助下修改代码
https://github.com/freedomtan/tensorflow/blob/deeplab_tflite_python/tensorflow/contrib/lite/examples/python/object_detection.py
这段代码将从图像中检测物体。但我想通过Pi相机真实的检测物体,而不是图像。我试图修改这段代码,从相机而不是图像中读取输入。以下是我的代码-
import numpy as np
from tensorflow.contrib.lite.python import interpreter as interpreter_wrapper
import cv2
cap = cv2.VideoCapture(0)
ret, image_np = cap.read()
PATH_TO_MODEL = "ssd_mobilenet_v1_coco.tflite"
interpreter = tf.contrib.lite.Interpreter(model_path=PATH_TO_MODEL)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
while True:
# NxHxWxC, H:1, W:2
height = input_details[0]['shape'][1]
width = input_details[0]['shape'][2]
ret, image_np = cap.read()
image_np_expanded = np.expand_dims(image_np, axis=0)
#if floating_model:
image_np_expanded = (np.float32(image_np_expanded) - input_mean) / input_std
#HERE I AM GETTING ERROR
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
但我得到了这个错误
Traceback (most recent call last):
File "New_object_detection.py", line 257, in <module>
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)
File "/home/saurabh/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/interpreter.py", line 151, in set_tensor
self._interpreter.SetTensor(tensor_index, value)
File "/home/saurabh/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 133, in SetTensor
return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_SetTensor(self, i, value)
ValueError: Cannot set tensor: Dimension mismatch
任何人都可以请告诉我如何修复这个错误或建议教程相同?
1条答案
按热度按时间qzwqbdag1#
许多基于图像的机器学习模型都是使用固定大小的输入来训练的,原始图像可能有不同的尺寸,但会被调整到固定大小(例如224x224x3)。
因此,在输入到模型之前,你需要调整图像的大小,因为训练数据也是从不同的大小调整的,所以这样做可能效果很好。
正如上面的评论已经指出的,
cv.resize
可以做到这一点。