def img_de(IMAGE_NAME,MODEL_NAME):
    # 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(MODEL_NAME,'frozen_inference_graph.pb')

    # Path to label map file
    PATH_TO_LABELS = os.path.join(CWD_PATH,'training','object-detection.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_expanded = np.expand_dims(image, axis=0)

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

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

    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)

    # All the results have been drawn on image. Now display the image.
    cv2.imshow('Object detector', image)

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

    # Clean up
    cv2.destroyAllWindows()
import matplotlib.pyplot as plt

from utils import visualization_utils as vis_util
from utils import label_map_util

from multiprocessing.dummy import Pool as ThreadPool

MAX_NUMBER_OF_BOXES = 10
MINIMUM_CONFIDENCE = 0.9

PATH_TO_LABELS = 'annotations/label_map.pbtxt'
PATH_TO_TEST_IMAGES_DIR = 'test_images'

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(
    label_map, max_num_classes=sys.maxsize, use_display_name=True)
CATEGORY_INDEX = label_map_util.create_category_index(categories)

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


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 detect_objects(image_path):
    image = Image.open(image_path)
Example #3
0
def slika(image):
    def load_image_into_numpy_array(image1):
        (im_width, im_height) = image1.size
        return np.array(image1.getdata()).reshape(
            (im_height, im_width, 3)).astype(np.uint8)

    sys.path.append("..")
    PATH_TO_CKPT = 'inference_graph/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', 'labelmap')
    NUM_CLASSES = 2
    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:
            # 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')
            slika = Image.open(image)
            image_np = load_image_into_numpy_array(slika)
            # 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,  # True
                line_thickness=4,
                min_score_thresh=0.8,
                agnostic_mode=False)
            image_return = Image.fromarray(image_np)
            image_return.save("mysite/image_return2.jpg")
    return image_return
Example #4
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()
Example #5
0
def Object_Classification(image):
    # This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")

    # Import utilites
    from utils import label_map_util

    # Name of the directory containing the object detection module we're using
    MODEL_NAME = 'inference_graph'
    IMAGE_NAME = 'test6.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 = 4

    # 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_expanded = np.expand_dims(image, axis=0)

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

    animals = [0, 0, 0, 0]
    lengthc = np.size(classes)
    lengths = np.size(scores)
    if lengthc == lengths:
        for i in range(lengths):
            if scores[0, i] > .7:
                if classes[0, i] == 1:
                    animals[0] += 1
                elif classes[0, i] == 2:
                    animals[1] += 1
                elif classes[0, i] == 3:
                    animals[2] += 1
                elif classes[0, i] == 4:
                    animals[3] += 1
            else:
                break
    return animals
def RunObjectRecognitionModel():
    pathToCheck = os.path.join(os.getcwd(), Model().ModelName, Model().Graph)

    if not os.path.exists(pathToCheck):
        print("Downloading Model...please wait...")
        Model().Download()

    GraphPath = Model().GetCKPT_Path()

    detection_graph = tf.Graph()

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

    LabelsPath = Model().GetLabelsPath()
    label_map = label_map_util.load_labelmap(LabelsPath)
    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)

    TEST_IMAGE_PATHS = _LoadImages()

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

        return_dict = {}
        for image_n, image_path in enumerate(_LoadImages()):
            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)
            # 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})

            img_number, avg_score, angle = FindDetectedObjects(
                category_index, np.squeeze(boxes), np.squeeze(classes),
                np.squeeze(scores), image_path, 'person')

            if avg_score > 0.0 or avg_score != 0:
                return_dict[img_number] = {"Score": avg_score, "Angle": angle}
            else:
                pass
            # 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=4)
            # plt.imshow(image_np)
            # plt.show()

        print(return_dict)
        return return_dict
Example #7
0
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()
                cv2.imwrite("image_np.jpg", image_np)
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 process_image(image):
    #Retain the initial image before ssd bounding boxes are drawn.
    #create a deep copy 
    imgo = copy.deepcopy(image)
    
    # 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

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

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

            # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
            image_np_expanded = np.expand_dims(image, 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.squeeze(boxes),
                 np.squeeze(classes).astype(np.int32),
                 np.squeeze(scores),
                 category_index,
                 use_normalized_coordinates=True,
                 line_thickness=8)
           #print("Tensorflow image process: boxes: {}".format(boxes))
           #print("scores: {}".format(scores))
           #print("classes: {}".format(classes))
           #print("num: {}".format(num))

    #plt.imshow(image)
    #plt.show()
    
    ##from my code in Udacity CapstoneProject for traffic light color classification########
    #initialize all variables
    yellowLight = False
    redLight = False
    greenLight = False
    yellowImg = image
    redImg = image
    greenImg = image
    result = image

    #The size of one traffic light is about 50 in x direction,125 in y direction
    #The center of the image is:
    x = image.shape[1]/2 
    y = image.shape[0]/2 

    tl_loc = traffic_light_location(boxes, scores, classes, image.shape)
    #print(tl_loc)
    # No traffic lights found, look in Bernards original location.
    if len(tl_loc) == 0:
        tl_loc = 0 #print("No Lights found by NN!")
    else:     
        ###################green color detection##########
        # define range of green color in HSV
        lower_green = np.array([60,125,125]) #100,100])
        upper_green = np.array([120,255,255])
        [clr_ID, greenImg] = get_light_color(image, imgo, tl_loc, lower_green, upper_green)
        if (clr_ID == "GREEN"):
            greenLight = True
        ##################red color detection#################
        # define range of red color in HSV
        lower_red = np.array([170,125,125]) 
        upper_red = np.array([179,255,255])
        [clr_ID, redImg] = get_light_color(image, imgo, tl_loc, lower_red, upper_red)
        if (clr_ID == "RED"):
            redLight = True


        ###########yellow traffic light detection###########
        # define range of orange color in HSV
        lower_yellow = np.array([5,150,150]) 
        upper_yellow = np.array([15,255,255]) #real amber traffic light works 15,255,255])
        [clr_ID, yellowImg] = get_light_color(image, imgo, tl_loc, lower_yellow, upper_yellow)
        if (clr_ID == "YELLOW"):
            yellowLight = True
            	
        if ((yellowLight == True) and (redLight == False) 
             and (greenLight == False)):
            clr_ID = "YELLOW"
            result = yellowImg

        elif ((yellowLight == False) and (redLight == True) 
            and (yellowLight == False)):
            clr_ID = "RED"
            result = redImg

        elif ((yellowLight == False) and (redLight == False) 
             and (greenLight == True)):
            clr_ID = "GREEN" 
            result = greenImg
        else:
            clr_ID = "UNKNOWN"
            result = image
        
        #plt.imshow(result)
        #plt.show()
        #print("Traffic Light color_ID: {}".format(clr_ID))
    return result        
Example #10
0
def load_models():
    # load models 시작시간
    code_start = time.time()
    config = tf.compat.v1.ConfigProto()
    config.gpu_options.allow_growth = True
    session = tf.compat.v1.Session(config=config)

    # Name of the directory containing the object detection module we're using
    MODEL_NAME = 'models'
    LABELMAP_NAME = 'labelmap'

    # 결과 출력(cv.imwrite)해서 확인하려면 output 경로 수정
    # output_dir = "test_images_output\\"

    # Grab path to current working directory
    CWD_PATH = os.getcwd()

    model_list = os.listdir(CWD_PATH + "\\" + MODEL_NAME)
    labelmap_list = os.listdir(CWD_PATH + "\\" + LABELMAP_NAME)

    # =========================================================================
    # model 에 따라서 바뀌는 값들 배열
    sess = {}
    detection_graph = {}
    categories = {}
    category_index = {}
    i = 0
    # 모델 불러오는 부분 => 반복 (여러 모델 불러오기)
    for model, labelmap in zip(model_list, labelmap_list):
        # print(str(len(model_list))+"개 중에 "+str(i+1)+"번째 모델 불러오는 중")
        # print(model+", "+labelmap)

        # 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, model)
        # print("model_path : "+PATH_TO_CKPT)

        # Path to label map file
        PATH_TO_LABELS = os.path.join(CWD_PATH, LABELMAP_NAME, labelmap)
        # print("label_path : "+PATH_TO_LABELS)

        # Number of classes the object detector can identify
        # max_num_classes 를 지정해주는 값
        # 최대 라벨 개수로 지정해주면 될 것 같음
        # label class 개수보다 작은 값이 들어가면 N/A 라고 나옴
        NUM_CLASSES = 2

        # Load the label map.
        # Label maps map indices to category names
        # 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[i] = label_map_util.convert_label_map_to_categories(
            label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        category_index[i] = label_map_util.create_category_index(categories[i])
        # print(category_index[i])

        # Load the Tensorflow model into memory.
        # Tensorflow 모델을 메모리에 로드
        # detection_graph 배열로 만들어서 이름 바꿔주면서 image 넣기
        detection_graph[i] = tf.Graph()
        with detection_graph[i].as_default():
            od_graph_def = tf.compat.v1.GraphDef()
            with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
                # print(PATH_TO_CKPT)
                serialized_graph = fid.read()
                od_graph_def.ParseFromString(serialized_graph)
                tf.import_graph_def(od_graph_def, name='')

            sess[i] = tf.Session(graph=detection_graph[i])
        i = i + 1
    # print("모델 불러오는데 걸리는 시간 : "+str(round(time.time() - code_start,5)))
    # ==========================================================================

    return sess, detection_graph, category_index
