These are the imports from object detection api
```import os
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder```
I've trained the model for a few steps and This is where the detection model has been loaded from ckpt-2
```# Load pipeline config and build a detection model
configs = config_util.get_configs_from_pipeline_file(CONFIG_PATH)
detection_model = model_builder.build(model_config=configs['model'], is_training=False)
# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(CHECKPOINT_PATH, 'ckpt-2')).expect_partial()
@tf.function
def detect_fn(image):
image, shapes = detection_model.preprocess(image)
prediction_dict = detection_model.predict(image, shapes)
detections = detection_model.postprocess(prediction_dict, shapes)
return detections```
Here's the Short-Code of where Detection is done in Real-Time
```import cv2
import numpy as np
category_index = label_map_util.create_category_index_from_labelmap(ANNOTATION_PATH+'/label_map.pbtxt')
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
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```
Errors
----First One--- I feel Like the both the errors are related to maybe shape of the input_tensor
```InvalidArgumentError Traceback (most recent call last)
Input In [24], in <cell line: 2>()
5 input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
6 # input_tensor.shape, input_tensor.dtype
----> 7 detections = detect_fn(input_tensor)
9 num_detections = int(detections.pop('num_detections'))
10 detections = {key: value[0, :num_detections].numpy()
11 for key, value in detections.items()}
File C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\util\traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
File C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\eager\execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
52 try:
53 ctx.ensure_initialized()
---> 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:```
Heres the Second Error this is just out of my league of understanding right now
```InvalidArgumentError: Graph execution error:
Detected at node 'Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Slice_4' defined at (most recent call last):
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\ProgramData\Anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\ProgramData\Anaconda3\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
app.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\ProgramData\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 601, in run_forever
self._run_once()
File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 1905, in _run_once
handle._run()
File "C:\ProgramData\Anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
await self.process_one()
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
await dispatch(*args)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
await result
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 662, in execute_request
reply_content = await reply_content
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 360, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2863, in run_cell
result = self._run_cell(
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2909, in _run_cell
return runner(coro)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3106, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3309, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3369, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\DELL\AppData\Local\Temp\ipykernel_12664\698571930.py", line 7, in <cell line: 2>
detections = detect_fn(input_tensor)
File "C:\Users\DELL\AppData\Local\Temp\ipykernel_12664\2924126859.py", line 16, in detect_fn
detections = detection_model.postprocess(prediction_dict, shapes)
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\meta_architectures\ssd_meta_arch.py", line 762, in postprocess
(nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks,
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\core\post_processing.py", line 997, in batch_multiclass_non_max_suppression
if use_combined_nms:
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\core\post_processing.py", line 1244, in batch_multiclass_non_max_suppression
batch_outputs = map_fn(
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\utils\shape_utils.py", line 226, in static_or_dynamic_map_fn
if isinstance(elems, list):
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\utils\shape_utils.py", line 226, in static_or_dynamic_map_fn
if isinstance(elems, list):
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\utils\shape_utils.py", line 239, in static_or_dynamic_map_fn
outputs = [fn(arg_tuple) for arg_tuple in arg_tuples]
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\utils\shape_utils.py", line 239, in static_or_dynamic_map_fn
outputs = [fn(arg_tuple) for arg_tuple in arg_tuples]
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\core\post_processing.py", line 1184, in _single_image_nms_fn
if use_class_agnostic_nms:
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\core\post_processing.py", line 1200, in _single_image_nms_fn
nmsed_boxlist, num_valid_nms_boxes = multiclass_non_max_suppression(
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\core\post_processing.py", line 530, in multiclass_non_max_suppression
for class_idx, boxes_idx in zip(range(num_classes), boxes_ids):
File "C:\ProgramData\Anaconda3\lib\site-packages\object_detection\core\post_processing.py", line 533, in multiclass_non_max_suppression
class_scores = tf.reshape(
Node: 'Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Slice_4'
Expected size[0] in [0, 6402], but got 12804
[[{{node Postprocessor/BatchMultiClassNonMaxSuppression/MultiClassNonMaxSuppression/Slice_4}}]] [Op:__inference_detect_fn_44608]
```
Here's the video i referred- This is the video i referred to...
Here's the complete code- Here's the complete code
My apologies to post such a huge amount of code