Hi all, I am quite new here.
I need help. I have been trying to figure it out for more than a week, but got stuck.
I appreciate if someone can help.
Thanks so much.
I am having a problem with openCV (version 4.5.2)
I am trying to run a video with an “object detection”. It is running, but then after about one second, the video is not responding. (picture is attached below).
FYI, the “object detection” is working until the video freezes. So, I believe there is nothing wrong with the 'object detection" code.
I also found out, even without the application of “object detection”, the video freezes at the same frame. However, it will run again after a while. But this is not the case for the video with “object detection”.
Please find my code below:
Video for running the video (without object detection)
cap = cv2.VideoCapture('Tensorflow/workspace/images/video.MP4')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while cap.isOpened():
ret, frame = cap.read()
image_np = np.array(frame)
if ret:
cv2.imshow('object detection', cv2.resize(image_np, (960, 540)))
if cv2.waitKey(240) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Video for running the video (with object detection)
cap = cv2.VideoCapture('Tensorflow/workspace/images/video.MP4')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
while cap.isOpened():
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=.4,
agnostic_mode=False)
if ret:
cv2.imshow('object detection', cv2.resize(image_np_with_detections, (960, 540)))
if cv2.waitKey(240) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
the error shown: It shows error:
ValueError: 'images' must have either 3 or 4 dimensions.