# The path to the current directory
CURRENT_DIR = os.getcwd()

# The path to the Frozen Inference Graph
FIG_DIR = os.path.join(CURRENT_DIR, FIG_MODEL_FOLDERNAME, FIG_FILENAME)

# The path to the Labelmap
LABELMAP_DIR = os.path.join(CURRENT_DIR, LABELMAP_FOLDERNAME,
                            LABELMAP_FILENAME)

# The path to the Video file
VIDEO_DIR = os.path.join(CURRENT_DIR, VIDEO_NAME)

# Load the labelmap to convert ID numbers the model uses into usable category names
labelmap = lmu.load_labelmap(LABELMAP_DIR)
categories = lmu.convert_label_map_to_categories(
    labelmap, max_num_classes=NUMBER_OF_CLASSES, use_display_name=True)
cat_index = lmu.create_category_index(categories)

print("Loaded directories, starting to load Tensorflow Model")

# Load the Tensorflow model
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(FIG_DIR, '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 main(argv=None):
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_list
    try:
        os.makedirs(FLAGS.output_dir)
    except OSError as e:
        if e.errno != 17:
            raise
    MODEL_NAME = FLAGS.checkpoint_path
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
    print(FLAGS.test_data_path, FLAGS.output_dir)
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'oid_bbox_trainable_label_map.pbtxt')
    NUM_CLASSES = 1000
    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 a (frozen) Tensorflow model into memory.
    # In[6]:
    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='')

    classes_detected = dict()
    # In[26]:
    with detection_graph.as_default():
        with tf.Session() as sess:
            # Get handles to input and output tensors
            ops = tf.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.get_default_graph().get_tensor_by_name(
                        tensor_name)

            image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
            TEST_IMAGE_PATHS = get_images()
            for image_path in TEST_IMAGE_PATHS:
                # the array based representation of the image will be used later in order to prepare the
                # result image with boxes and labels on it.

                start = time.time()
                image = cv2.imread(image_path)
                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]

                # Run inference
                output_dict = sess.run(tensor_dict,
                                       feed_dict={image_tensor: np.expand_dims(image, 0)})
                # print('[Net timing] {}'.format(time.time()-start))

                # start = time.time()
                # all outputs are float32 numpy arrays, so convert types as appropriate
                classes = output_dict[
                    'detection_classes'][0].astype(np.uint8)
                boxes = output_dict['detection_boxes'][0]
                scores = output_dict['detection_scores'][0]

                output_list = []
                for i in range(boxes.shape[0]):
                    if scores is None or scores[i] > 0.1:
                        if classes[i] in category_index.keys():
                            class_name = category_index[classes[i]]['name']
                        else:
                            class_name = 'N/A'
                        display_str = str(class_name)
                        # ymin, xmin, ymax, xmax = box
                        # box_coord = (left, right, top, bottom)
                        try:
                            classes_detected[display_str] += 1
                        except KeyError:
                            classes_detected[display_str] = 1
                        output_list.append(
                            str(classes[i]) + ',' + "{0:.2f}".format(scores[i]) + ',' + display_str + ':' + ','.join(
                                map(str, boxes[i])))
                if len(output_list) > 0:
                    res_file = os.path.join(
                        FLAGS.output_dir,
                        '{}.txt'.format(
                            os.path.basename(image_path).split('.')[0]))
                    with open(res_file, 'w') as f:
                        f.write('\n'.join(output_list))

                # print('[Writing timing] {}'.format(time.time() - start))

    res_file = os.path.join(
        FLAGS.output_dir,'{}.txt'.format('meta-classes'))
    with open(res_file, 'w') as f:
        for key, value in sorted(classes_detected.items()):
            f.write(str(key) + ',' + str(value) + '\n')
Example #13
0
def main(argv):

	ops_shop_id=""
	ops_gpu=0
	#ops_date=""

	try:
		opts, args = getopt.getopt(argv,"hd:g:s:f:t",["interval=","gpu=","shop_id=","from=","to="])
	except getopt.GetoptError:
		print ('detection_8_off.py -d 2019-05-22 -g 0 -s 0 -f 0 -t 100')
		sys.exit(2)
	for opt, arg in opts:
		if opt == '-h':
			print ('detection_8_off.py -d 2019-05-22 -g 0 -s 0 -f 0 -t 100')
			sys.exit()
		elif opt in ("-d", "--date"):
			ops_date = arg
		elif opt in ("-g", "--gpu"):
			ops_gpu = int(arg)
		elif opt in ("-s", "--shop_id"):
			ops_shop_id = int(arg)
		elif opt in ("-f", "--from"):
			ops_from = int(arg)
		elif opt in ("-t", "--to"):
			ops_from = int(arg)
	#print ('Interval: ', ops_interval)
	#print ('Gpu: ', ops_gpu)

	os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"   # see issue #152
	os.environ["CUDA_VISIBLE_DEVICES"]=str(ops_gpu)
	#os.environ["CUDA_VISIBLE_DEVICES"]='XLA_GPU:0'

	screenshots_path	= "/home/alex/storage/shop_screens/video/"
	processed_path		= "/home/alex/storage/shop_screens/video/processed/"
	save_images_path	= "/home/alex/storage/rcimages/"
	shop_names			= ["Altuf","Avangard","Mar","Tag"]
	NUM_CLASSES 		= 18
	PATH_TO_CKPT		= '../../inference_v7_12097/frozen_inference_graph.pb'
	PATH_TO_LABELS		= '../training/mscoco_label_map.pbtxt'
	
	score_limit	= 0.7
	sizeMin	= 10
	sizeMax	= 1090
	font = cv2.FONT_HERSHEY_SIMPLEX
	
	#with tf.device('/device:XLA_GPU:0'):	
	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)
	
	file_date=filedate()
	
	# ==== Temporary code
	# query	= "SELECT path FROM files where path<>'' ORDER BY path"
	# cursor.execute(query)
	# sql_answer=cursor.fetchall()
	# # == prepare array
	# files_from_sql=[]
	# for record_current in sql_answer:
		# files_from_sql.append(record_current[0])
	# print("Recognized files array size: "+str(len(files_from_sql)))
	
	with detection_graph.as_default():		
		with tf.Session(graph=detection_graph) as sess:
			
			conn = pymssql.connect(server='10.2.4.25', user='******', password='******', database='shopEvents')
			cursor = conn.cursor()
				
			#step in every shop
			#if (True):
			#for shop_id in range(4):
			#if ops_shop_id!="" and shop_id!=int(ops_shop_id):
			#	continue
			#dtnow=datetime.datetime.now()
			#today=dtnow.strftime("%Y-%m-%d_08-56")
			#files = os.listdir(screenshots_path+shop_names[shop_id]+"/grabs/")
			#images = list(filter(lambda x: today in x, files))
			
			#if len(files)>0:
			#	send_to_telegram(chat,str(len(files))+" "+shop_names[shop_id]+" begin")
			while (True):				

				detectedImagesCount	= 0
				
				query	= "SELECT file_full_path,shop_id,file_name FROM files_to_process where gpu_id = "+str(ops_gpu)+" ORDER BY file_id"
				cursor.execute(query)
				sql_answer=cursor.fetchall()
				# == prepare array
				#files_from_sql=[]
				#files=[]
				#bar = progressbar.ProgressBar(maxval=len(sql_answer), widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
				#bar.start()
				###send_to_telegram(chat,"detection "+str(len(sql_answer))+" files started")
				print("len(sql_answer)",len(sql_answer))
				for record_current in sql_answer:
					
					file_full_path	= record_current[0]
					shop_id			= record_current[1]
					#file_id			= record_current[2]
					file_name_sql	= record_current[2]
					#	files_from_sql.append(record_current[0])
						#print("Recognized files array size: "+str(len(files_from_sql)))
					#print("file_full_path",file_full_path)
					
					#files=files.sort()
					#files = natsorted(files, alg=ns.PATH)
					#print("Processing "+str(len(files))+" files of "+shop_names[shop_id])				
					#for file_id in range(len(files)):				
					
					#bar.update(file_id+1)
					
					file_sql_query	= ""
					
					#file_full_path	= screenshots_path+shop_names[shop_id]+"/grabs/"+files[file_id]
					
					if not os.path.isfile(file_full_path):
						#print(file_full_path)
						#exit()
						continue
					
					#if os.stat(file_full_path).st_size==0:
						#drop file
					#	os.remove(file_full_path)
						#print(files[file_id],"removed as empty")
					#	continue
					#if file_full_path.find(".jpg")==-1:
						#print(files[file_id],"skipped as not jpg file")
					#	continue
					
					#file_date.update(files[file_id])
					file_date.update(file_name_sql)
					
					# Temporary code
					# if file_full_path in files_from_sql:
						# file_full_path_new	= processed_path+shop_names[shop_id]+"_"+files[file_id]
						# os.rename(file_full_path,file_full_path_new)
						# move_sql_query = "INSERT INTO files (path,date,shop_id) VALUES ('"+file_full_path_new+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
						# cursor.execute(move_sql_query)
						# conn.commit()
						# continue
						
					# ============ print(shop_id,"recognizing:",files[file_id],file_id,"of",len(files))
					
					#print(file_date.sqlFormat())
					#print("y",file_date.year)
					#print("date extracted from filename")
					#exit()
					
					image_np = cv2.imread(file_full_path)
					#image_original	= image_np
					image_original	= image_np.copy()
					#image_original	= copy.deepcopy(image_np)
					
					#RECOGNIZE++
					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})
					final_score = np.squeeze(scores)
					
					#get objects detected count
					objectsDetectedCount	= 0
					score_summ	= 0
					npsboxes=np.squeeze(boxes)
					imageheight, imagewidth  = image_np.shape[:2]
																						
					for i in range(100):
						
						current_class_id	= np.squeeze(classes).astype(np.int32)[i]
						current_class_name	= category_index[current_class_id]['name']
						
						if (scores is None or final_score[i] > score_limit) and "banknotes" in current_class_name:
							
							
							rx1=int(npsboxes[i][1]*imagewidth)	#1 xLeft
							ry1=int(npsboxes[i][0]*imageheight)	#0 yTop
							rx2=int(npsboxes[i][3]*imagewidth)	#3 xRight
							ry2=int(npsboxes[i][2]*imageheight)	#2 yBottom
							
							xlen=rx2-rx1
							ylen=ry2-ry1
							
							if xlen>sizeMin and xlen<sizeMax and ylen>sizeMin and ylen<sizeMax:								
								
								#file_name	= files[file_id].replace(".jpg","")								
								file_name	= file_name_sql.replace(".jpg","")
								
								file_name_all_boxes	= file_name+"_all_boxes.jpg"
								save_path_all_boxes	= save_images_path+str(shop_id)+"/original/"+file_name_all_boxes
								file_name_original	= file_name+".jpg"
								save_path_original	= save_images_path+str(shop_id)+"/original/"+file_name_original
								file_name_box	= file_name+"_"+str(objectsDetectedCount)+".jpg"
								save_path_box	= save_images_path+str(shop_id)+"/boxed/"+file_name_box								
								#file_sql_query	= file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('"+save_path_original+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
								#file_sql_query	= file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('"+save_path_all_boxes+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"								
								file_sql_query	= file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('"+save_path_box+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
								query	= "INSERT INTO events (eventDate,objectsCount,middleScore,FileName,shop_id,box_id,box_left,box_right,box_top,box_bottom,filename_original,filename_box,file_source_path) VALUES ('"+file_date.sqlFormat()+"',1,"+str(final_score[i])+",'"+file_name_all_boxes+"',"+str(shop_id)+","+str(objectsDetectedCount)+","+str(rx1)+","+str(rx2)+","+str(ry1)+","+str(ry2)+",'"+file_name_original+"','"+file_name_box+"','"+file_full_path+"')"
								cursor.execute(query)
								conn.commit()								
								score_summ	= score_summ+final_score[i]
								image_np_current_box	= image_original.copy()
								
								#object_description	= str(round(final_score[i]*100))+"% "+current_class_name
								object_description	= str(round(final_score[i]*100))+"%"
								fontScale              = 0.5								
								lineType               = 2
								
								#boxes
								cv2.rectangle(image_np_current_box,	(rx1,ry1) 	, (rx2,ry2)			, (255,0,0) , 2)	#regular box
								cv2.rectangle(image_np_current_box,	(rx1,ry1+1)	, (rx1+200,ry1-15)	, (0,255,0) , -1)	#text background
								cv2.rectangle(image_np,				(rx1,ry1)	, (rx2,ry2)			, (0,255,0) , 2)	#regular box
								cv2.rectangle(image_np,				(rx1,ry1+1)	, (rx1+200,ry1-15)	, (0,255,0) , -1)	#text background								
								
								cv2.putText(image_np_current_box,	object_description, (rx1,ry1), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (0,0,0), lineType)
								cv2.putText(image_np,				object_description, (rx1,ry1), cv2.FONT_HERSHEY_SIMPLEX, fontScale, (0,0,0), lineType)
								
								#save ONE BOX
								cv2.imwrite(save_path_box, image_np_current_box)
								print("cv2.imwrite image_np_current_box:",save_path_box)
								objectsDetectedCount+=1
								
					if objectsDetectedCount>0:						
						score_summString	= '%.2f'%(score_summ/objectsDetectedCount)#middle
						score_summStringH	= '%.2f'%(score_summ/objectsDetectedCount*100)#middle
					else:
						score_summString	= "0"
						score_summStringH	= "0"
					
					cv2.putText(image_np,str(objectsDetectedCount)+":"+score_summStringH+"%",(10,150), font, 1,(255,255,255),1,cv2.LINE_AA)
					#RECOGNIZE--
					
					if objectsDetectedCount>0:						
						#save ORIGINAL
						#cv2.imwrite(save_path_original, image_original)
						#save ALL BOXES
						cv2.imwrite(save_path_all_boxes, image_np)
						print("cv2.imwrite image_np:",save_path_all_boxes)
						detectedImagesCount+=1
						#file_sql_query	= file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('"+save_path_original+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
						file_sql_query	= file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('"+save_path_all_boxes+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"

					#drop source file
					#os.remove(file_full_path) # <============== move
					#file_full_path_new	= processed_path+shop_names[shop_id]+"_"+files[file_id]
					file_full_path_new	= processed_path+shop_names[shop_id]+"_"+file_name_sql
					os.rename(file_full_path,file_full_path_new)
					print("os.rename From:",file_full_path,"To:",file_full_path_new)
					
					# == Save filename to sql					
					#move_sql_query = "INSERT INTO files (path,date,shop_id) VALUES ('"+file_full_path_new+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
					file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('"+file_full_path_new+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
					#cursor.execute(move_sql_query)
					cursor.execute(file_sql_query)
					conn.commit()
					
					#remove record from file_to_process
					file_to_process_query	= "DELETE FROM files_to_process where file_full_path='"+file_full_path+"'"
					cursor.execute(file_to_process_query)
					conn.commit()

				
				#bar.finish()				
				#send_to_telegram(chat,str(detectedImagesCount)+" detected by "+str(ops_gpu)+" gpu")	
				
				#file_to_process_query	= "DELETE FROM files_to_process where gpu_id = "+str(ops_gpu)
				#cursor.execute(file_to_process_query)
				#conn.commit()
				print("calculating sleep time..")
				task_year	= (datetime.datetime.now() + datetime.timedelta(days=1)).year		
				task_month	= (datetime.datetime.now() + datetime.timedelta(days=1)).month
				task_day	= datetime.datetime.now().day
				task_hour	= 21
				task_minute	= 0
				sleeptime	= datetime.datetime(task_year, task_month, task_day, task_hour, task_minute)-datetime.datetime.now()
				print(str(ops_gpu)+" gpu job complete. sleeping",sleeptime.seconds,"seconds")
				#send_to_telegram(chat,str(detectedImagesCount)+" images detected. Exit")
				Going to sleep for "+str(sleeptime.seconds)+" seconds")				
				time.sleep(sleeptime)
Example #14
0
# Path to label map file
PATH_TO_LABELS = os.path.join(CWD_PATH, 'training', 'labelmap.pbtxt')

# Path to video
PATH_TO_VIDEO = os.path.join(CWD_PATH, VIDEO_NAME)

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

# 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
Example #15
0
def main(args):

    count = 0
    start = False
    frame_processed = 0

    num_frame = args.num_frame
    num_transform = args.num_transform

    subfolder = args.subfolder
    save_dir = os.path.join(args.save_dir, subfolder)
    if not os.path.isdir(save_dir):
        os.mkdir(save_dir)

    labelmap_dir = args.labelmap_dir
    inference_graph_dir = args.inference_graph_dir

    sequence = create_sequence()

    label_map = label_map_util.load_labelmap(labelmap_dir)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=2, 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(inference_graph_dir, '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')

    #Font for text
    font = cv2.FONT_HERSHEY_SIMPLEX

    # Initialize webcam feed
    video = cv2.VideoCapture(0)
    ret = video.set(3, WIDTH)
    ret = video.set(4, HEIGHT)

    xml_tree = ET.parse(args.xml_template)

    while (True):

        ret, frame = video.read()

        key = cv2.waitKey(1)
        # Press 'q' to quit
        if key == ord('q'):
            break
        # Press 's' to start
        elif key == ord('s'):
            start = True
            start_time = time.time()

        if not start:
            cv2.putText(frame, "Waiting", (20, 40), font, 1, (0, 255, 0), 2,
                        cv2.LINE_AA)
            cv2.putText(frame, "Press s to start", (20, 70), font, 1,
                        (0, 255, 0), 2, cv2.LINE_AA)
            cv2.imshow('Data collector', frame)
            continue

        count += 1
        if count % num_frame == 0:
            count = 0

            frame_processed += 1

            frames = np.array([frame for _ in range(num_transform)],
                              dtype=np.uint8)
            frames = sequence(images=frames)
            frames = np.vstack([frames, np.expand_dims(frame, axis=0)])

            img_paths, xml_paths = get_names(save_dir, subfolder,
                                             frame_processed,
                                             num_transform + 1)
            save_imgs(frames, img_paths)
            frame_processed += num_transform + 1

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

            for i in range(boxes.shape[0]):
                box = filter_boxes(boxes[i], scores[i], classes[i],
                                   CONFIDENCE_THRESHOLD, (HEIGHT, WIDTH))
                if box.shape[0] == 0:
                    continue
                get_and_save_xml(xml_tree, img_paths[i], box[0], xml_paths[i])

            text = "{:.1f}s".format(time.time() - start_time)
            cv2.putText(frame, text, (20, 40), font, 1, (0, 255, 0), 2,
                        cv2.LINE_AA)
            cv2.imshow('Data collector', frame)

    # Clean up
    video.release()
    cv2.destroyAllWindows()
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()
Example #17
0
def create_kitti_labels(output_path, label_map_path, label_dir, calib_dir,
                        image_dir, graph_dir, examples):
    grid_map_data_resolution = 0.1
    grid_map_data_origin_idx = np.array([79, 35])

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

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

    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_boxes_rot = detection_graph.get_tensor_by_name(
                'detection_boxes_rot: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')
            for idx, example in enumerate(examples):
                label_calib_name = '%06d' % (int(example)) + '.txt'
                path_label = os.path.join(label_dir, label_calib_name)
                label_name = '%06d' % (idx) + '.txt'
                output_label = os.path.join(output_path, 'label', label_name)
                output_label_gt = os.path.join(output_path, 'label_gt',
                                               label_name)
                copyfile(path_label, output_label_gt)
                path_calib = os.path.join(calib_dir, label_calib_name)
                velo_to_cam = read_calib(path_calib, 6)
                print(velo_to_cam)
                P2 = read_calib(path_calib, 3)
                R0_rect = read_calib(path_calib, 5)
                trans_image = P2.dot(R0_rect)
                image_to_velo = np.array([[0, -1, grid_map_data_origin_idx[0]],
                                          [-1, 0, grid_map_data_origin_idx[1]],
                                          [0, 0, 1]])
                image_name = example + '_euclidean_merge.png'
                image_path = os.path.join(image_dir, image_name)
                image_name_ground = example + '_euclidean_height_ground.png'
                image_path_ground = os.path.join(image_dir, image_name_ground)
                image_name_height_max = example + '_euclidean_height_max.png'
                image_path_height_max = os.path.join(image_dir,
                                                     image_name_height_max)
                image_name_height_min = example + '_euclidean_height_min.png'
                image_path_height_min = os.path.join(image_dir,
                                                     image_name_height_min)
                image_name_height_norm_max = example + '_euclidean_height_norm_max.png'
                image_path_height_norm_max = os.path.join(
                    image_dir, image_name_height_norm_max)
                print('Image path', image_path)
                image = cv2.imread(image_path)
                image = image[:, :, [2, 1, 0]]
                image_ground = cv2.imread(image_path_ground, 0)
                image_height_max = cv2.imread(image_path_height_max, 0)
                image_height_min = cv2.imread(image_path_height_min, 0)
                image_height_norm_max = cv2.imread(image_path_height_norm_max,
                                                   0)
                image_np_expanded = np.expand_dims(image, axis=0)
                start_time = time.time()
                (boxes, boxes_rot, scores, classes,
                 num) = sess.run([
                     detection_boxes, detection_boxes_rot, detection_scores,
                     detection_classes, num_detections
                 ],
                                 feed_dict={image_tensor: image_np_expanded})
                print('Inference time:', time.time() - start_time)
                boxes_rot_np = np.squeeze(boxes_rot)
                boxes_np = np.squeeze(boxes)
                scores_np = np.squeeze(scores)
                classes_np = np.squeeze(classes)
                vis_util.visualize_boxes_and_labels_on_image_array(
                    image,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    boxes_rot=np.squeeze(boxes_rot),
                    use_normalized_coordinates=True,
                    line_thickness=3)
                test_image_name = 'image' + str(idx) + '.png'
                test_image_path = os.path.join(output_path, test_image_name)
                cv2.imwrite(test_image_path, image)
                file_output = open(output_label, 'w')
                for i in range(scores_np.shape[0]):
                    if scores_np[i] > .75:
                        object_class = category_index[int(
                            classes_np[i])]['name']
                        box = tuple(boxes_np[i])
                        y_min = box[0] * image.shape[0]
                        x_min = box[1] * image.shape[1]
                        y_max = box[2] * image.shape[0]
                        x_max = box[3] * image.shape[1]
                        box_rot = tuple(boxes_rot_np[i])
                        print('box_rot:', box_rot)
                        x_c = box_rot[0] * image.shape[1]
                        y_c = box_rot[1] * image.shape[0]
                        w_s = box_rot[2]
                        h_s = box_rot[3]
                        angle = box_rot[4]
                        angle_rad = angle * 3.141 / 180
                        vec_h_x = h_s * math.cos(angle_rad) / 2.0
                        vec_h_y = h_s * math.sin(angle_rad) / 2.0
                        vec_w_x = -w_s * math.sin(angle_rad) / 2.0
                        vec_w_y = w_s * math.cos(angle_rad) / 2.0
                        x1 = (x_c - vec_w_x - vec_h_x) * image.shape[1]
                        x2 = (x_c - vec_w_x + vec_h_x) * image.shape[1]
                        x3 = (x_c + vec_w_x + vec_h_x) * image.shape[1]
                        y1 = (y_c - vec_w_y - vec_h_y) * image.shape[0]
                        y2 = (y_c - vec_w_y + vec_h_y) * image.shape[0]
                        y3 = (y_c + vec_w_y + vec_h_y) * image.shape[0]
                        l = math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) *
                                      (y2 - y1))
                        w = math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) *
                                      (y3 - y2))
                        image_ground_box = image_ground[
                            int(round(y_min)):int(round(y_max)),
                            int(round(x_min)):int(round(x_max))]
                        if (image_ground_box[image_ground_box < 255]
                            ).size == 0:
                            continue
                        mean_ground = image_ground_box[
                            image_ground_box < 255].mean()
                        image_height_max_box = image_height_max[
                            int(round(y_min)):int(round(y_max)),
                            int(round(x_min)):int(round(x_max))]
                        if (image_height_max_box[image_height_max_box < 255]
                            ).size == 0:
                            continue
                        height_max = image_height_max_box[
                            image_height_max_box < 255].min()
                        image_height_min_box = image_height_min[
                            int(round(y_min)):int(round(y_max)),
                            int(round(x_min)):int(round(x_max))]
                        if (image_height_min_box[image_height_min_box < 255]
                            ).size == 0:
                            continue
                        height_min = image_height_min_box[
                            image_height_min_box < 255].max()
                        image_height_norm_max_box = image_height_norm_max[
                            int(round(y_min)):int(round(y_max)),
                            int(round(x_min)):int(round(x_max))]
                        if (image_height_norm_max_box[
                                image_height_min_box < 255]).size == 0:
                            continue
                        height_norm_max = image_height_norm_max_box[
                            image_height_min_box < 255].min()
                        object_length = (y_max -
                                         y_min) * grid_map_data_resolution
                        object_width = (x_max -
                                        x_min) * grid_map_data_resolution

                        object_length_rot = l * grid_map_data_resolution
                        object_width_rot = w * grid_map_data_resolution
                        object_height = -3 * height_norm_max / 255 + 3
                        if object_class == 'Car':
                            if object_height < 1.3 or object_height > 1.9:
                                object_height = 1.56
                        #    if object_width < 1.4 or object_width > 1.8:
                        #        object_width = 1.6
                        #    if object_length < 3.2 or object_length > 4.7:
                        #        object_length = 3.9
                        object_t_rot = np.array([
                            x_c * grid_map_data_resolution,
                            y_c * grid_map_data_resolution
                        ])
                        object_t_velo_rot = image_to_velo.dot(
                            np.append(object_t_rot, 1))
                        object_t_velo_rot[2] = -2 * mean_ground / 255 - 0.75
                        object_t_cam_rot = velo_to_cam.dot(
                            np.append(object_t_velo_rot, 1))
                        print('object_t_cam_rot:', object_t_cam_rot)

                        #print('object_height', object_height)
                        #print('object_width_rot', object_width_rot)
                        #print('object_length_rot', object_length_rot)
                        #print('object_t_velo_rot', object_t_velo_rot)
                        #print('object_t_cam_rot', object_t_cam_rot)
                        #print('angle_rad', angle_rad)
                        #print('')

                        object_corners_rot = np.array(
                            [[
                                object_length_rot / 2, object_length_rot / 2,
                                -object_length_rot / 2, -object_length_rot / 2,
                                object_length_rot / 2, object_length_rot / 2,
                                -object_length_rot / 2, -object_length_rot / 2
                            ],
                             [
                                 0, 0, 0, 0, -object_height, -object_height,
                                 -object_height, -object_height
                             ],
                             [
                                 object_width_rot / 2, -object_width_rot / 2,
                                 -object_width_rot / 2, object_width_rot / 2,
                                 object_width_rot / 2, -object_width_rot / 2,
                                 -object_width_rot / 2, object_width_rot / 2
                             ], [1, 1, 1, 1, 1, 1, 1, 1]])

                        #corners_to_cam = np.array([[1, 0, 0, object_t_cam_rot[0]],[0, 1, 0, object_t_cam_rot[1]]
                        #                              ,[0, 0 , 1, object_t_cam_rot[2]],[0, 0, 0, 1]])
                        corners_to_cam = np.array([[
                            math.cos(angle_rad), 0,
                            math.sin(angle_rad), object_t_cam_rot[0]
                        ], [0, 1, 0, object_t_cam_rot[1]],
                                                   [
                                                       -math.sin(angle_rad), 0,
                                                       math.cos(angle_rad),
                                                       object_t_cam_rot[2]
                                                   ], [0, 0, 0, 1]])
                        object_corners_cam = corners_to_cam.dot(
                            object_corners_rot)
                        object_corners_image = trans_image.dot(
                            object_corners_cam)
                        object_corners_image_2d = np.array([
                            object_corners_image[0] / object_corners_image[2],
                            object_corners_image[1] / object_corners_image[2]
                        ])
                        write_labels(file_output, object_class,
                                     object_corners_image_2d, object_height,
                                     object_width_rot, object_length_rot,
                                     object_t_cam_rot, angle_rad, scores_np[i])
def cardetect():  
# This is needed since the notebook is stored in the object_detection folder.
    sys.path.append("..")

    # Import utilites
    from utils import label_map_util
    from utils import visualization_utils as vis_util

    # Name of the directory containing the object detection module we're using
    MODEL_NAME = 'inference_graph'

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

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

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

    # 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')
    link=(e1.get())
    video = cv2.VideoCapture("http://"+link+":4747/video")
    #video=cv2.VideoCapture("http://"+link+":8080/video")for using ipwebcam
    ret = video.set(3,1280)
    ret = video.set(4,720)

    while(True):

        # Acquire frame and expand frame 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
        ret, frame = video.read()
        frame_expanded = np.expand_dims(frame, 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: frame_expanded})
        # Draw the results of the detection (aka 'visulaize the results')
        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.60)
        cv2.imshow('Object detector', frame)
        
        if cv2.waitKey(1) == ord('q'):
            break
    # Clean up
    video.release()
    cv2.destroyAllWindows()
 def load_labelmap(self):
     print('> Loading label map')
     label_map = label_map_util.load_labelmap(self.label_path)
     categories = label_map_util.convert_label_map_to_categories(
         label_map, max_num_classes=self.num_classes, use_display_name=True)
     self.category_index = label_map_util.create_category_index(categories)
Example #20
0
    def __init__(self):
        self.boxes = []
        self.classes = []
        self.scores = []
        self.counter = 0
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        self.video = cv2.VideoCapture('guns.mp4')

        # Name of the directory containing the object detection module we're using
        MODEL_NAME = '../first_try_faster_rcnn_hackhacton'

        # 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, '../config',
                                      'oid_object_detection_label_map.pbtxt')

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

        # 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
        self.label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
        self.categories = label_map_util.convert_label_map_to_categories(
            self.label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
        self.category_index = label_map_util.create_category_index(
            self.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='')

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

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

        # Input tensor is the image
        self.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
        self.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.
        self.detection_scores = detection_graph.get_tensor_by_name(
            'detection_scores:0')
        self.detection_classes = detection_graph.get_tensor_by_name(
            'detection_classes:0')

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

        self.gun_predictions_queue = Queue()
        self.frames_for_face_recognition_queue = Queue()

        self.remove_duplicates_process = Process(
            target=remove_duplicates,
            args=[
                self.gun_predictions_queue,
                [
                    'static/images/references/gun_ref.jpg',
                    'static/images/references/gun_ref2.jpg',
                    'static/images/references/gun_ref3.jpg',
                    'static/images/references/gun_ref4.jpg'
                ]
            ])
        self.remove_duplicates_process.start()

        self.faces_recoginition_process = Process(
            target=face_recognition_event_loop,
            args=[
                self.frames_for_face_recognition_queue,
                ['static/images/references/face_ref.jpg']
            ])
        self.faces_recoginition_process.start()
Example #21
0
def load_labels(PATH_TO_LABELS, NUM_CLASSES):
    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 detected_img_crop(datedir):
    detect_im_dir = os.path.join(detect_path, datedir)
    results_dir = os.path.join(imsave_path, datedir)
    if not os.path.isdir(results_dir):
        os.makedirs(results_dir)

    for crop_file in os.listdir(detect_im_dir):
        if crop_file[-3:] == 'jpg':
            IMAGE_NAME = crop_file

            # 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(detect_im_dir, IMAGE_NAME)
            print('start image : ' + 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_expanded = np.expand_dims(image, axis=0)

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

            fig = plt.figure()

            max_boxes_to_draw = 5
            min_score_thresh = .8
            sq_boxes = np.squeeze(boxes)

            resultImList = set()
            for i in range(min(max_boxes_to_draw, sq_boxes.shape[0])):
                if scores is not None and np.squeeze(
                        scores)[i] > min_score_thresh:
                    (ymin, xmin, ymax, xmax) = tuple(sq_boxes[i].tolist())
                    (im_height, im_width) = image.shape[:2]
                    (xminn, xmaxx, yminn,
                     ymaxx) = (xmin * im_width, xmax * im_width,
                               ymin * im_height, ymax * im_height)

                    crop_image = tf.image.crop_to_bounding_box(
                        image, np.int32(yminn), np.int32(xminn),
                        np.int32(ymaxx - yminn), np.int32(xmaxx - xminn))
                    sess = tf.Session()
                    img_data = sess.run(crop_image)
                    sess.close()

                    fig.add_subplot()
                    class_name = category_index[np.squeeze(classes).astype(
                        np.int32)[i]]['name']
                    print(class_name,
                          str(int(100 * np.squeeze(scores)[i])) + '%')
                    class_name = class_name[:-1] if class_name.endswith(
                        '_') else class_name
                    if class_name is not 'leg':
                        img_name = IMAGE_NAME[:
                                              -4] + '_' + class_name + IMAGE_NAME[
                                                  -4:]
                        resultImList.add(img_name)
                        plt.imsave(os.path.join(results_dir, img_name),
                                   cv2.cvtColor(img_data, cv2.COLOR_BGR2RGB))

            resultImList = list(resultImList)

            if len(resultImList) != 2:
                for file in os.listdir(results_dir):
                    if os.path.isfile(os.path.join(results_dir,
                                                   file)) and file.startswith(
                                                       IMAGE_NAME[:-4]):
                        print('delete file = ', file)
                        os.remove(os.path.join(results_dir, file))
def dealVideo():
    global size
    # dir = '/Users/hongyanma/Downloads/trafficvideo'
    # flvurl = getFlvUrl('sxjgl_sdsdgs_370000011011001001267', 'G2_K459%2B246_右东')
    # dowloadVideo('fcd1f8027f524a059c59401ef6542b81', 'G1(京哈高速)天津段天昂公司K85%2B135遥控', dir)
    # flvurl = getFlvUrl('fcd1f8027f524a059c59401ef6542b81', 'G1(京哈高速)天津段天昂公司K85%2B135遥控')
    # flvurl = getFlvUrl('17c821b5-0dcd-40c8-aa86-bb92f0243392', '清河北出京出口广场(遥控)')

    # print(flvurl)
    # cap = cv2.VideoCapture('/Users/hongyanma/Downloads/trafficvideo/sxjgl_nhugs_320500011011001001063.flv')  # 打开摄像头
    # response = requests.get(flvurl, stream=True)
    # for chunk in response.iter_content(chunk_size=1024):
    #     cap = cv2.VideoCapture(chunk)
    # print(cv2.CAP_XINE)
    # cap = cv2.VideoCapture('https://youku.com-qq.net/20190526/494_d6346149/1000k/hls/index.m3u8')
    # cap = cv2.VideoCapture('https://60-9-3-149.xiu123.cn/httpflv/v69716930-171412695.flv') #https://www.6.cn/
    # cap = cv2.VideoCapture('https://ali-adaptive.pull.yximgs.com/gifshow/sgGRBqHEIW0.flv?auth_key=1581771209-0-0-77ee89696de9e928a5a0de8f47ec2db1&highTraffic=1&oidc=alihb')  # 快手的地址

    # print(cv2.VideoCaptureAPIs)
    # cap = cv2.VideoCapture(flvurl,cv2.CAP_FFMPEG)
    # cap = cv2.VideoCapture('rtmp://127.0.0.1:1935/live')
    #time.sleep(1)
    cap = cv2.VideoCapture(
        '/Users/hongyanma/gitspace/python/python/data/视频源/源视频0.avi',
        cv2.CAP_FFMPEG)
    # cap.set(cv2.CAP_PROP_FPS,15)
    # print(cv2.CAP_PROP_FPS)
    print(cap.isOpened())
    # aaa = cap.grab()
    # print(aaa)
    # testframe = cap.retrieve()
    # print(testframe)
    # cv2.imshow('mahy', testframe[1])
    # for i in range(19):
    #     print(i, cap.get(i))
    #
    # if aaa is not True:
    #     print('error')
    #     exit(-1)
    # while True:
    #     ret, image_np = cap.read()
    #     # if ret is not True:
    #     #     continue
    #     print(ret)
    #     print(image_np)
    #     print('----------------------split')
    #     cv2.imshow('mahy', image_np)  # 显示
    #     cv2.waitKey(int(1000 / int(22)))
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    print(size)
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2018_01_28'
    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
    print('Loading 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='')

    ##################### Loading label map
    print('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)
    print('Detecting...')
    image_container = {}
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            while True:
                ret, image_np = cap.read()  # 从摄像头中获取每一帧图像
                # print(image_np)
                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')
                # Actual detection.
                # print('~~~~~~~~~~~~~~')
                # print(cap.isOpened())
                if not cap.isOpened():
                    print('end of video !! ret flag')
                    break
                if not image_np_expanded.size:
                    print('end of video !!')
                    break
                try:
                    (boxes, scores, classes, num_detections) = sess.run(
                        [boxes, scores, classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})
                except:
                    print('error!!!')
                    break

                # extractData(image_np, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores),
                #              category_index, image_container)
                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)
                q.put(image_np)
                cv2.imshow('mahy', image_np)  # 显示
                cv2.waitKey(int(1000 / int(22)))
    global video_end_flag
    video_end_flag = True
    cap.release()
def objects():
    
    
    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile
    from gtts import gTTS
    import pyttsx3
    
    from collections import defaultdict
    from io import StringIO
    from matplotlib import pyplot as plt
    from PIL import Image
    
    
    from utils import label_map_util
    
    from utils import visualization_utils as vis_util
    
    
    # # 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:
        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]
          image_np_expanded = np.expand_dims(image_np, axis=0)
          image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
          # Each box represents a part of the image where a particular object was detected.
          boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
          # Each score represent how level of confidence for each of the objects.
          # Score is shown on the result image, together with the class label.
          scores = detection_graph.get_tensor_by_name('detection_scores:0')
          classes = detection_graph.get_tensor_by_name('detection_classes:0')
          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)
    #      plt.figure(figsize=IMAGE_SIZE)
    #      plt.imshow(image_np)
          a = ([category_index.get(value) for index,value in enumerate(classes[0]) if scores[0,index] > 0.5])
          #a=([category_index.get('id') for index,value in enumerate(classes[0]) if scores[0,index] > 0.5])
          #x = category_index['name']
          #a=str()
          #b = a[]
          #print(b)
          engine = pyttsx3.init()
          engine.say(a)
          engine.say("is in front of you")
          
          engine.runAndWait()
          #tts = gTTS(a,lang='en',slow=False)
          #tts.save("abc.mp3")
          #os.system("abc.mp3")
          
          
          #print(scores)
          cv2.imshow('image',cv2.resize(image_np,(1280,960)))
          if cv2.waitKey(25) & 0xFF == ord('q'):
              cv2.destroyAllWindows()
              cap.release()
              break
Example #25
0
# for object detection.
PATH_TO_CKPT_vehicle = os.path.join(CWD_PATH,"Vehicle_ssd",'frozen_inference_graph.pb')
PATH_TO_CKPT_LicensePlate = os.path.join(CWD_PATH,"License_PLate",'frozen_inference_graph.pb')

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


# 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_vehicle = label_map_util.load_labelmap(PATH_TO_LABELS_vehicle)
categories_vehicle = label_map_util.convert_label_map_to_categories(label_map_vehicle, max_num_classes=2, use_display_name=True)
category_index_vehicle = label_map_util.create_category_index(categories_vehicle)

label_map_LicensePlate = label_map_util.load_labelmap(PATH_TO_LABELS_LicensePlate)
categories_LicensePlatev = label_map_util.convert_label_map_to_categories(label_map_LicensePlate, max_num_classes=1, use_display_name=True)
category_index_LicensePlate = label_map_util.create_category_index(categories_LicensePlatev)

# Load the Tensorflow model into memory.
detection_graph_vehicle = tf.Graph()
with detection_graph_vehicle.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT_vehicle, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')
    sess_vehicle = tf.Session(graph=detection_graph_vehicle)
Example #26
0
    def __post_init__(self):
        # Grab path to current working directory
        self.CWD_PATH = self.OBJ_DETECT_PATH

        # Path to frozen detection graph .pb file, which contains the model that is used
        # for object detection.
        self.PATH_TO_CKPT = os.path.join(self.CWD_PATH, self.MODEL_NAME,
                                         'frozen_inference_graph.pb')

        # Path to label map file
        self.PATH_TO_LABELS = os.path.join(self.CWD_PATH, 'data',
                                           'mscoco_label_map.pbtxt')

        # Load the label map.
        # Label maps map indices to category names, so that when the 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
        self.label_map = label_map_util.load_labelmap(self.PATH_TO_LABELS)
        self.categories = label_map_util.convert_label_map_to_categories(
            self.label_map,
            max_num_classes=self.NUM_CLASSES,
            use_display_name=True)
        self.category_index = label_map_util.create_category_index(
            self.categories)

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

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

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

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

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

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

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

        # Initialize frame rate calculation
        self.frame_rate_calc = 1
        self.freq = self.cv2.getTickFrequency()

        # Initialize Picamera and grab reference to the raw capture
        self.camera = PiCamera()
        self.camera.resolution = (self.IM_WIDTH, self.IM_HEIGHT)
        self.camera.framerate = YAMLAccess.IM_FPS
        self.rawCapture = PiRGBArray(self.camera,
                                     size=(self.IM_WIDTH, self.IM_HEIGHT))
        self.rawCapture.truncate(0)
Example #27
0
def Predict(img,
            detection_graph,
            sess,
            MODEL_FOLDER,
            labels2show,
            threshold=0.7):

    # Grab path to current working directory
    CWD_PATH = os.getcwd()

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

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

    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)

    # color scheeme for different classes
    color_map = {
        'person': 'DeepSkyBlue',
        'dog': 'IndianRed',
        'cat': 'yellow',
        'chair': 'Cyan',
        'bottle': 'Orange'
    }

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

    # threshold of detection
    thresh = threshold

    items = []
    coordinates = []
    #if you want to resize to tune inference
    #img = cv2.resize(img_org, (300,300))
    img_expanded = np.expand_dims(img, axis=0)
    #print(img_expanded.shape)
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: img_expanded})

    objects = []
    for index, value in enumerate(classes[0]):
        object_dict = {}
        if scores[0, index] > thresh:
            object_dict[(
                category_index.get(value)).get('name')] = scores[0, index]
            objects.append(object_dict)
            #print (objects)

    #Get all the detected class labels in one list
    for y in objects:
        for keys in y.keys():
            m = list(y.keys())[0]
            items.append(m)

    #Get co ordinates of the detected classes
    coordinates = vis_util.return_coordinates(img,
                                              np.squeeze(boxes),
                                              np.squeeze(classes).astype(
                                                  np.int32),
                                              np.squeeze(scores),
                                              category_index,
                                              use_normalized_coordinates=True,
                                              line_thickness=10,
                                              min_score_thresh=thresh)

    new_items = []
    new_coordinates = []
    display_str_list = []
    for i, item in enumerate(items):
        if item.lower() in labels2show:
            new_items.append(item.lower())
            new_coordinates.append(coordinates[i][:-1])
            display_str_list.append(
                [item + ' : ' + str(int(coordinates[i][-1])) + '%'])

    for i, box in enumerate(new_coordinates):

        ymin, ymax, xmin, xmax = box[0], box[1], box[2], box[3]
        display_str = display_str_list[i]
        color = color_map[new_items[i]]
        vis_util.draw_bounding_box_on_image_array(
            img,
            ymin,
            xmin,
            ymax,
            xmax,
            color=color,
            thickness=4,
            display_str_list=display_str,
            use_normalized_coordinates=False)

    return new_coordinates, new_items, img
