Пример #1
0
def main(_):
  print(time.ctime())
  host, port = FLAGS.server.split(':')
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
  # Send request
  IMAGE_NAME = '/home/rice/tensorflow1/models/research/object_detection/test_image/'

  TEST_IMAGE_PATHS = glob.glob(os.path.join(IMAGE_NAME, '*.*'))


  for image_path in TEST_IMAGE_PATHS:
    print(image_path)

    image = Image.open(image_path)
    # the array based representation of the image will be used later in order to prepare the
    # result image with boxes and labels on it.
    image_np = load_image_into_numpy_array(image)
    image_np_expanded = np.expand_dims(image_np, axis=0)
    print(image_np_expanded.shape)

    request = predict_pb2.PredictRequest()
    request.model_spec.name = 'saved_model'
    request.model_spec.signature_name = 'serving_default'
    # request.inputs['inputs'].CopyFrom(
    #     tf.contrib.util.make_tensor_proto(image_np_expanded,shape=[1]))
    # print(time.ctime())
    # request.inputs['inputs'].CopyFrom(
    #     make_tensor_proto(image_np_expanded)
    # )

    # dims = [tensor_shape_pb2.TensorShapeProto.Dim(size=1)]
    # tensor_shape_proto = tensor_shape_pb2.TensorShapeProto(dim=dims)
    tensor_proto = tensor_pb2.TensorProto(
        dtype=types_pb2.DT_UINT8,
        tensor_shape=tensor_shape.as_shape(image_np_expanded.shape).as_proto(),
        # string_val=[open(image_path, 'rb').read()]
    )
    tensor_proto.tensor_content = image_np_expanded.tostring()

    request.inputs['inputs'].CopyFrom(tensor_proto)

    print(time.ctime())
    start = time.time()
    result = stub.Predict(request, 100.0)  # 10 secs timeout
    #print(result)
    print(time.time() - start)


    boxes = (result.outputs['detection_boxes'].float_val)
    classes = (result.outputs['detection_classes'].float_val)
    scores = (result.outputs['detection_scores'].float_val)

    box_np_arr = array([boxes[x:x + 4] for x in range(0, len(boxes), 4)])
    class_np_arr = array([classes[x:x + 1] for x in range(0, len(classes), 1)])
    score_np_arr = array([scores[x:x + 1] for x in range(0, len(scores), 1)])

    # print(type(box_np_arr),len(box_np_arr))
    # print(type(class_np_arr),len(classes))
    # print(type(class_np_arr),len(scores))

    # print(result)

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

    image_vis,result_box = vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      np.squeeze(box_np_arr),
      np.squeeze(class_np_arr).astype(np.int32),
      np.squeeze(score_np_arr),
      category_index,
      use_normalized_coordinates=True,
      line_thickness=8,
      min_score_thresh=0.80)

    # Save inference to disk
    scipy.misc.imsave('%s.jpg' % (image), image_vis)

    for box,label_score in result_box.items():
        print(label_score,',',box)

    break
def main():
    print("starting program . . .")

    if not checkIfNecessaryPathsAndFilesExist():
        return
    # end if

    # now that we've checked for the protoc compile, import the TensorFlow models repo utils content
    from utils import label_map_util
    from utils import visualization_utils as vis_util

    # if TensorFlow version is too old, show error message and bail
    # this next comment line is necessary to avoid a false warning if using the editor PyCharm
    # noinspection PyUnresolvedReferences
    if StrictVersion(tf.__version__) < StrictVersion('1.5.0'):
        print('error: Please upgrade your tensorflow installation to v1.5.* or later!')
        return
    # end if

    # if the frozen inference graph file does not already exist, download the model tar file and unzip it
    try:
        if not os.path.exists(FROZEN_INFERENCE_GRAPH_LOC):
            # if the model tar file has not already been downloaded, download it
            if not os.path.exists(os.path.join(MODEL_SAVE_DIR_LOC, MODEL_FILE_NAME)):
                # download the model
                print("downloading model . . .")
                # instantiate a URLopener object, then download the file
                opener = urllib.request.URLopener()
                opener.retrieve(DOWNLOAD_MODEL_FROM_LOC + MODEL_FILE_NAME, os.path.join(MODEL_SAVE_DIR_LOC, MODEL_FILE_NAME))
            # end if

            # unzip the tar to get the frozen inference graph
            print("unzipping model . . .")
            tar_file = tarfile.open(os.path.join(MODEL_SAVE_DIR_LOC, MODEL_FILE_NAME))
            for file in tar_file.getmembers():
                file_name = os.path.basename(file.name)
                if 'frozen_inference_graph.pb' in file_name:
                    tar_file.extract(file, MODEL_SAVE_DIR_LOC)
                # end if
            # end for
        # end if
    except Exception as e:
        print("error downloading or unzipping model: "   + str(e))
        return
    # end try

    # if the frozen inference graph does not exist after the above, show an error message and bail
    if not os.path.exists(FROZEN_INFERENCE_GRAPH_LOC):
        print("unable to get / create the frozen inference graph")
        return
    # end if

    # load the frozen model into memory
    print("loading frozen model into memory . . .")
    detection_graph = tf.Graph()
    try:
        with detection_graph.as_default():
            od_graph_def = tf.GraphDef()
            with tf.gfile.GFile(FROZEN_INFERENCE_GRAPH_LOC, 'rb') as fid:
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')
            # end with
        # end with
    except Exception as e:
        print("error loading the frozen model into memory: " + str(e))
        return
    # end try

    # load the label map
    print("loading label map . . .")
    label_map = label_map_util.load_labelmap(LABEL_MAP_LOC)
    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)

    print("starting object detection . . .")
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            for fileName in os.listdir(TEST_IMAGES_DIR):
                if fileName.endswith(".jpg"):
                    image_np = cv2.imread(os.path.join(TEST_IMAGES_DIR, fileName))
                    if image_np is not None:
                        # Definite input and output Tensors for detection_graph
                        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.
                        detection_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.
                        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')

                        # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                        image_np_expanded = np.expand_dims(image_np, axis=0)
                        # Actual detection.
                        (boxes, scores, classes, num) = sess.run(
                            [detection_boxes, detection_scores, detection_classes, num_detections],
                            feed_dict={image_tensor: image_np_expanded})
                        # Visualization of the results of a detection.
                        vis_util.visualize_boxes_and_labels_on_image_array(image_np,
                                                                           np.squeeze(boxes),
                                                                           np.squeeze(classes).astype(np.int32),
                                                                           np.squeeze(scores),
                                                                           category_index,
                                                                           use_normalized_coordinates=True,
                                                                           line_thickness=8)
                        cv2.imshow("result", image_np)
                        cv2.waitKey()
