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

I do object detection with tensorflow in Google Colab. I'm trying to get video from the webcam. This is the last stage. But I am getting the error below continent.How can I size the pictures?

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.

How can i solve?

All Code:

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


Solution 1:[1]

cap = cv2.VideoCapture(0)

Try to listen with different values ranging between 0,1,2.... Worked for mine.

Solution 2:[2]

Verify that you are getting an image frame from the following line:

ret, frame = cap.read()

When I got the same error (albeit slightly different code), I was pointing to a non-existent directory rather than an image.

Solution 3:[3]

maybe u should try this...u just got error from ur webcam, as specifically u got lag between ur webcam and ur system, and the solution is u need to change ur code cv2.waitKey(1) & 0xFF == ord('q'): to key == ord('q'): and before if u should add key = cv2.waitKey(1) & 0xFF and at the end of ur line add this cap.release() and this cv2.destroyAllWindows()

Solution 4:[4]

Note: This problem may occur If you are using RTSP.

I was almost working on a license plate recognition program. I had almost the same problem as you When the program was running, it crashed after the first few seconds Of course, I searched the web a lot, but I did not get anywhere. I made any changes to the camera settings that you can think of. I changed the whole code, I tried a lot and finally realized the problem.

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.

What exactly does the error tell us? It tells us that I was expecting to receive image with 3 or 4 dimension but did not receive it, Or rather, It did not receive anything.

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.

Here is what you need:

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

In the except section, as you can see, we re-create the RTSP connection.

You can use this app with no problem now.

I hope this helped you.

Solution 5:[5]

I solved this issue by uninstalling OpenCV-python and reinstalling it.

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

restart the kernel & ta-da....

Solution 6:[6]

So let me explain this. This is not any error, its just lag in between your laptop's webcam and programming accessing it. Just restart your laptop. It will work fine. I faced the same problem...and just restarting solved it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Flair
Solution 2 Flair
Solution 3
Solution 4
Solution 5
Solution 6 Rushikesh Magar