Example #28
0
def detect_traffic_lights(PATH_TO_TEST_IMAGES_DIR, MODEL_NAME, Num_images, plot_flag=False):
    """
    Detect traffic lights and draw bounding boxes around the traffic lights
    :param PATH_TO_TEST_IMAGES_DIR: testing image directory
    :param MODEL_NAME: name of the model used in the task
    """

    # --------test images------
    TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, 'img_{}.jpg'.format(
        i)) for i in range(1, Num_images+1)]

    commands = []

    # What model to download
    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 = 'mscoco_label_map.pbtxt'

    # number of classes for COCO dataset
    NUM_CLASSES = 90

    # --------Download model----------
    if path.isdir(MODEL_NAME) is False:
        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.compat.v2.io.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.compat.v1.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')

            for image_path in TEST_IMAGE_PATHS:
                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)
                # 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})
                # print([category_index.get(i) for i in classes[0]])
                # print(scores) # traffic lights are first
                result = read_traffic_lights(image, np.squeeze(boxes), np.squeeze(
                    scores), np.squeeze(classes).astype(np.int32))
                print(result)

                # Visualization of the results of a detection.
                if plot_flag:
                    plot_origin_image(image_np, boxes, classes,
                                      scores, category_index)

    return commands
Example #29
0
def get_frame2():
    flat=True

    sys.setrecursionlimit(10000)
    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2018_01_28'
    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')

    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)

    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]
          image_np_expanded = np.expand_dims(image_np, axis=0)
          image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
          # Each box represents a part of the image where a particular object was detected.
          boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
          # Each score represent how level of confidence for each of the objects.
          # Score is shown on the result image, together with the class label.
          scores = detection_graph.get_tensor_by_name('detection_scores:0')
          classes = detection_graph.get_tensor_by_name('detection_classes:0')
          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)
    #      plt.figure(figsize=IMAGE_SIZE)
    #      plt.imshow(image_np)
          imgencode=cv2.imencode('.jpg',image_np)[1]
          lb=[category_index.get(value).get('name') for index,value in enumerate(classes[0]) if scores[0,index] > 0.5]
          if "car" in lb and flat==True:
            cap.release()  
            flat=False    
            ins(lb)
            time.sleep(2)
            flat=True
            cap = cv2.VideoCapture(0)
          if "motorcycle" in lb and flat==True:
            cap.release()  
            flat=False   
            ins(lb) 
            time.sleep(2)
            flat=True
            cap = cv2.VideoCapture(0)
          if "truck" in lb and flat==True:
            cap.release()  
            flat=False    
            ins(lb)
            time.sleep(2)
            flat=True
            cap = cv2.VideoCapture(0)

          if cv2.waitKey(25) & 0xFF == ord('q'):
              exit()
          stringData=imgencode.tostring()
          yield (b'--frame\r\n'
              b'Content-Type: text/plain\r\n\r\n'+stringData+b'\r\n')
