Start - Copy (2)
Start - Copy (2)
labels = [
{'name':'Hello', 'id':1},
{'name':'Friend', 'id':2},
{'name':'Yes', 'id':3},
{'name':'No', 'id':4},
{'name':'Thank You', 'id':5},
{'name':'Ok', 'id':6},
{'name':'Bathroom', 'id':7},
{'name':'Please', 'id':8},
{'name':'1000 Years of Death', 'id':9},
]
import tensorflow as tf
from object_detection.utils import config_util # for path configuration
from object_detection.protos import pipeline_pb2
from google.protobuf import text_format
# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(CHECKPOINT_PATH, 'ckpt-21')).expect_partial() # ckpt-21
is the latest checkpoint
@tf.function
def detect_fn(image):
image, shapes = detection_model.preprocess(image) # resizing image to 320x320
prediction_dict = detection_model.predict(image, shapes) # making predictions
detections = detection_model.postprocess(prediction_dict, shapes)
return detections
category_index = label_map_util.create_category_index_from_labelmap(ANNOTATION_PATH
+ '/label_map.pbtxt')
# Setting up capture
cap = cv2.VideoCapture(0) # '0' is the device number for my camera
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print('''\n
================================ BOOTING-UP =================================
\n''')
while True:
ret, frame = cap.read()
image_np = np.array(frame)
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
vz_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)