Reputation: 91
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
Upvotes: 5
Views: 12392
Reputation: 23443
I had a slightly different root cause but with the same error,
image_path_jpg = "path_to.jpg"
img = tf.io.read_file(image_path_jpg)
img_resized = tf.image.resize(img, [100, 100])
Also produced
ValueError: 'images' must have either 3 or 4 dimensions.
For me solution turned out I was missing the decoding step,
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.
Upvotes: 1
Reputation: 1
First Check the if the image is being captured or not in this point image_np = np.array(frame). because it shows there is no dimension so there is no image.
Upvotes: 0
Reputation: 1
I solved this issue by uninstalling OpenCV-python and reinstalling it.
$pip uninstall opencv-python
$pip install opencv-python
restart the kernel & ta-da....
Upvotes: 0
Reputation: 125
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.
Upvotes: 0
Reputation: 1
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()
Upvotes: 0
Reputation: 68
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.
Upvotes: 2
Reputation: 35
cap = cv2.VideoCapture(0)
Try to listen with different values ranging between 0,1,2.... Worked for mine.
Upvotes: 1
Reputation: 1
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.
Upvotes: -2