Exemplo n.º 1
0
def make_category_index(LABEL_MAP, NUM_CLASSES):
    label_map = label_map_util.load_labelmap(LABEL_MAP)
    categories = label_map_util.convert_label_map_to_categories(label_map,
                                                        max_num_classes=NUM_CLASSES,
                                                        use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    return category_index
Exemplo n.º 2
0
 def labelify(self):
     NUM_CLASSES = 90
     label_path = os.path.join(os.getcwd(), 'training', 'data',
                               'object_detection.pbtxt')
     label_map = label_map_util.load_labelmap(label_path)
     categories = label_map_util.convert_label_map_to_categories(
         label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
     category_index = label_map_util.create_category_index(categories)
     return category_index
Exemplo n.º 3
0
    def __init__(self):
        self.detectedReferenceFlag = None
        self.isReferenceFlagDetected = False
        self.imageWarpingSize = (128, 128)

        self.L2Norm = 0

        # Grab path to current working directory
        CWD_PATH = os.getcwd()
        # Path to frozen detection graph .pb file, which contains the model that is used
        # for object detection.
        PATH_TO_CKPT = os.path.join(CWD_PATH, 'IG',
                                    'frozen_inference_graph.pb')
        # Path to label map file
        PATH_TO_LABELS = os.path.join(CWD_PATH, 'IG', 'label.pbtxt')
        # Number of classes the object detector can identify
        NUM_CLASSES = 1

        # Load the label map.
        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

        # Load the Tensorflow model into memory.
        self.detection_graph = tf.Graph()
        with self.detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            self.sess = tf.Session(graph=self.detection_graph)

        # Define input and output tensors (i.e. data) for the object detection classifier

        # Input tensor is the image
        self.image_tensor = self.detection_graph.get_tensor_by_name(
            'image_tensor:0')

        # Output tensors are the detection boxes, scores, and classes
        # Each box represents a part of the image where a particular object was detected
        self.detection_boxes = self.detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represents level of confidence for each of the objects.
        # The score is shown on the result image, together with the class label.
        self.detection_scores = self.detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = self.detection_graph.get_tensor_by_name(
            'detection_classes:0')

        # Number of objects detected
        self.num_detections = self.detection_graph.get_tensor_by_name(
            'num_detections:0')
def make_category_index(LABEL_MAP, NUM_CLASSES):
    ''' DOCSTRING
        makes the category_index used to pull the name of the detected
        object from the numerical detection class
        ----------
        INPUTS:
        LABEL_MAP: a .pbtxt file (generated with xml_to_csv.py if you're using
                  my code)
        NUM_CLASSES: Int, the number of classes your model will predict
        __________
        RETURNS:
        category_index
    '''
    label_map = label_map_util.load_labelmap(LABEL_MAP)
    categories = label_map_util.convert_label_map_to_categories(label_map,
                                                        max_num_classes=NUM_CLASSES,
                                                        use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    return category_index
def predict(file, filename):
    PATH_TO_IMAGE = file
    NUM_CLASSES = 4
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        sess = tf.Session(graph=detection_graph)
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name(
        'detection_classes:0')
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

    image = cv2.imread(PATH_TO_IMAGE)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, axis=0)

    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_expanded})
    vis_util.visualize_boxes_and_labels_on_image_array(
        image,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        category_index,
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.60)
    cv2.imwrite('uploads/' + filename + 'result.jpg', image)
    return 0