Пример #3
0
def foo(imgin, imgout):
    #%matplotlib inline
    sys.path.append("..")

    MODEL_NAME = 'carrotBeanGraph'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('training', 'object-detection.pbtxt')

    # Modify this as you add classes
    NUM_CLASSES = 2

    # Loads the frozen 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='')

    # Loads 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)

    # Numpy helper code
    def load_image_into_numpy_array(image):
        (im_width, im_height) = image.size
        return np.array(image.getdata()).reshape(
            (im_height, im_width, 3)).astype(np.uint8)

    # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
    PATH_TO_TEST_IMAGES_DIR = 'test_images'
    # Only doing one image at a time, so change this.
    # TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(3, 8) ]

    # Size, in inches, of the output images.
    IMAGE_SIZE = (12, 8)

    # Algorithm to create Tensors, detect the objects, and output an image file with mappings.
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            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.
            detection_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.
            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')
            # Don't want a for-loop in final version

            #for image_path in TEST_IMAGE_PATHS:
            image = Image.open(
                imgin)  #change later so raw image file, rather than file path
            # the array based representation of the image will be used later in order to prepare the
            # result image with boxes and labels on it.
            image_np = load_image_into_numpy_array(image)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})
            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8)
            plt.figure(figsize=IMAGE_SIZE)
            #plt.imshow(image_np)
            plt.imsave(imgout, image_np)
