tensorflow ValueError:'images'必须有3或4个维度,在Colab中

ljo96ir5  于 2022-11-25  发布在  其他
关注(0)|答案(8)|浏览(290)

我在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
6psbrbz9

6psbrbz91#

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

ret, frame = cap.read()

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

atmip9wb

atmip9wb2#

cap = cv2.VideoCapture(0)

试着用0,1,2之间的不同值来听...对我的很有效。

p8h8hvxi

p8h8hvxi3#

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

vxqlmq5t

vxqlmq5t4#

注意:如果您使用RTSP,则可能会发生此问题。

我几乎是在一个车牌识别程序的工作。我有几乎相同的问题,你当程序运行,它崩溃后的第一个几秒钟。当然,我搜索了很多网页,但我没有得到任何结果。我做了任何更改相机设置,你能想到的。我改变了整个代码,我尝试了很多,最后意识到的问题。

一月一日
错误到底告诉我们什么?它告诉我们,我期待收到图像与3或4维,但没有收到它,或者说,它没有收到任何东西。
一个月一个月
以下是您需要的内容:

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连接。
您现在可以毫无问题地使用此应用程序。

"希望这对你有帮助"

mjqavswn

mjqavswn5#

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

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

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

v2g6jxz6

v2g6jxz66#

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

knsnq2tg

knsnq2tg7#

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

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]

没有其他错误。

2cmtqfgy

2cmtqfgy8#

所以让我来解释一下。这不是任何错误,它只是在你的笔记本电脑的摄像头和编程访问它之间的滞后。只要重新启动你的笔记本电脑。它会工作正常。我遇到了同样的问题...只是重新启动解决了它。

相关问题