models icon indicating copy to clipboard operation
models copied to clipboard

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

Open Rodeazzo opened this issue 5 years ago • 4 comments

Hi, I am trying to run detections on a video (mp4) following the code from: link on a Colab document. I am getting the error in the title after a couple of iterations.

The following is the code I'm using (tried pasting code but it was showing bad):

image

Error: ` ValueError: in user code:

<ipython-input-12-a0f927373045>:28 detect_fn  *
    image, shapes = 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:3242 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.`

The error occurs on this line: detections, predictions_dict, shapes = detect_fn(input_tensor)

I tried with and without the VideoWriter lines and I still get an error (albeit it takes longer without VideoWriter). Anyone know why this could be happening?

Thanks for the help!

Rodeazzo avatar May 06 '21 14:05 Rodeazzo

So, I fixed my issue (hopefully).

The issue (for anyone else in the same situation) is that following the tutorial of TensorFlow, the line:

image_np_expanded = np.expand_dims(frame, axis=0)

Is called but not used in any subsequent calls. I noticed that the following line was doing the numpy expansion yet again. So, I change the following line to use the "image_np_expanded" instead of doing the np.expand yet again. You could also remove the first line instead.

I'm not entirely sure if that was the whole issue, but today it let me run through random videos and complete.

Rodeazzo avatar May 09 '21 12:05 Rodeazzo

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar May 09 '21 12:05 google-ml-butler[bot]

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

i too have same error while detect image by real time video camera , did you find the solution???

Dreamcouple avatar Aug 19 '22 17:08 Dreamcouple

This usually means your camera is inaccessible or is already currently in use. Try shutting down anything that might be accessing the camera or restart the computer and then restart it.

mkaur013 avatar Aug 19 '22 18:08 mkaur013