Example #30
0
def predict_image_tfapi(detection_graph,
                        sess,
                        PATH_TO_CKPT,
                        PATH_TO_LABELS,
                        PATH_TO_IMAGE,
                        NUM_CLASSES,
                        output_dir,
                        tfapi_min_score_thresh=1e-5,
                        is_show=True,
                        is_img_save=True,
                        class_model=None,
                        dict_class={0.0: "Car"},
                        class_min_score_thresh=0.5,
                        model_height=331,
                        model_width=331,
                        TTA=None,
                        TTA_rotate_deg=0,
                        TTA_crop_num=0):
    """
    tfAPIでpredict
    kerasの分類モデル引数にあればそのモデルでもpredict
    Args:
        PATH_TO_CKPT: path to frozen detection graph.pb
        PATH_TO_LABELS: path to labelmap.pbtxt
        PATH_TO_IMAGE: path to image file
        NUM_CLASSES: number of classes
        output: path to output directory of detected image file
        tfapi_min_score_thresh: tfAPIのpredictの閾値
        is_show: 検出画像表示するか
        is_img_save: 検出画像保存するか
        class_model: kerasの分類モデルオブジェクト
        dict_class: kerasの分類モデルのクラスのidとクラス名の辞書型データ
        class_min_score_thresh: kerasの分類モデルのpredictの閾値
        model_height, model_width: 予測するkerasの分類モデルの入力画像サイズ(modelのデフォルトのサイズである必要あり)
        TTA*:TTA option
    """
    # Load the label map.
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    #print(label_map)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    #print(categories)
    category_index = label_map_util.create_category_index(categories)
    #print(category_index)

    # Define input and output tensors (i.e. data) for the object detection classifier
    # Input tensor is the image
    image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
    # Output tensors are the detection boxes, scores, and classes
    # Each box represents a part of the image where a particular object was detected
    detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
    # Each score represents level of confidence for each of the objects.
    # The score is shown on the result image, together with the class label.
    detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
    detection_classes = detection_graph.get_tensor_by_name(
        'detection_classes:0')
    # Number of objects detected
    num_detections = detection_graph.get_tensor_by_name('num_detections:0')

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

    # Perform the actual detection by running the model with the image as input
    (boxes, scores, classes, num) = sess.run(
        [detection_boxes, detection_scores, detection_classes, num_detections],
        feed_dict={image_tensor: image_expanded})
    #print(type(classes))
    #print(classes.shape)
    #print(classes)
    #print(type(scores))
    #print(scores.shape)
    #print(scores)
    # -------------------- 分類モデルで予測 --------------------
    if class_model is not None:
        category_index = make_category_index_from_dict_class(dict_class)
        boxes = np.squeeze(boxes)  # np.squeeze: サイズ1の次元の削除(4次元テンソルなので3次元にする)
        scores = np.squeeze(scores)
        boxes_class_model = []
        classes_class_id_model = []
        classes_class_name_model = []
        scores_class_model = []
        for box, score in zip(boxes, scores):
            #print(box)
            ymin, xmin, ymax, xmax = box
            #print(ymin, xmin, ymax, xmax)

            # 位置情報無いレコードは除く
            # tfAPIの予測領域は(0,0,0,0)の結果が始まったら後続のレコードもすべて(0,0,0,0)なのでbreakする
            if (ymin == 0.0) & (xmin == 0.0) & (ymax == 0.0) & (xmax == 0.0):
                break

            # 領域予測の閾値
            if score < tfapi_min_score_thresh:
                continue

            img_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            # (ndarray型の画像データから)検出領域切り出し
            # ndarray型の切り出しは[y:y_max,x:x_max]の順番じゃないとおかしくなる
            # https://qiita.com/tadOne/items/8967f046ca395669329d
            dst = img_RGB[int(img_RGB.shape[0] * ymin):int(img_RGB.shape[0] *
                                                           ymax),
                          int(img_RGB.shape[1] *
                              xmin):int(img_RGB.shape[1] *
                                        xmax)]  # スライス[:]はint型でないとエラー
            # ここで画像表示すると、bbox付き画像保存されない.あくまで確認用
            #print(dst)
            #plt.imshow(dst / 255.)
            #plt.show()
            # 切り出し画像を分類モデルでpredict
            class_conf, class_label_id = predict_class_model(
                dst,
                class_model,
                model_height,
                model_width,
                TTA=TTA,
                TTA_rotate_deg=TTA_rotate_deg,
                TTA_crop_num=TTA_crop_num)
            #print('class_label_name :', dict_class[class_label_id])
            #print('class_conf :', class_conf)

            # 分類モデルの閾値
            if class_conf >= class_min_score_thresh:
                boxes_class_model.append(box)
                classes_class_id_model.append(int(class_label_id))
                classes_class_name_model.append(str(
                    dict_class[class_label_id]))
                scores_class_model.append(float(class_conf))

        boxes = np.array(boxes_class_model)
        classes = np.array(
            [int(c) for c in classes_class_id_model]
        )  # intでないと vis_util.visualize_boxes_and_labels_on_image_array()でエラーになる
        classes_class_name_model = np.array(classes_class_name_model)
        scores = np.array(scores_class_model)
        # 元の型である4次元テンソルへ変換
        boxes = np.expand_dims(boxes, axis=0)
        classes = np.expand_dims(classes, axis=0)
        classes_class_name_model = np.expand_dims(classes_class_name_model,
                                                  axis=0)
        scores = np.expand_dims(scores, axis=0)
        print('boxes.shape:', boxes.shape)
        #print(boxes)
        #print(type(classes))
        print('classes.shape:', classes.shape)
        #print(classes)
        #print(type(scores))
        print('scores.shape:', scores.shape)
        #print(scores)
    # ---------------------------------------------------------
    else:
        img_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        classes = classes.astype(
            np.int32
        )  # intでないと vis_util.visualize_boxes_and_labels_on_image_array()でエラーになる

    # 検出数が1のときに検出画像をだすとエラーになるのでtryで囲む
    try:
        # 検出画像保存するか
        if is_img_save == True:
            print("Draw the results of the detection......")
            if boxes.shape == (1, 1, 4):
                vis_boxes = boxes.reshape(
                    1, 4
                )  # 検出数が1つだけのときnp.squeezeするとclassesのshape=()となりエラーになるためreshapeでサイズ固定する
                vis_classes = classes.reshape(1, )
                vis_scores = scores.reshape(1, )
            else:
                vis_boxes = np.squeeze(boxes)  # np.squeeze: サイズ1の次元の削除
                vis_classes = np.squeeze(classes)
                vis_scores = np.squeeze(scores)
            #print(vis_boxes.shape)
            #print(vis_classes.shape)
            #print(vis_classes)
            #print(vis_scores.shape)
            # Draw the results of the detection (aka 'visulaize the results')
            vis_util.visualize_boxes_and_labels_on_image_array(
                image,
                vis_boxes,
                vis_classes,
                vis_scores,
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8,
                max_boxes_to_draw=int(num[0]),  # 描画するbboxの最大数
                min_score_thresh=tfapi_min_score_thresh  # 領域予測の閾値
            )
            img_RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            PIL.Image.fromarray(img_RGB).save(
                os.path.join(output_dir,
                             str(Path(PATH_TO_IMAGE).stem) + '.jpg'))  # ファイル出力

            # 検出画像表示するか
            if is_show == True:
                plt.figure(figsize=(4, 4), dpi=200)
                plt.imshow(image)
                plt.show()
    except Exception as e:
        print('#### visualize_boxes Exception. img path:', PATH_TO_IMAGE,
              '#####')
        print(e)

    # ローカル変数の存在をチェック
    if 'classes_class_name_model' in locals():
        # 分類モデルで予測ある時
        return boxes, scores, classes_class_name_model, img_RGB
    else:
        # 分類モデルで予測ない時
        return boxes, scores, classes, img_RGB
