Created
December 1, 2020 07:17
-
-
Save zakirangwala/93c917275a2878cf3bf3793109b77fbd to your computer and use it in GitHub Desktop.
Real-Time Prediction - Mask Detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Real Time Prediction -> Video Capture | |
def real_time_prediction(): | |
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)) | |
# Make detection | |
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 | |
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=1, | |
min_score_thresh=.001, | |
agnostic_mode=False) | |
cv2.imshow('object detection', cv2.resize( | |
image_np_with_detections, (640, 480))) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
cap.release() | |
cv2.destroyAllWindows() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment