ValueError:“图像”必须具有 3 或 4 维。在科拉布

问题描述 投票:0回答:9

我在 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
python tensorflow object-detection
9个回答
2
投票

验证您是否从以下行获取图像帧:

ret, frame = cap.read()

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


1
投票
cap = cv2.VideoCapture(0)

尝试用 0、1、2 之间的不同值聆听......对我有用。


1
投票

我的根本原因略有不同,但有相同的错误,

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]

不再有错误。


0
投票

也许你应该尝试这个...你刚刚从你的网络摄像头收到错误,特别是你的网络摄像头和你的系统之间存在滞后,解决方案是你需要将你的代码

cv2.waitKey(1) & 0xFF == ord('q'):
更改为
key == ord('q'):
并且之前如果你应该添加
key = cv2.waitKey(1) & 0xFF
并在你的行末尾添加这个
cap.release()
和这个
cv2.destroyAllWindows()


0
投票

注意:如果您使用 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:[email protected]: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:[email protected]:554/1/1")
        print("Reconnected!")
        continue

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

您现在可以毫无问题地使用这个应用程序了。

我希望这对你有帮助。


0
投票

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

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

重新启动内核 & ta-da....


0
投票

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


0
投票

我遇到了这个错误,我忘记了解码步骤


-2
投票

那么让我解释一下。这不是任何错误,它只是笔记本电脑的网络摄像头和访问它的编程之间存在滞后。只需重新启动您的笔记本电脑即可。它会工作得很好。我遇到了同样的问题...然后重新启动就解决了。

© www.soinside.com 2019 - 2024. All rights reserved.