Skip to content

Instantly share code, notes, and snippets.

@zakirangwala
Created December 1, 2020 06:57
Show Gist options
  • Select an option

  • Save zakirangwala/61c28859c6dbba6dedc7ad58a890f7dc to your computer and use it in GitHub Desktop.

Select an option

Save zakirangwala/61c28859c6dbba6dedc7ad58a890f7dc to your computer and use it in GitHub Desktop.

Revisions

  1. zakirangwala created this gist Dec 1, 2020.
    34 changes: 34 additions & 0 deletions detect.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    def check(image):
    image_np = cv2.imread(image)
    input_tensor = tf.convert_to_tensor(
    np.expand_dims(image_np, 0), dtype=tf.float32)

    detections = detect_fn(input_tensor)
    category_index = label_map_util.create_category_index_from_labelmap(
    ANNOTATION_PATH+'/label_map.pbtxt')

    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=0.1,
    agnostic_mode=False)

    cv2.imwrite('Tensorflow/workspace/images/check/results/six.png',
    image_np_with_detections)
    print(
    f"Class - {detections['detection_classes'][0] + label_id_offset}\nScores - {detections['detection_scores'][0]}\nBoxes - {detections['detection_boxes'][0]}")