我有一个麻烦与我的程序检测,我已经尝试了不同的解决方案,我只能从重新启动,试图插入和拔出我的相机等,所有的解决方案似乎不起作用,所以我一直认为有一些错误在我的代码,但我真的不知道什么。代码上的错误似乎是说它无法检测到3维和4维。
主代码
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
这是错误代码
ValueError Traceback(最近一次调用)在45 input_tensor = tf.convert_to_tensor(np。Expand_dims (image_np, 0), dtype=tf.float32)——比;6 detections = detect_fn(input_tensor)78 num_detections = int(detections.pop('num_detections'))
F:Anacondalibsite-packagestensorflowpythoneagerdef_function.py in __call__(self, *args, **kwds)
887
888 with OptionalXlaContext(self._jit_compile):
--> 889 result = self._call(*args, **kwds)
890
891 new_tracing_count = self.experimental_get_tracing_count()
F:Anacondalibsite-packagestensorflowpythoneagerdef_function.py in _call(self, *args, **kwds)
922 # In this case we have not created variables on the first call. So we can
923 # run the first trace but we should fail if variables are created.
--> 924 results = self._stateful_fn(*args, **kwds)
925 if self._created_variables:
926 raise ValueError("Creating variables on a non-first call to a function"
F:Anacondalibsite-packagestensorflowpythoneagerfunction.py in __call__(self, *args, **kwargs)
3020 with self._lock:
3021 (graph_function,
-> 3022 filtered_flat_args) = self._maybe_define_function(args, kwargs)
3023 return graph_function._call_flat(
3024 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access
F:Anacondalibsite-packagestensorflowpythoneagerfunction.py in _maybe_define_function(self, args, kwargs)
3442
3443 self._function_cache.missed.add(call_context_key)
-> 3444 graph_function = self._create_graph_function(args, kwargs)
3445 self._function_cache.primary[cache_key] = graph_function
3446
F:Anacondalibsite-packagestensorflowpythoneagerfunction.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
3287 arg_names=arg_names,
3288 override_flat_arg_shapes=override_flat_arg_shapes,
-> 3289 capture_by_value=self._capture_by_value),
3290 self._function_attributes,
3291 function_spec=self.function_spec,
F:Anacondalibsite-packagestensorflowpythonframeworkfunc_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
997 _, original_func = tf_decorator.unwrap(python_func)
998
--> 999 func_outputs = python_func(*func_args, **func_kwargs)
1000
1001 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
F:Anacondalibsite-packagestensorflowpythoneagerdef_function.py in wrapped_fn(*args, **kwds)
670 # the function a weak reference to itself to avoid a reference cycle.
671 with OptionalXlaContext(compile_with_xla):
--> 672 out = weak_wrapped_fn().__wrapped__(*args, **kwds)
673 return out
674
F:Anacondalibsite-packagestensorflowpythonframeworkfunc_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
ValueError: in user code:
<ipython-input-18-f5bf27ce595e>:11 detect_fn *
image, shapes = detection_model.preprocess(image)
F:Anacondalibsite-packagesobject_detectionmeta_architecturesssd_meta_arch.py:484 preprocess *
normalized_inputs, self._image_resizer_fn)
F:Anacondalibsite-packagesobject_detectionutilsshape_utils.py:492 resize_images_and_return_shapes *
outputs = static_or_dynamic_map_fn(
F:Anacondalibsite-packagesobject_detectionutilsshape_utils.py:246 static_or_dynamic_map_fn *
outputs = [fn(arg) for arg in tf.unstack(elems)]
F:Anacondalibsite-packagesobject_detectioncorepreprocessor.py:3327 resize_image *
new_image = tf.image.resize_images(
F:Anacondalibsite-packagestensorflowpythonutildispatch.py:206 wrapper **
return target(*args, **kwargs)
F:Anacondalibsite-packagestensorflowpythonopsimage_ops_impl.py:1544 resize_images
skip_resize_if_same=True)
F:Anacondalibsite-packagestensorflowpythonopsimage_ops_impl.py:1396 _resize_images_common
raise ValueError(''images' must have either 3 or 4 dimensions.')
ValueError: 'images' must have either 3 or 4 dimensions.
你刚刚从你的摄像头得到了错误,特别是你得到了你的摄像头和你的系统之间的延迟,解决方案是你需要改变你的代码cv2.waitKey(1) & 0xFF == ord('q'):
到key == ord('q'):
和之前,如果你应该添加key = cv2.waitKey(1) & 0xFF
和在你的行末尾添加这个cap.release()
和这个cv2.destroyAllWindows()