值错误:"图像"必须具有 3 或 4 维。(科拉布)



我在Google Colab中使用tensorflow进行对象检测。我在试着从网络摄像头里获取视频。这是最后一个阶段。但是我在大陆下面得到了误差。我该如何给图片定大小?

ValueError: in user code:
<ipython-input-49-1e7efe9130ee>:11 detect_fn  *
image, shapes = detection_model.preprocess(image)
/usr/local/lib/python3.7/dist-packages/object_detection/meta_architectures/ssd_meta_arch.py:484 preprocess  *
normalized_inputs, self._image_resizer_fn)
/usr/local/lib/python3.7/dist-packages/object_detection/utils/shape_utils.py:492 resize_images_and_return_shapes  *
outputs = static_or_dynamic_map_fn(
/usr/local/lib/python3.7/dist-packages/object_detection/utils/shape_utils.py:246 static_or_dynamic_map_fn  *
outputs = [fn(arg) for arg in tf.unstack(elems)]
/usr/local/lib/python3.7/dist-packages/object_detection/core/preprocessor.py:3241 resize_image  *
new_image = tf.image.resize_images(
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper  **
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/image_ops_impl.py:1468 resize_images
skip_resize_if_same=True)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/image_ops_impl.py:1320 _resize_images_common
raise ValueError(''images' must have either 3 or 4 dimensions.')
ValueError: 'images' must have either 3 or 4 dimensions.

如何解决?

所有代码:

while True: 
ret, frame = cap.read()
image_np = np.array(frame)

input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)

num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=5,
min_score_thresh=.5,
agnostic_mode=False)
cv2.imshow('object detection',  cv2.resize(image_np_with_detections, (800, 600)))

if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
break

验证您正在从以下行获取图像帧:

ret, frame = cap.read()

当我得到相同的错误时(尽管代码略有不同),我指向了一个不存在的目录,而不是一个图像。

cap = cv2.VideoCapture(0)

试着用0、1、2....之间的不同值来听

我有一个稍微不同的根本原因,但有相同的错误,

image_path_jpg = "path_to.jpg"
img = tf.io.read_file(image_path_jpg)
img_resized = tf.image.resize(img, [100, 100])

也产生了

ValueError: 'images' must have either 3 or 4 dimensions.

对于我来说,解决方案是我错过了解码步骤,

image_path_jpg = "path_to.jpg"
img = tf.io.read_file(image_path_jpg)
img.get_shape().as_list()  # []
img = tf.image.decode_jpeg(img)
img.get_shape().as_list()  # [300, 400, 3]
img_resized = tf.image.resize(img, [100, 100])
img_resized.get_shape().as_list()  # [100, 100, 3]

No more errors.

也许你应该试试这个…你刚刚从你的网络摄像头得到了错误,特别是你的网络摄像头和你的系统之间有延迟,解决方案是你需要改变你的代码cv2.waitKey(1) & 0xFF == ord('q'):key == ord('q'):,如果你应该添加key = cv2.waitKey(1) & 0xFF,在你的行末尾添加这个cap.release()和这个cv2.destroyAllWindows()

注意:如果您使用RTSP,则可能出现此问题。

我几乎在做一个车牌识别程序。我的问题和你差不多当程序运行时,它在开始几秒钟后就崩溃了当然,我在网上搜索了很多,但我没有得到任何地方。我把你能想到的相机设置都改了。我修改了整个代码,我试了很多次,终于发现了问题所在。

The problem with the RTSP protocol is that you should know that RTSP runs on the UDP platform and UDP has no warranty or, in other words, no responsibility for packets. They do not have to reach their destination completely. Unlike TCP, what happens is that you may not receive any frame while running your program.

这个错误到底告诉我们什么?它告诉我们,我希望收到3或4维的图像,但没有收到,或者更确切地说,它没有收到任何东西。

So you should be using Try and Except In Python For handling This Problem for if not frame captured App Does Not crash and It Does reconnect the RTSP.

这是你需要的:

cap = cv2.VideoCapture("rtsp://admin:admin@192.168.1.2:554/1/1")
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))  # Get video framerate
while True:
try:
ret, frame = cap.read()
if frame is None:
print("disconnected!")
image_np = np.array(frame)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes']+label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=5,
min_score_thresh=.8,
agnostic_mode=False)
#Extract Plate Shape On Entire Image
detection_thereshold = 0.7
image = image_np_with_detections
scores = list(filter(lambda x: x> detection_thereshold, detections['detection_scores']))
boxes = detections['detection_boxes'][:len(scores)]
classes = detections['detection_classes'][:len(scores)]
width = image.shape[1]
height = image.shape[0]
cv2.imshow('object detection',  cv2.resize(image_np_with_detections, (800, 600)))

if cv2.waitKey(10) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
except:
cap.release()
cap = cv2.VideoCapture("rtsp://admin:admin@192.168.1.2:554/1/1")
print("Reconnected!")
continue

在except部分,如您所见,我们重新创建了RTSP连接。

你现在可以使用这个应用程序没有问题。

我希望这对你有帮助。

我通过卸载OpenCV-python并重新安装来解决这个问题。

  • $pip uninstall opencv-python
  • $pip install opencv-python

重启内核&哈哈…

首先检查图像是否在此时被捕获image_np = np.array(frame)。因为它显示没有维度所以没有图像

我来解释一下。这不是什么错误,只是你笔记本电脑的网络摄像头和访问它的程序之间的延迟。重启你的笔记本电脑。没问题的。我也遇到过同样的问题……重新启动就解决了。

最新更新