Пример #4
0
    def cocomain(input_speed):
        initial_car_acceleration = 800
        if not os.path.exists(MODEL_NAME + '/frozen_inference_graph.pb'):
        	print ('Downloading the model')
        	opener = urllib.request.URLopener()
        	opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        	tar_file = tarfile.open(MODEL_FILE)
        	for file in tar_file.getmembers():
        	  file_name = os.path.basename(file.name)
        	  if 'frozen_inference_graph.pb' in file_name:
        	    tar_file.extract(file, os.getcwd())
        	print('Download complete')
        else:
        	print('Model already exists')

        # ## Load a (frozen) 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='')

        # Loading label map:
        # Label maps map indices to category names, so that when our convolution
        # network predicts `5`, we know that this corresponds to `airplane`.  Here
        # we use internal utility functions, but anything that returns a dictionary
        # mapping integers to appropriate string labels would be fine

        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)

        # Intializing the web camera device: 0 for internal and 1 for external camera
        # of the laptop used
        cap = cv2.VideoCapture(0)

        # Running the tensorflow session
        with detection_graph.as_default():
          with tf.Session(graph=detection_graph) as sess:
           ret = True
           while (ret):
              ret,image_np = cap.read()
              #print(image_np)
              # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
              image_np_expanded = np.expand_dims(image_np, axis=0)
              #print(image_np_expanded)
              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')
              num_detections = detection_graph.get_tensor_by_name('num_detections:0')
              # Actual detection.
              (boxes, scores, classes, num_detections) = sess.run(
                  [boxes, scores, classes, num_detections],
                  feed_dict={image_tensor: image_np_expanded})
              # Visualization of the results of a detection.
              vis_util.visualize_boxes_and_labels_on_image_array(
                  image_np,
                  np.squeeze(boxes),
                  np.squeeze(classes).astype(np.int32),
                  np.squeeze(scores),
                  category_index,
                  use_normalized_coordinates=True,
                  line_thickness=8)
              classes_detected = classes[scores>0.5]
              if 13 in classes_detected:
                  #print('detected')
                  car3.changeSpeed(0, initial_car_acceleration)
                  car2.changeSpeed(0, initial_car_acceleration)
                  #car3.changeLaneRight(250, 250)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  #print(car3.piece)
              elif 10 in classes_detected:
                  print('verkeerslicht detected')
                  #print('detected')
                  car3.changeSpeed(0, initial_car_acceleration)
                  car2.changeSpeed(0, initial_car_acceleration)
                  #car3.changeLaneRight(250, 250)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  """
              elif 1 in classes_detected:
                  print('car detected')
                  car3.changeSpeed(int(initial_car_speed/2), initial_car_acceleration)
                  car2.changeSpeed(int(initial_car_speed/2), initial_car_acceleration)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  """
              else:
                  car3.changeSpeed(input_speed, initial_car_acceleration)
                  car2.changeSpeed(input_speed, initial_car_acceleration)
                  car3.setLocationChangeCallback(locationChangeCallback)
                  #print(car3.piece)
                  #drive(cv2)
              #print(image_np,boxes,classes,scores,category_index)
              #plt.figure(figsize=IMAGE_SIZE)
              #plt.imshow(image_np)
              cv2.imshow('image',cv2.resize(image_np,(1280,960)))
              if cv2.waitKey(25) & 0xFF == ord('q'):
                  cv2.destroyAllWindows()
                  cap.release()
                  break
Пример #5
0
)
density_model = model_from_json(
    open('/home/allen/linux/catkin_ws/src/counting/test_video/model.json').
    read())
density_model.load_weights(
    '/home/allen/linux/catkin_ws/src/counting/test_video/CSRNET.h5')
# Path to label map file
PATH_TO_LABELS = os.path.join(CWD_PATH, 'data', 'labelmap.pbtxt')

# Path to image
PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_NAME)

# Number of classes the object detector can identify
NUM_CLASSES = 1

label_map = label_map_util.load_labelmap(
    '/home/allen/linux/catkin_ws/src/counting/test_video/data/labelmap.pbtxt')
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)
def load_labels(path_to_labels_file, num_classes):
	label_map = label_map_util.load_labelmap(path_to_labels_file)
	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
Пример #7
0
def accident_detection():
    print("car crash detection")
    MODEL_NAME = 'inference_graph'

    CWD_PATH = os.getcwd()

    PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                'frozen_inference_graph.pb')

    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    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)

    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')

    count = 1

    cap = cv2.VideoCapture(
        'C:\\TensorflowModels\\models\\research\\object_detection\\test_video\\acc6.mp4'
    )
    while (cap.isOpened()):
        ret, frame = cap.read()
        image_expanded = np.expand_dims(frame, axis=0)
        (boxes, scores, classes,
         num) = sess.run([
             detection_boxes, detection_scores, detection_classes,
             num_detections
         ],
                         feed_dict={image_tensor: image_expanded})
        # if args.textual:
        print("\n\n" + "=" * 50 + "	Results    " + "=" * 50 + "\n\n")
        print("        Class               Surety")
        print()
        count = 0
        for i in range(scores.shape[1]):
            if scores[0, i] > 0.8:
                print("    " + str(i + 1) + ".  " +
                      str(category_index.get(classes[0, i])['name']) +
                      "    ==>    " + str(scores[0, i] * 100) + ' %')
                print()
                count += 1

                if (str(category_index.get(classes[0,
                                                   i])['name']) == "crashed"):
                    winsound.Beep(440, 500)
        print("\n	Total " + str(count) + " objects classified.\n")

        vis_util.visualize_boxes_and_labels_on_image_array(
            frame,
            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.80)
        cv2.imshow('Object detector', frame)
        count += 1

        cv2.waitKey(1)

    cap.release()

    cv2.destroyAllWindows()
# coding: utf-8
from __future__ import print_function
import numpy as np
import os
# import six.moves.urllib as urllib
import sys
# import tarfile
import tensorflow as tf
# import zipfile
# from collections import defaultdict
# from io import StringIO
# import matplotlib
# matplotlib.use('TkAgg')
# from matplotlib import pyplot as plt
# from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util
from utils import ops as utils_ops

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

if tf.__version__ < '1.4.0':
    raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')


# ## Variables
#
# Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.
Пример #9
0
    # Path to frozen detection graph.
    path_to_ckpt = op.join(model_name, 'frozen_inference_graph.pb')
    # Path to the label file
    path_to_label = op.join(os.getcwd(), 'label_map.pbtxt')
    #only train on buildings
    num_classes = 2
    #Directory to test images path
    test_image_path = op.join(os.getcwd(), 'test')
    test_imgs = glob.glob(test_image_path + "/*.JPG")
    #print(test_imgs)
    ############
    #Load the frozen tensorflow model
    #############

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.io.gfile.GFile('frozen_inference_graph.pb', 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    ############
    #Load the label file
    #############
    label_map = label_map_util.load_labelmap('label_map.pbtxt')
    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)
    tf_od_pred()
Пример #10
0
 def load_category(self, label_file):
     label_map = label_map_util.load_labelmap(label_file)
     categories = label_map_util.convert_label_map_to_categories(
         label_map, max_num_classes=20, use_display_name=True)
     category_index = label_map_util.create_category_index(categories)
     return category_index
Пример #11
0
def detect_in_video():

    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='')

    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)

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:

            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')

            videogen = skvideo.io.vreader('myVideo.mkv')
            frames = list(videogen)

            for i, frame in enumerate(frames[:500]):

                if i % 100 == 0:
                    print(f'{i}/{len(frames)}')

                image_np_expanded = np.expand_dims(frame, axis=0)

                (boxes, scores, classes,
                 num) = sess.run([
                     detection_boxes, detection_scores, detection_classes,
                     num_detections
                 ],
                                 feed_dict={image_tensor: image_np_expanded})

                vis_util.visualize_boxes_and_labels_on_image_array(
                    frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=2,
                    min_score_thresh=.20)

                frames2.append(frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break

            skvideo.io.vwrite("outputVideoX.mkv", frames2)
            print('Complete!')
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='')


# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

# In[7]:

label_map = label_map_util.load_labelmap(os.path.join('/Users/sinsuung/.virtualenvs/cv/lib/python3.6/site-packages/tensorflow/models/research/object_detection/data', 'mscoco_label_map.pbtxt'))
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)


# ## Helper code

# In[8]:

def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)


# # Detection
def detect_in_video():
    # VideoWriter is the responsible of creating a copy of the video
    # used for the detections but with the detections overlays. Keep in
    # mind the frame size has to be the same as original video.
    #fourcc = cv2.VideoWriter_fourcc(*'XVID')
    #out = cv2.VideoWriter('gun.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), 250, (1280, 720))
    out = cv2.VideoWriter('gun7.avi',
                          cv2.VideoWriter_fourcc('M', 'J', 'P',
                                                 'G'), 30, (1280, 720))

    #Load a (frozen) 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='')

    #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)

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            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.
            detection_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.
            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')
            #cv2.VideoCapture (): 0 is the default camera of the computer, 1 can change the source.
            #for example: direct access to surveillance cameras
            cap = cv2.VideoCapture(
                'C:\\Users\\28771\\models\\research\\object_detection\\test_images1\\video7.mp4'
            )

            while (cap.isOpened()):
                # Read the frame and capture frame-by-frame
                ret, frame = cap.read()

                # Recolor the frame. By default, OpenCV uses BGR color space.
                detect_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

                image_np_expanded = np.expand_dims(detect_frame, axis=0)

                # Actual detection.
                (boxes, scores, classes,
                 num) = sess.run([
                     detection_boxes, detection_scores, detection_classes,
                     num_detections
                 ],
                                 feed_dict={image_tensor: image_np_expanded})

                # Visualization of the results of a detection.
                # note: perform the detections using a higher threshold
                vis_util.visualize_boxes_and_labels_on_image_array(
                    detect_frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=8,
                    min_score_thresh=.20)

                #cv2.imshow('detect_output', detect_frame)
                output_rgb = cv2.cvtColor(detect_frame, cv2.COLOR_RGB2BGR)
                cv2.imshow('detect_output', output_rgb)
                out.write(output_rgb)

                #The number in waitKey(1) represents the invalid time before waiting for the key input.
                #The unit is milliseconds. During this time period, the key 'q' will not be recorded.
                #In other words, after the invalid time, it is detected whether the key 'q' is pressed in the time period of the last image display.
                #If not, it will jump out of the if statement and capture and display the next frame of image.
                #The return value of cv2.waitkey (1) is more than 8 bits, but only the last 8 bits are actually valid.
                # To avoid interference, the remaining position is 0 through the '&' operation
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            #when everything done , release the capture
            out.release()
            cap.release()
# ## Load a (frozen) 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='')


# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

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)

#intializing the web camera device

import cv2
cap = cv2.VideoCapture(0)

# Running the tensorflow session
with detection_graph.as_default():
  with tf.Session(graph=detection_graph) as sess:
   ret = True
   while (ret):
      ret,image_np = cap.read()
      # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
Пример #15
0
def upload():
    target = os.path.join(APP_ROOT, 'images')
    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        filename = file.filename
        destination = '/'.join([target, filename])
        file.save(destination)
        MODEL_NAME = 'inference_graph'
        IMAGES = os.listdir('C:/Users/MY PC/Desktop/FlaskApp/images')
        IMAGE_NAME = IMAGES[0]
        IMAGE_PATH = 'C:/Users/MY PC/Desktop/FlaskApp/images'

        CWD_PATH = 'E:/miniproject'

        PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                    'frozen_inference_graph.pb')

        PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

        PATH_TO_IMAGE = os.path.join(IMAGE_PATH, IMAGE_NAME)

        NUM_CLASSES = 1

        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_expanded = np.expand_dims(image, 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=4,
            min_score_thresh=0.65)

        cv2.imshow('Object detector', image)

        cv2.imwrite('new_img.jpg', image)

        cv2.waitKey(0)

        cv2.destroyAllWindows()
        os.remove(PATH_TO_IMAGE)
    return render_template('predict.html')
def detectObject():
    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import win32
    from PIL import Image

    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")
    from object_detection.utils import ops as utils_ops

    if tf.__version__ < '1.4.0':
        raise ImportError(
            'Please upgrade your tensorflow installation to v1.4.* or later!')

    import cv2
    cap = cv2.VideoCapture(0)

    # ## Env setup

    from utils import label_map_util

    from utils import visualization_utils as vis_util

    # # Model preparation

    # ## Variables
    #
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_FROZEN_GRAPH` to point to a new .pb file.
    #
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

    NUM_CLASSES = 90

    # ## Download Model

    # Check the frozen_inference_graph is exists at path.
    # If not ssd_mobilenet_v1_coco_2017_11_17 model is downloaded and frozen_inference_graph.pb moved to path.
    if os.path.exists(PATH_TO_FROZEN_GRAPH):
        print(
            "ssd_mobilenet_v1_coco_2017_11_17/frozen_inference_graph.pb Exists at path. Skipping download... "
        )
    else:
        opener = urllib.request.URLopener()
        opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        tar_file = tarfile.open(MODEL_FILE)
        for file in tar_file.getmembers():
            file_name = os.path.basename(file.name)
            if 'frozen_inference_graph.pb' in file_name:
                tar_file.extract(file, os.getcwd())

    # ## Load a (frozen) Tensorflow model into memory.

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.compat.v1.GraphDef()
        with tf.io.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

    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

    def run_inference_for_single_image(image, graph):
        with graph.as_default():
            with tf.compat.v1.Session() as sess:
                # Get handles to input and output tensors
                ops = tf.compat.v1.get_default_graph().get_operations()
                all_tensor_names = {
                    output.name
                    for op in ops for output in op.outputs
                }
                tensor_dict = {}
                for key in [
                        'num_detections', 'detection_boxes',
                        'detection_scores', 'detection_classes',
                        'detection_masks'
                ]:
                    tensor_name = key + ':0'
                    if tensor_name in all_tensor_names:
                        tensor_dict[key] = tf.compat.v1.get_default_graph(
                        ).get_tensor_by_name(tensor_name)
                if 'detection_masks' in tensor_dict:
                    # The following processing is only for single image
                    detection_boxes = tf.squeeze(
                        tensor_dict['detection_boxes'], [0])
                    detection_masks = tf.squeeze(
                        tensor_dict['detection_masks'], [0])
                    # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
                    real_num_detection = tf.cast(
                        tensor_dict['num_detections'][0], tf.int32)
                    detection_boxes = tf.slice(detection_boxes, [0, 0],
                                               [real_num_detection, -1])
                    detection_masks = tf.slice(detection_masks, [0, 0, 0],
                                               [real_num_detection, -1, -1])
                    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
                        detection_masks, detection_boxes, image.shape[0],
                        image.shape[1])
                    detection_masks_reframed = tf.cast(
                        tf.greater(detection_masks_reframed, 0.5), tf.uint8)
                    # Follow the convention by adding back the batch dimension
                    tensor_dict['detection_masks'] = tf.expand_dims(
                        detection_masks_reframed, 0)
                image_tensor = tf.compat.v1.get_default_graph(
                ).get_tensor_by_name('image_tensor:0')

                # Run inference
                output_dict = sess.run(
                    tensor_dict,
                    feed_dict={image_tensor: np.expand_dims(image, 0)})

                # all outputs are float32 numpy arrays, so convert types as appropriate
                output_dict['num_detections'] = int(
                    output_dict['num_detections'][0])
                output_dict['detection_classes'] = output_dict[
                    'detection_classes'][0].astype(np.uint8)
                output_dict['detection_boxes'] = output_dict[
                    'detection_boxes'][0]
                output_dict['detection_scores'] = output_dict[
                    'detection_scores'][0]
                if 'detection_masks' in output_dict:
                    output_dict['detection_masks'] = output_dict[
                        'detection_masks'][0]
        return output_dict

    while True:
        ret, image_np = cap.read()
        # Actual detection.
        output_dict = run_inference_for_single_image(image_np, detection_graph)
        # Visualization of the results of a detection.
        vis_util.visualize_boxes_and_labels_on_image_array(
            image_np,
            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)
        #print(output_dict['detection_scores'])
        lbl = output_dict['detection_classes'][0]
        #print(category_index[lbl]['name'])
        win32.voice(category_index[lbl]['name'])
        cv2.imshow('Image Detction', cv2.resize(image_np, (1280, 720)))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
Пример #17
0
import numpy as np
import tensorflow as tf
import sys
from PIL import Image
import cv2
cap = cv2.VideoCapture(0)

from utils import label_map_util
from utils import visualization_utils as vis_util

# ------------------ Face Model Initialization ------------------------------ #
face_label_map = label_map_util.load_labelmap('training_face/labelmap.pbtxt')
face_categories = label_map_util.convert_label_map_to_categories(
    face_label_map, max_num_classes=2, use_display_name=True)
face_category_index = label_map_util.create_category_index(face_categories)

face_detection_graph = tf.Graph()

with face_detection_graph.as_default():
    face_od_graph_def = tf.GraphDef()
    with tf.gfile.GFile('inference_graph_face_ssd/frozen_inference_graph.pb',
                        'rb') as fid:
        face_serialized_graph = fid.read()
        face_od_graph_def.ParseFromString(face_serialized_graph)
        tf.import_graph_def(face_od_graph_def, name='')

    face_session = tf.Session(graph=face_detection_graph)

face_image_tensor = face_detection_graph.get_tensor_by_name('image_tensor:0')
face_detection_boxes = face_detection_graph.get_tensor_by_name(
    'detection_boxes:0')
Пример #18
0
    # Path to frozen detection graph.
    path_to_ckpt = op.join(model_name, 'frozen_inference_graph.pb')
    # Path to the label file
    path_to_label = op.join(os.getcwd(), FLAGS.path_to_label)
    #only train on buildings
    num_classes = 2
    #Directory to test images path
    test_image_path = op.join(os.getcwd(), FLAGS.test_image_path)
    test_imgs = glob.glob(test_image_path + "/*.jpg")

    ############
    #Load the frozen tensorflow model
    #############

    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='')

    ############
    #Load the label file
    #############
    label_map = label_map_util.load_labelmap(path_to_label)
    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)
tf_od_pred()
Пример #19
0
def camara():

    tamanoMax = 768
    movimiento = False
    count = 0

    #file = tkinter.filedialog.askopenfilename(defaultextension="*.avi", title="Control: diropenbox",initialdir="C:/Users/DANI/Desktop/object_recognition_detection",parent=None)

    cam = cv2.VideoCapture(1)
    if (cam.isOpened() == False):
        print("ERROR: Camara no operativa")
        cam = cv2.VideoCapture(0)
        if (cam.isOpened() == False):
            exit(-1)  # Error acceso a la camara

    # Llamada al método
    fgbg = cv2.createBackgroundSubtractorKNN(history=500,
                                             dist2Threshold=400,
                                             detectShadows=False)

    # Deshabilitamos OpenCL, si no hacemos esto no funciona
    cv2.ocl.setUseOpenCL(False)

    contadorMovimiento = 0

    ret = True

    #cargarTensorflow()

    # # Model preparation
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.

    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'

    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

    NUM_CLASSES = 90

    # ## Download Model

    if not os.path.exists(MODEL_NAME + '/frozen_inference_graph.pb'):
        print('Downloading the model')
        opener = urllib.request.URLopener()
        opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
        tar_file = tarfile.open(MODEL_FILE)
        for file in tar_file.getmembers():
            file_name = os.path.basename(file.name)
            if 'frozen_inference_graph.pb' in file_name:
                tar_file.extract(file, os.getcwd())
        print('Download complete')
    else:
        print('Model already exists')

    # ## Load a (frozen) 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:  # aqui le da el modelo de datos
            serialized_graph = fid.read()  # lee
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

    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)  # estaba true
    category_index = label_map_util.create_category_index(categories)

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(
        "GRABACIONES/" + time.strftime("%d_%m_%y_%H%M%S") + '.avi', fourcc,
        20.0, (640, 480))
    #out1=cv2.VideoWriter("GRABACIONES/"+"contorno_"+time.strftime("%d_%m_%y_%H%M%S")+'.avi', fourcc, 20.0, (640, 480))
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s : %(levelname)s : %(message)s',
        filename=("GRABACIONES/" + "Indicencias_" +
                  time.strftime("%d_%m_%y_%H%M%S") + '.log'),
        filemode='w',
    )
    logging.debug('REGISTRO DE INCIDENCIAS.')
    instanteInicial = time.time()

    instanteFinal = time.time()
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            while True:
                ret, frame = cam.read()
                contornosimg = frame
                if ret == True:
                    movimiento = False
                    frame = cv2.resize(frame, (640, 480))
                    contornosimg = cv2.resize(contornosimg, (640, 480))
                    # IMPORTADO TUTORIAL MOVIMIENTO
                    # Aplicamos el algoritmo
                    fgmask = fgbg.apply(frame)

                    # Copiamos el umbral para detectar los contornos
                    contornosimg = fgmask.copy()

                    # Buscamos contorno en la imagen
                    im, contornos, hierarchy = cv2.findContours(
                        contornosimg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

                    # Recorremos todos los contornos encontrados
                    for c in contornos:
                        # Eliminamos los contornos más pequeños

                        if cv2.contourArea(c) < 500:
                            continue
                        else:
                            movimiento = True

                        # Obtenemos el bounds del contorno, el rectángulo mayor que engloba al contorno
                        (x, y, w, h) = cv2.boundingRect(c)
                        # Dibujamos el rectángulo del bounds
                        cv2.rectangle(contornosimg, (x, y), (x + w, y + h),
                                      (0, 255, 0), 2)

                    #image_np = cv2.resize(image_np, (640, 480))

                    print(movimiento)

                    if count > 3:
                        if ret == True:
                            # frame = cv2.flip(frame,0)
                            contadorMovimiento = 1
                            timestamp = datetime.datetime.now()
                            ts = timestamp.strftime("%d %B %Y %I:%M:%S%p")
                            cv2.rectangle(frame, (2, 220), (185, 235),
                                          (0, 0, 0), -1)
                            cv2.putText(frame, ts, (5, 230), cv2.FONT_ITALIC,
                                        0.35, (255, 111, 255))
                            detectarObjetos(frame, detection_graph,
                                            category_index, sess)
                            print("grabando")
                            for x in range(20):
                                out.write(frame)
                            #out1.write(contornosimg)
                            #cv2.imshow("ventana", frame)
                    #else:
                    #movimiento=False

                    if movimiento == True:

                        count += 1
                        print("mov")
                        instanteInicial = time.time()

                        if count % 15 == 0:
                            print("movimiento a la hora " +
                                  time.strftime("%y_%m_%d_%H%M%S"))
                            nombre = "namepattern-" + time.strftime(
                                "%y-%m-%d- %H:%M:%S") + ".jpg"

                    else:
                        count = 0

                        instanteFinal = time.time()
                        tiempo = instanteFinal - instanteInicial  # Devuelve un objeto timedelta

                        print(tiempo)
                        if (tiempo > 2):
                            #cuando vuelva a haber movimiento empezara un nuevo video
                            #out = cv2.VideoWriter("GRABACIONES/"+time.strftime("%d_%m_%y_%H%M%S") + '.avi', fourcc, 20.0, (640, 480))
                            instanteInicial = instanteFinal
                            contadorMovimiento = 0
                        else:
                            if (contadorMovimiento == 1):
                                print("grabando")
                                #detectarObjetos(frame, detection_graph, category_index,sess)
                                timestamp = datetime.datetime.now()
                                ts = timestamp.strftime("%d %B %Y %I:%M:%S%p")
                                cv2.rectangle(frame, (2, 220), (185, 235),
                                              (0, 0, 0), -1)
                                cv2.putText(frame, ts, (5, 230),
                                            cv2.FONT_ITALIC, 0.35,
                                            (255, 111, 255))

                                out.write(frame)
                                #out1.write(contornosimg)
                                #cv2.imshow("ventana",frame)

                    movimiento = False

    print("camara")
Пример #20
0
def execute(path):
    cred = credentials.Certificate(
        "C:/tensorflow1/models/research/object_detection/firebase/schoolai-firebase-adminsdk-78lrk-064b6e6416.json"
    )
    firebase_admin.initialize_app(
        cred, {'databaseURL': 'https://schoolai.firebaseio.com'})
    print(path)
    print(datetime.now())
    MODEL_NAME = 'inference_graph'

    CWD_PATH = os.getcwd()

    PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                'frozen_inference_graph.pb')

    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    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)

    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')
    ref = db.reference('KATHIR/CAM1')
    video = cv2.VideoCapture(path)
    video.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
    video.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
    x = 0
    while (video.isOpened()):
        x = x + 1
        ret, frame = video.read()
        if (ret == True and x == 1):
            x = 0
            frame_expanded = np.expand_dims(frame, axis=0)

            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: frame_expanded})
            vis_util.visualize_boxes_and_labels_on_image_array(
                frame,
                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.30)
            now = datetime.now()
            ims = cv2.resize(frame, (960, 540))
            cv2.imshow(path, ims)
            if (scores[0, 0] > 0.65):
                curdate = str(datetime.now()).split(" ")
                hrmin = curdate[1].split(":")
                users_ref = ref.child(curdate[0] + '/' + hrmin[0] + ':' +
                                      hrmin[1])
                users_ref.set({'Flag': 1})
        if cv2.waitKey(1) == ord('q'):
            video.release()
            cv2.destroyAllWindows()
Пример #21
0
for index, each_class in enumerate(all_classes):
    print each_class
    item = label_map_proto.item.add()
    item.id = index + 1
    item.name = each_class

# print label_map_proto

label_map_string = text_format.MessageToString(label_map_proto)
label_map_path = '/home/research/DataServer/datasets/external/airbus_ship_detection/label_map/label_map.pbtxt'
with tf.gfile.GFile(label_map_path, 'wb') as fid:
    fid.write(label_map_string)

label_map_dict = label_map_util.get_label_map_dict(label_map_path)
label_map = label_map_util.load_labelmap(label_map_path)
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes=len(label_map_dict), use_display_name=True)
category_index = label_map_util.create_category_index(categories)

tf_record_data_path = '/home/research/DataServer/datasets/external/airbus_ship_detection/tfrecords'

NUM_SHARDS = 20
length_files = len(df)

num_per_shard = int(math.ceil(length_files / float(NUM_SHARDS)))
mp = multiprocessing.Pool(NUM_SHARDS)
results = []
for shard_id in range(NUM_SHARDS):

    print('processing sharid:' + str(shard_id))
def detect_objects(IMAGE_NAME, SCORE_THRESHOLD, desired_classes):

    # Name of the directory containing the object detection module we're using
    MODEL_NAME = 'inference_graph'
    #IMAGE_NAME = os.path.join('test','test4.jpg')

    # 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, MODEL_NAME,
                                'frozen_inference_graph.pb')

    # Path to label map file
    PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

    # Path to image
    PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_NAME)

    # Number of classes the object detector can identify
    NUM_CLASSES = 5

    # Load the label map.
    # Label maps map indices to category names, so that when our convolution
    # network predicts `5`, we know that this corresponds to `king`.
    # Here we use internal utility functions, but anything that returns a
    # dictionary mapping integers to appropriate string labels would be fine
    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_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, 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})

    print(classes.shape, scores.shape, boxes.shape)
    # Draw the results of the detection (aka 'visulaize the results')

    classes, scores, boxes = filter(classes, scores, boxes, desired_classes)
    print(classes.shape, scores.shape, boxes.shape)

    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=SCORE_THRESHOLD)

    cv2.imwrite('cache/temp.jpg', image)
Пример #23
0
def main(image_url):
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    # PATH_TO_CKPT =  'trainedModels/ssd_mobilenet_RoadDamageDetector.pb'
    PATH_TO_CKPT = 'model/sim.pb'
    # List of the strings that is used to add correct label for each box.
    # PATH_TO_LABELS = 'trainedModels/crack_label_map.pbtxt'
    PATH_TO_LABELS = 'model/sim.pbtxt'
    NUM_CLASSES = 8

    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='')

    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)

    IMAGE_SIZE = (12, 8)

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            # Definite input and output Tensors for detection_graph
            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.
            detection_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.
            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_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            # 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 represent how level of confidence for each of the objects.
            # 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')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            image = Image.open(image_url)
            # the array based representation of the image will be used later in order to prepare the
            # result image with boxes and labels on it.
            image_np = load_image_into_numpy_array(image)
            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image_np, axis=0)
            # Actual detection.
            (boxes, scores, classes,
             num) = sess.run([
                 detection_boxes, detection_scores, detection_classes,
                 num_detections
             ],
                             feed_dict={image_tensor: image_np_expanded})
            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                min_score_thresh=0.2,
                use_normalized_coordinates=True,
                line_thickness=8)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            count = 0
            print(scores)
            print(classes)
            for score in range(scores[0].shape[0]):
                if (scores[0][score] > 0.2):
                    print('s:', scores[0][score], 'damage: ',
                          classes[0][score])
                    count += 1
            name = image_url.split('/')[2].split('.')[0]
            name = name + '_predicted.png'
            plt.savefig('media/images/' + name)
            return (name, count)
Пример #24
0
#-*-coding:UTF-8-*-
from __future__ import print_function

import sys
import rospy
from std_msgs.msg import Int32
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import tensorflow.contrib.tensorrt as trt
import tensorflow as tf
import numpy as np
import time
from tf_trt_models.detection import download_detection_model, build_detection_graph
import cv2
from utils import label_map_util
label_map = label_map_util.load_labelmap('mscoco_label_map.pbtxt')
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes=90, use_display_name=True)
from geometry_msgs.msg import Pose2D
from lgsvl_msgs.msg import Detection2D
from lgsvl_msgs.msg import Detection2DArray
from lgsvl_msgs.msg import BoundingBox2D

run = cv2.imread('a.jpg')
cv2.namedWindow('run', 0)
cv2.imshow('run', run)
cv2.waitKey(1000)
cv2.destroyWindow('run')

global process_this_frame, boxes, scores, classes, num_detections, runMark
runMark = 0
Пример #25
0
def api_detection_view(request):
    detection = Detection.objects.all()

    if request.method == 'POST':
        serializer = DetectionSerializers

        MODEL_NAME = 'inference_graph'
        IMAGE_NAME = 'P10.jpg'

        CWD_PATH = os.getcwd()

        PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_NAME,
                                    'frozen_inference_graph.pb')
        PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')
        PATH_TO_IMAGE = os.path.join(CWD_PATH, IMAGE_NAME)

        NUM_CLASSES = 3

        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_expanded = np.expand_dims(image, 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.imshow('Object detector', image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

        serializer.save()

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
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='')


# ## Loading label map
# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine

# In[ ]:


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)


# ## Helper code

# In[ ]:


def load_image_into_numpy_array(image):
  (im_width, im_height) = image.size
  return np.array(image.getdata()).reshape(
      (im_height, im_width, 3)).astype(np.uint8)

def objectDetectionCap():  
    import cv2
    import numpy as np 
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile
    
    from collections import defaultdict
    from io import StringIO
    from matplotlib import pyplot as plt
    from PIL import Image
    

    # This is needed since the notebook is stored in the object_detection folder.
    #cwd = os.getcwd()
    #os.chdir('../lib')
    #sys.path.append("..")
    os.chdir('../lib')
    from object_detection.utils import ops as utils_ops

    
    if tf.__version__ < '1.4.0':
      raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')
    
    
    # ## Env setup
    
    
    # This is needed to display the images.
    get_ipython().magic('matplotlib inline')
    
    
    # ## Object detection imports
    # Here are the imports from the object detection module.
    
    os.chdir('../lib/object_detection')
    from utils import label_map_util
    
    from utils import visualization_utils as vis_util
    
    
    # # Model preparation 
    
    # ## Variables
    # 
    # Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing `PATH_TO_CKPT` to point to a new .pb file.  
    # 
    # By default we use an "SSD with Mobilenet" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies.
    
    
    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
    
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
    
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
    
    NUM_CLASSES = 90
    
   
    # ## Download Model
    
#    opener = urllib.request.URLopener()
#    opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
#    tar_file = tarfile.open(MODEL_FILE)
#    for file in tar_file.getmembers():
#      file_name = os.path.basename(file.name)
#      if 'frozen_inference_graph.pb' in file_name:
#        tar_file.extract(file, os.getcwd())
   
    
    # ## Load a (frozen) 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='')
    
    
    # ## Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    
    
    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)

    
    cap = cv2.VideoCapture(0)
    with detection_graph.as_default():
      with tf.Session(graph=detection_graph) as sess:
       ret = True
       while (ret):
          ret,image_np = cap.read()
          image_np_expanded = np.expand_dims(image_np, axis=0)
          image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
          boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
          scores = detection_graph.get_tensor_by_name('detection_scores:0')
          classes = detection_graph.get_tensor_by_name('detection_classes:0')
          num_detections = detection_graph.get_tensor_by_name('num_detections:0')
          (boxes, scores, classes, num_detections) = sess.run(
                  [boxes, scores, classes, num_detections],
                  feed_dict={image_tensor: image_np_expanded})
          vis_util.visualize_boxes_and_labels_on_image_array(
              image_np,
              np.squeeze(boxes),
              np.squeeze(classes).astype(np.int32),
              np.squeeze(scores),
              category_index,
              use_normalized_coordinates=True,
              line_thickness=8)
          cv2.imshow('image', cv2.resize(image_np,(1280,800)))
          if cv2.waitKey(25) & 0xFF == ord('q'):
              cv2.destroyAllWindows()
              cap.release()
              break

    os.chdir('../../doc')
Пример #28
0
    tf.import_graph_def(graph_def, name='')
    
# Enforce that no new nodes are added
detection_graph.finalize()

# Create the session that we'll use to execute the model
sess_config = tf.ConfigProto(
    log_device_placement=False,
    allow_soft_placement = True,
    gpu_options = tf.GPUOptions(
        per_process_gpu_memory_fraction=1
    )
)
sess = tf.Session(graph=detection_graph, config=sess_config)

label_map = label_map_util.load_labelmap(R_DIR + 'object_detection/data/mscoco_label_map.pbtxt')
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)

print(time.ctime() + " Graph loaded")

def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape(
        (im_height, im_width, 3)).astype(np.uint8)

@app.route('/')
def classify():

    t = time.time()
    print('-----------------------')


"""
CONVERT CSV TO RECORD
"""
# ## Loading label map
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = f'../datasets/{DATASET_NAME}/data/{PBTXT_FILE}'
RELATIVE_LABELS_PATH = os.path.abspath(
    os.path.dirname(__file__) + '/' + PATH_TO_LABELS)

NUM_CLASSES = len(label_map_util.get_label_map_dict(RELATIVE_LABELS_PATH))

# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
label_map = label_map_util.load_labelmap(RELATIVE_LABELS_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)


# Filter through .pbtxt to return integer id
def class_text_to_int(row_label):
    indexList = list(
        filter((lambda category: category['name'] == row_label), categories))

    # check if list is empty before indexing
    if not indexList:
        sys.exit(
            "ERROR: Value(s) in csv is not in pbtxt. Ensure correct labels are in csv and rerun script."
        )
def main():
    print("starting program . . .")

    if not checkIfNecessaryPathsAndFilesExist():
        return
    # end if

    # this next comment line is necessary to avoid a false PyCharm warning
    # noinspection PyUnresolvedReferences
    if StrictVersion(tf.__version__) < StrictVersion('1.5.0'):
        raise ImportError('Please upgrade your tensorflow installation to v1.5.* or later!')
    # end if

    # load a (frozen) TensorFlow model into memory
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(FROZEN_INFERENCE_GRAPH_LOC, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        # end with
    # end with

    # Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(LABELS_LOC)
    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)

    imageFilePaths = []
    for imageFileName in os.listdir(TEST_IMAGE_DIR):
        if imageFileName.endswith(".jpg"):
            imageFilePaths.append(TEST_IMAGE_DIR + "/" + imageFileName)
        # end if
    # end for

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            for image_path in imageFilePaths:

                print(image_path)

                image_np = cv2.imread(image_path)

                if image_np is None:
                    print("error reading file " + image_path)
                    continue
                # end if

                # Definite input and output Tensors for detection_graph
                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.
                detection_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.
                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')

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                (boxes, scores, classes, num) = sess.run(
                    [detection_boxes, detection_scores, detection_classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                vis_util.visualize_boxes_and_labels_on_image_array(image_np,
                                                                   np.squeeze(boxes),
                                                                   np.squeeze(classes).astype(np.int32),
                                                                   np.squeeze(scores),
                                                                   category_index,
                                                                   use_normalized_coordinates=True,
                                                                   line_thickness=8)
                cv2.imshow("image_np", image_np)
                cv2.waitKey()
Пример #31
0
def main():
    print("starting program . . .")

    if not checkIfNecessaryPathsAndFilesExist():
        return
    # end if
    classifications = []
    # for each line in the label file . . .
    for currentLine in tf.gfile.GFile(RETRAINED_LABELS_TXT_FILE_LOC):
        # remove the carriage return
        classification = currentLine.rstrip()
        # and append to the list
        classifications.append(classification)
    # this next comment line is necessary to avoid a false PyCharm warning
    # noinspection PyUnresolvedReferences
    if StrictVersion(tf.__version__) < StrictVersion('1.5.0'):
        raise ImportError(
            'Please upgrade your tensorflow installation to v1.5.* or later!')
    # end if

    # load a (frozen) TensorFlow model into memory
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        with tf.gfile.GFile(FROZEN_INFERENCE_GRAPH_LOC, 'rb') as fid:
            od_graph_def = tf.GraphDef()
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        # end with
    # end with
    classification_graph = tf.Graph()
    with classification_graph.as_default():
        with tf.gfile.FastGFile(RETRAINED_GRAPH_PB_FILE_LOC,
                                'rb') as retrainedGraphFile:
            # instantiate a GraphDef object
            graphDef = tf.GraphDef()
            # read in retrained graph into the GraphDef object
            graphDef.ParseFromString(retrainedGraphFile.read())
            # import the graph into the current default Graph, note that we don't need to be concerned with the return value
            tf.import_graph_def(graphDef, name='')
    # end with
    # Loading label map
    # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`.  Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
    label_map = label_map_util.load_labelmap(LABELS_LOC)
    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)

    with tf.Session(graph=detection_graph) as sessd:
        with tf.Session(graph=classification_graph) as sessc:
            for imageFileName in os.listdir(TEST_IMAGE_DIR):
                if not imageFileName.endswith(".jpg"):
                    continue
                image_path = TEST_IMAGE_DIR + "/" + imageFileName
                print(image_path)
                image_np = cv2.imread(image_path)

                if image_np is None:
                    print("error reading file " + image_path)
                    continue
                # end if

                # Definite input and output Tensors for detection_graph
                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.
                detection_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.
                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')

                # Expanded dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                # Actual detection.
                (boxes, scores, classes,
                 num) = sessd.run([
                     detection_boxes, detection_scores, detection_classes,
                     num_detections
                 ],
                                  feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                img_height, img_width, img_channel = image_np.shape
                THRESHOLD = 0.4
                #print(scores[0])
                i = 0
                while scores[0][i] >= THRESHOLD and i <= 99:
                    ymin, xmin, ymax, xmax = boxes[0][i]
                    x_up = int(xmin * img_width)
                    y_up = int(ymin * img_height)
                    x_down = int(xmax * img_width)
                    y_down = int(ymax * img_height)
                    cropped_image = image_np[y_up:y_down, x_up:x_down]
                    detected_ocr = OCR.ocr(cropped_image)
                    # get the final tensor from the graph
                    finalTensor = sessc.graph.get_tensor_by_name(
                        'final_result:0')
                    # convert the OpenCV image (numpy array) to a TensorFlow image
                    tfImage = np.array(cropped_image)[:, :, 0:3]
                    # run the network to get the predictions
                    predictions = sessc.run(finalTensor,
                                            {'DecodeJpeg:0': tfImage})
                    # sort predictions from most confidence to least confidence
                    sortedPredictions = predictions[0].argsort(
                    )[-len(predictions[0]):][::-1]

                    prediction = sortedPredictions[0]
                    strClassification = classifications[prediction]
                    if strClassification.endswith("s"):
                        strClassification = strClassification[:-1]
                    confidence = predictions[0][prediction]
                    scoreAsAPercent = confidence * 100.0
                    label = strClassification + ", " + "{0:.2f}".format(
                        scoreAsAPercent)
                    cv2.putText(image_np, label, (int(x_up), int(y_down)),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 1)
                    #cv2.putText(image_np, detected_ocr, (int((x_down+x_up)/2), int(y_up), cv2.FONT_HERSHEY_SIMPLEX,0.8, (255, 0, 0),1)
                    i = i + 1
                    #cropped_image_path = os.getcwd() + "/detected_number_plates/" + imageFileName[:-4] + str(i) + ".jpg"
                    #cv2.imwrite(cropped_image_path, cropped_image)

                vis_util.visualize_boxes_and_labels_on_image_array(
                    image_np,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=3)

                # cv2.imshow("image_np", image_np)
                # while cv2.waitKey() != 32:
                #     pass
                cv2.imwrite(results_dir + imageFileName, image_np)
Пример #32
0
from utils import label_map_util
from utils import visualization_utils as vis_util

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='')


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 load_image():
    ret, frame = cap.read()
    array = frame
    array=array.astype(np.uint8)
    return array

test_image_path=[os.path.join('/home/nvidia/data/projects/tensorflow-object-detection-api/models/research/object_detection/test_images','image{}.jpg'.format(i)) for i in range(1,2)]

show_size=(12,8)
i = 0
Пример #33
0
def detection():

    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_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_expanded = np.expand_dims(image_rgb, 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')

    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.70)

    coordinates = vis_util.return_coordinates(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.70)

    for coordinate in coordinates:
        print(coordinate)
        #ymin,ymax,xmin,xmax
        (y1, y2, x1, x2, accuracy, classification) = coordinate

    with open(os.path.join(
            CWD_PATH,
            "json_hand/" + "script" + IMAGE_NAME.split(".")[0] + ".json"),
              "w",
              encoding='utf-8') as f:
        json.dump(coordinates, f, ensure_ascii=False, indent=4)
        f.write('\n')

    output = image.copy()
    cv2.imshow('Object detector', output)

    # Press any key to close the image
    cv2.waitKey(0)

    # Clean up
    cv2.destroyAllWindows()