def test_object_detection():
    # Create the label/category maps for drawing the bbox objects.
    label_map = label_map_util.load_labelmap(config.PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=config.NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    # Load the model.
    model = odl.load_model()

    # Get test images.
    test_image = get_test_images()[TEST_IMAGE]

    # Feed the model and the first images to the inference runner.
    output_dict = odl.run_inference_for_single_image(test_image, model)

    # Log the detections.
    objs = set()
    scores = output_dict['detection_scores']
    classes = output_dict['detection_classes']
    for i in range(len(scores)):
        score = scores[i]
        c = classes[i]
        if score >= 0.2:
            objs.add(category_index[c]['name'])
    print("Objects detected: %s" % str(objs))

    # Save the output.
    vis_util.visualize_boxes_and_labels_on_image_array(
        test_image,
        output_dict['detection_boxes'],
        output_dict['detection_classes'],
        output_dict['detection_scores'],
        category_index,
        instance_masks=output_dict.get('detection_masks'),
        use_normalized_coordinates=True,
        line_thickness=8,
        min_score_thresh=0.4)
    vis_util.save_image_array_as_png(test_image, 'test_screens/test_image.png')
Exemplo n.º 7
0
 def __init__(self, COORDINATE_CLASS, GRAPH_PATH, LABEL_PATH, VIDEO1, VIDEO2, NUM_CLASSES=CLNUM, Verbose=False):
     self.cclass = COORDINATE_CLASS
     self.graph_path = GRAPH_PATH
     self.label_path = LABEL_PATH
     self.video1 = VIDEO1
     self.video2 = VIDEO2
     self.num_classes = NUM_CLASSES
     self.Verbose = Verbose
     
     # Map output nodes to labels for classification
     label_map = label_map_util.load_labelmap(LABEL_PATH)
     categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
     self.category_index = label_map_util.create_category_index(categories)
     
     # create graph and load tensorflow model
     self.graph = tf.Graph()
     with self.graph.as_default():
         graph_def = tf.GraphDef()
         with tf.gfile.GFile(GRAPH_PATH, 'rb') as fid:
             serialized_graph = fid.read()
             graph_def.ParseFromString(serialized_graph)
             tf.import_graph_def(graph_def, name='')
             self.sess = tf.Session(graph=self.graph)
             
     # Input tensor is the image detected from webcam
     self.image_tensor = self.graph.get_tensor_by_name('image_tensor:0')
     
     # Output tensors are the detection boxes, scores, and classes
     # Bounding box outlines the object and will be used to give location of the hand for control
     self.detection_boxes = self.graph.get_tensor_by_name('detection_boxes:0')
     
     # Scores give confidence value
     # Classes give prediction
     self.detection_scores = self.graph.get_tensor_by_name('detection_scores:0')
     self.detection_classes = self.graph.get_tensor_by_name('detection_classes:0')
     
     # Number of objects detected
     self.num_detections = self.graph.get_tensor_by_name('num_detections:0')
import time
import argparse
import numpy as np
import tensorflow as tf

from queue import Queue
from threading import Thread
from utils.app_utils import FPS, WebcamVideoStream, draw_boxes_and_labels
from models.research.object_detection.utils import label_map_util
from frozen_detection_graph import PATH_TO_CKPT, PATH_TO_LABELS

NUM_CLASSES = 90

# Loading label map
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)


def detect_objects(image_np, sess, detection_graph):
    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

    # Each box represents a part of the image where a particular object was detected.
    boxes = detection_graph.get_tensor_by_name('detection_boxes:0')

    # Each score represent how level of confidence for each of the objects.
    # Score is shown on the result image, together with the class label.
    scores = detection_graph.get_tensor_by_name('detection_scores:0')
    classes = detection_graph.get_tensor_by_name('detection_classes:0')
Exemplo n.º 9
0
    def browse_mul_image_file(self, _):
        self.selected_image_file = tkFileDialog.askopenfilename(
            initialdir="~/Downloads",
            title="Select file",
            filetypes=(("jpeg files", "*.jpg"), ("all files", "*.*")))
        if self.selected_image_file:
            self.mul_image_file_entry.delete(0, END)
            self.mul_image_file_entry.insert(0, self.selected_image_file)

    #def object_detect_show(self):

        CWD_PATH = os.getcwd()

        #IMAGE_NAME = '000001.jpg'

        PATH_TO_CKPT = os.path.join(CWD_PATH, 'models', 'research',
                                    'object_detection', 'inference_graph',
                                    'frozen_inference_graph.pb')
        # Path to label map file
        PATH_TO_LABELS = os.path.join(CWD_PATH, 'models', 'research',
                                      'object_detection', 'training',
                                      'labelmap.pbtxt')

        # Path to image
        PATH_TO_IMAGE = self.selected_image_file

        NUM_CLASSES = 2

        label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        categories = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index = label_map_util.create_category_index(categories)

        # Load the Tensorflow model into memory.
        detection_graph = tf.Graph()
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            sess = tf.Session(graph=detection_graph)

        # Define input and output tensors (i.e. data) for the object detection classifier

        # Input tensor is the image
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')

        # Output tensors are the detection boxes, scores, and classes
        # Each box represents a part of the image where a particular object was detected
        detection_boxes = detection_graph.get_tensor_by_name(
            'detection_boxes:0')

        # Each score represents level of confidence for each of the objects.
        # The score is shown on the result image, together with the class label.
        detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')

        # Number of objects detected
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        # Load image using OpenCV and
        # expand image dimensions to have shape: [1, None, None, 3]
        # i.e. a single-column array, where each item in the column has the pixel RGB value
        image = cv2.imread(PATH_TO_IMAGE)
        image_expanded = np.expand_dims(image, axis=0)

        # Perform the actual detection by running the model with the image as input
        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})

        # Draw the results of the detection (aka 'visulaize the results')

        image_detected = vis_util.visualize_boxes_and_labels_on_image_array(
            image,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.60)
        #image = Image.open(image)
        '''self.tk_image = ImageTk.PhotoImage(image)
        self.main_panel.config(
            width=max(image.width(), 256), height=max(image.width(), 256))
        self.main_panel.create_image(0, 0, image=self.tk_image, anchor=NW)'''

        cv2.imwrite(os.path.join(CWD_PATH, 'waka.jpg'), image)

        image = Image.open(os.path.join(CWD_PATH, 'waka.jpg'))
        self.tk_image = ImageTk.PhotoImage(image)
        self.main_panel.config(width=max(self.tk_image.width(), 256),
                               height=max(self.tk_image.height(), 256))
        self.main_panel.create_image(0, 0, image=self.tk_image, anchor=NW)