Example #31
0
def object_detection_function(video_link, model_name, id):
    global _folder_path
    filename = "video_" + str(id) + "_upd.mp4"
    #filename = "video_1_61.mp4"
    print("processing..", filename)

    urllib.request.urlretrieve(video_link, filename)
    cap = cv2.VideoCapture(filename)
    #Use these if you want to know the height and width of the video and set the ROI line Accordingly.

    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    roi = int(height * 2 / 3)
    if (height == 1080):
        roi = 650
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    vis_util.helper(roi)

    # By default I 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 = model_name
    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
    # uncomment if you have not download the model yet
    # 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 I 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)
    outputFile = filename + "_Faster_out.avi"
    total_passed_vehicle = 0
    total_cars = [0] * 2
    total_trucks = [0] * 2
    total_bus = [0] * 2
    total_person = [0] * 2
    total_motorcycle = [0] * 2
    speed = 'waiting...'
    direction = 'waiting...'
    size = 'waiting...'
    color = 'waiting...'
    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')

            # for all the frames that are extracted from input video
            vid_writer = cv2.VideoWriter(
                outputFile, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30,
                (width, height))
            while cap.isOpened():
                (ret, frame) = cap.read()

                if not ret:
                    print('end of the video file...')
                    break

                input_frame = frame

                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(input_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.
                (counter, csv_line) = \
                    vis_util.visualize_boxes_and_labels_on_image_array(
                    cap.get(1),
                    input_frame,
                    np.squeeze(boxes),
                    np.squeeze(classes).astype(np.int32),
                    np.squeeze(scores),
                    category_index,
                    use_normalized_coordinates=True,
                    line_thickness=4,
                    )

                total_passed_vehicle = total_passed_vehicle + sum(counter)
                if direction == 'up':
                    total_cars[0] = total_cars[0] + counter[0]
                    total_trucks[0] = total_trucks[0] + counter[1]
                    total_bus[0] = total_bus[0] + counter[2]
                    total_person[0] = total_person[0] + counter[3]
                    total_motorcycle[0] = total_motorcycle + counter[4]
                elif direction == "down":
                    total_cars[1] = total_cars[1] + counter[0]
                    total_trucks[1] = total_trucks[1] + counter[1]
                    total_bus[1] = total_bus[1] + counter[2]
                    total_person[1] = total_person[1] + counter[3]
                    total_motorcycle[1] = total_motorcycle[1] + counter[4]

                #print(counter)
                # insert information text to video frame
                font = cv2.FONT_HERSHEY_SIMPLEX
                cv2.putText(
                    input_frame,
                    'Vehicles:' + str(total_passed_vehicle) + ' Car up:' +
                    str(total_cars[0]) + ' C down:' + str(total_cars[1]) +
                    ' Truck up:' + str(total_trucks[0]) + ' T down:' +
                    str(total_trucks[1]),
                    (10, 35),
                    font,
                    0.8,
                    (0, 0xFF, 0),
                    2,
                    cv2.FONT_HERSHEY_SIMPLEX,
                )
                cv2.putText(
                    input_frame,
                    'Bus up:' + str(total_bus[0]) + ' B d:' +
                    str(total_bus[1]) + ' P up:' + str(total_person[0]) +
                    ' P down:' + str(total_person[1]) + ' Bike up:' +
                    str(total_motorcycle[0]) + ' B d:' +
                    str(total_motorcycle[1]),
                    (10, 55),
                    font,
                    0.8,
                    (0, 0xFF, 0),
                    2,
                    cv2.FONT_HERSHEY_SIMPLEX,
                )

                # when the vehicle passed over line and counted, make the color of ROI line green
                if sum(counter) == 1:
                    cv2.line(input_frame, (0, roi), (width, roi), (0, 0xFF, 0),
                             5)
                else:
                    cv2.line(input_frame, (0, roi), (width, roi), (0, 0, 0xFF),
                             5)

                # insert information text to video frame
                #cv2.rectangle(input_frame, (10, 275), (230, 337), (180, 132, 109), -1)
                cv2.putText(
                    input_frame,
                    'ROI Line',
                    (width - 100, roi - 40),
                    font,
                    0.6,
                    (0, 0, 0xFF),
                    2,
                    cv2.LINE_AA,
                )

                vid_writer.write(input_frame.astype(np.uint8))
                #cv2.imshow('vehicle detection', input_frame)

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

                if csv_line != 'not_available':
                    with open('traffic_measurement.csv', 'a') as f:
                        writer = csv.writer(f)
                        (size, color, direction, speed) = \
                            csv_line.split(',')
                        writer.writerows([csv_line.split(',')])
            cap.release()
            cv2.destroyAllWindows()

            #time2 = cv2.getTickCount()
            #time = (time2-time1)/ cv2.getTickFrequency()

            #print("Total Time taken to process the video :",time)
            return ([
                total_cars[0], total_cars[1], total_person[0], total_person[1],
                total_motorcycle[0], total_motorcycle[1], total_trucks[0],
                total_trucks[1]
            ])
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()
def load_labels(path, num_classes):
    label_map = label_map_util.load_labelmap(path)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=num_classes, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    return category_index
Example #34
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

with detection_graph.as_default():
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')